Привет, классы
This commit is contained in:
parent
bb64fbaf62
commit
ee18b5535c
93
main.py
93
main.py
@ -1,5 +1,5 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
from bottle import abort, route, run, template, debug, request
|
from bottle import abort, route, run, request
|
||||||
import pymongo
|
import pymongo
|
||||||
|
|
||||||
mongoclient = pymongo.MongoClient('localhost', 27017)
|
mongoclient = pymongo.MongoClient('localhost', 27017)
|
||||||
@ -7,23 +7,43 @@ database = mongoclient['pycms']
|
|||||||
posts = database['posts']
|
posts = database['posts']
|
||||||
|
|
||||||
|
|
||||||
@route('/post/<name>')
|
class Init:
|
||||||
def post(name):
|
def readConfig():
|
||||||
'''
|
"""
|
||||||
Get post
|
Read config file, if not exists - call Init.createConfig()
|
||||||
'''
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def createConfig():
|
||||||
|
"""
|
||||||
|
Create config file
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def setupConnect():
|
||||||
|
"""
|
||||||
|
Setup MongoDB connection
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Back:
|
||||||
|
"""
|
||||||
|
All actions that will be triggered by http
|
||||||
|
"""
|
||||||
|
def getRootPost():
|
||||||
try:
|
try:
|
||||||
return posts.find_one({'name':name})['text']
|
return posts.find_one({'name': '_root_'})['text']
|
||||||
except TypeError:
|
except TypeError:
|
||||||
return abort(404, 'No such page')
|
return abort(404, 'No such page')
|
||||||
|
|
||||||
|
def getPost(name):
|
||||||
|
try:
|
||||||
|
return posts.find_one({'name': name})['text']
|
||||||
|
except TypeError:
|
||||||
|
return abort(404, 'No such page')
|
||||||
|
|
||||||
@route('/post/<name>', method='POST')
|
def updatePost(name, body):
|
||||||
def post(name):
|
|
||||||
'''
|
|
||||||
Insert/Update post
|
|
||||||
'''
|
|
||||||
body = request.forms.get('body')
|
|
||||||
# If post exists, update it
|
# If post exists, update it
|
||||||
if posts.find_one({'name': name}):
|
if posts.find_one({'name': name}):
|
||||||
newPost = {'$set': {'text': body}}
|
newPost = {'$set': {'text': body}}
|
||||||
@ -33,15 +53,37 @@ def post(name):
|
|||||||
newPost = {'name': name, 'text': body}
|
newPost = {'name': name, 'text': body}
|
||||||
return str(posts.insert_one(newPost).inserted_id)
|
return str(posts.insert_one(newPost).inserted_id)
|
||||||
|
|
||||||
|
def deletePost(name):
|
||||||
|
return posts.delete_one({'name': name})
|
||||||
|
|
||||||
|
|
||||||
|
@route('/post/<name>')
|
||||||
|
def post(name):
|
||||||
|
'''
|
||||||
|
Get post
|
||||||
|
'''
|
||||||
|
return Back.getPost(name)
|
||||||
|
|
||||||
|
|
||||||
|
@route('/post/<name>', method='POST')
|
||||||
|
def postUpd(name):
|
||||||
|
'''
|
||||||
|
Insert/Update post
|
||||||
|
'''
|
||||||
|
body = request.forms.get('body')
|
||||||
|
return Back.updatePost(name=name, body=body)
|
||||||
|
|
||||||
|
|
||||||
@route('/post/<name>', method='DELETE')
|
@route('/post/<name>', method='DELETE')
|
||||||
def post(name):
|
def postDel(name):
|
||||||
'''
|
'''
|
||||||
Delete post by name
|
Delete post by name
|
||||||
'''
|
'''
|
||||||
return str(posts.delete_one({'name':name}))
|
# return str(posts.delete_one({'name':name}))
|
||||||
|
return str(Back.deletePost(name))
|
||||||
|
|
||||||
@route('/posts')
|
|
||||||
|
@route('/post')
|
||||||
def all_posts():
|
def all_posts():
|
||||||
'''
|
'''
|
||||||
Returns all posts
|
Returns all posts
|
||||||
@ -55,25 +97,8 @@ def all_posts():
|
|||||||
|
|
||||||
@route('/')
|
@route('/')
|
||||||
def index():
|
def index():
|
||||||
try:
|
return Back.getRootPost()
|
||||||
return posts.find_one({'name':'/'})['text']
|
|
||||||
except TypeError:
|
|
||||||
return abort(404, 'No such page')
|
|
||||||
|
|
||||||
@route('/', method='POST')
|
|
||||||
def post():
|
|
||||||
'''
|
|
||||||
Insert/Update post
|
|
||||||
'''
|
|
||||||
body = request.forms.get('body')
|
|
||||||
# If post exists, update it
|
|
||||||
if posts.find_one({'name': '/'}):
|
|
||||||
newPost = {'$set': {'text': body}}
|
|
||||||
return str(posts.update_one({'name': '/'}, newPost))
|
|
||||||
# Else - create new
|
|
||||||
else:
|
|
||||||
newPost = {'name': '/', 'text': body}
|
|
||||||
return str(posts.insert_one(newPost).inserted_id)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
run(host='0.0.0.0', port=8081, reloader=True, debug=True)
|
run(host='0.0.0.0', port=8081, reloader=True, debug=True)
|
||||||
|
Loading…
Reference in New Issue
Block a user