Compare commits

..

No commits in common. "85a860eb1a4c137be5ab9adffbfde4c33881268e" and "f72c941a3a984438ed24e273671762c2101b9e61" have entirely different histories.

30
main.py
View File

@ -2,23 +2,18 @@
from bottle import abort, route, run, request
import pymongo
# TODO: auth to /admin
# TODO: timestamps to posts
# TODO: author to posts and multiple users
class Config:
"""
posts - table with posts
config structure:
DB:
- host
- port
- dbname
posts - public, posts table from MongoClient
config: private, config for DB
config.db.host
config.db.port
config.db.dbname
"""
def __init__(self):
"""
Init config
Init init
"""
self.readConfig()
@ -31,7 +26,7 @@ class Config:
def readConfig(self):
"""
Read config file, if not exists - call self.createConfig()
Read config file, if not exists - call Init.createConfig()
"""
import configparser
config = configparser.ConfigParser()
@ -75,15 +70,12 @@ class Back():
return abort(404, 'No such page')
def getAllPosts(self):
# TODO: clear up output, remove '_id' and 'body',
# now there should be only
dict_posts = list()
for i in posts.find():
dict_posts.append(i)
return str(dict_posts)
def updatePost(self, name, body):
# TODO: return RESTful error/success result
# If post exists, update it
if posts.find_one({'name': name}):
newPost = {'$set': {'text': body}}
@ -94,8 +86,7 @@ class Back():
return str(posts.insert_one(newPost).inserted_id)
def deletePost(self, name):
# TODO: return RESTful error/success result
return bool(posts.delete_one({'name': name}).deleted_count)
return posts.delete_one({'name': name})
@route('/post/<name>')
@ -106,7 +97,7 @@ def post(name):
return str(back.getPost(name))
@route('/admin/post/<name>', method='POST')
@route('/post/<name>', method='POST')
def postUpd(name):
'''
Insert/Update post
@ -115,11 +106,12 @@ def postUpd(name):
return back.updatePost(name=name, body=body)
@route('/admin/post/<name>', method='DELETE')
@route('/post/<name>', method='DELETE')
def postDel(name):
'''
Delete post by name
'''
# return str(posts.delete_one({'name':name}))
return str(back.deletePost(name))