2021-09-06 05:34:47 +03:00
|
|
|
#!/usr/bin/python3
|
|
|
|
from bottle import 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 04:54:24 +03:00
|
|
|
return posts.find_one({'name':name})['text']
|
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():
|
|
|
|
return "Hello"
|
|
|
|
|
|
|
|
|
2021-09-07 06:35:22 +03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
run(host='0.0.0.0', port=8081, reloader=True, debug=True)
|