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