pycms/main.py

60 lines
1.2 KiB
Python
Raw Normal View History

#!/usr/bin/python3
from bottle import route, run, template, debug, request
2021-09-07 04:54:24 +03:00
import pymongo
2021-09-07 04:54:24 +03:00
mongoclient = pymongo.MongoClient('localhost', 27017)
database = mongoclient['pycms']
posts = database['posts']
@route('/post/<name>')
def post(name):
'''
Get post
'''
2021-09-07 04:54:24 +03:00
return posts.find_one({'name':name})['text']
@route('/post/<name>', method='POST')
def post(name):
'''
Insert/Update post
'''
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)
@route('/post/<name>', method='DELETE')
def post(name):
'''
Delete post by name
'''
2021-09-07 04:54:24 +03:00
return str(posts.delete_one({'name':name}))
@route('/posts')
def all_posts():
'''
Returns all posts
'''
dict_posts = list()
for i in posts.find():
dict_posts.append(i)
return str(dict_posts)
@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)