2021-09-06 05:34:47 +03:00
|
|
|
#!/usr/bin/python3
|
2021-09-07 07:14:31 +03:00
|
|
|
from bottle import abort, route, run, template, debug, request
|
2021-09-07 04:54:24 +03:00
|
|
|
import pymongo
|
2021-09-06 05:34:47 +03:00
|
|
|
|
2021-09-07 04:54:24 +03:00
|
|
|
mongoclient = pymongo.MongoClient('localhost', 27017)
|
|
|
|
database = mongoclient['pycms']
|
|
|
|
posts = database['posts']
|
2021-09-06 05:34:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
@route('/post/<name>')
|
|
|
|
def post(name):
|
2021-09-07 05:17:29 +03:00
|
|
|
'''
|
|
|
|
Get post
|
|
|
|
'''
|
2021-09-07 07:14:31 +03:00
|
|
|
try:
|
|
|
|
return posts.find_one({'name':name})['text']
|
|
|
|
except TypeError:
|
|
|
|
return abort(404, 'No such page')
|
2021-09-06 05:34:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
@route('/post/<name>', method='POST')
|
|
|
|
def post(name):
|
2021-09-07 05:17:29 +03:00
|
|
|
'''
|
|
|
|
Insert/Update post
|
|
|
|
'''
|
2021-09-06 05:34:47 +03:00
|
|
|
body = request.forms.get('body')
|
2021-09-07 04:54:24 +03:00
|
|
|
# If post exists, update it
|
|
|
|
if posts.find_one({'name': name}):
|
|
|
|
newPost = {'$set': {'text': body}}
|
|
|
|
return str(posts.update_one({'name': name}, newPost))
|
|
|
|
# Else - create new
|
|
|
|
else:
|
|
|
|
newPost = {'name': name, 'text': body}
|
|
|
|
return str(posts.insert_one(newPost).inserted_id)
|
2021-09-06 05:34:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
@route('/post/<name>', method='DELETE')
|
|
|
|
def post(name):
|
2021-09-07 05:17:29 +03:00
|
|
|
'''
|
|
|
|
Delete post by name
|
|
|
|
'''
|
2021-09-07 04:54:24 +03:00
|
|
|
return str(posts.delete_one({'name':name}))
|
2021-09-06 05:34:47 +03:00
|
|
|
|
2021-09-07 05:17:29 +03:00
|
|
|
@route('/posts')
|
|
|
|
def all_posts():
|
|
|
|
'''
|
|
|
|
Returns all posts
|
|
|
|
'''
|
|
|
|
dict_posts = list()
|
|
|
|
for i in posts.find():
|
|
|
|
dict_posts.append(i)
|
2021-09-06 05:34:47 +03:00
|
|
|
|
2021-09-07 05:17:29 +03:00
|
|
|
return str(dict_posts)
|
2021-09-06 05:34:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
@route('/')
|
|
|
|
def index():
|
2021-09-07 07:14:31 +03:00
|
|
|
try:
|
|
|
|
return posts.find_one({'name':'/'})['text']
|
|
|
|
except TypeError:
|
|
|
|
return abort(404, 'No such page')
|
2021-09-06 05:34:47 +03:00
|
|
|
|
2021-09-07 07:29:43 +03:00
|
|
|
@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)
|
2021-09-06 05:34:47 +03:00
|
|
|
|
2021-09-07 06:35:22 +03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
run(host='0.0.0.0', port=8081, reloader=True, debug=True)
|