This commit is contained in:
Lulzette 2021-09-20 02:18:03 +03:00
parent 5aecf2d643
commit 85a860eb1a

23
main.py
View File

@ -2,18 +2,23 @@
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 - public, posts table from MongoClient posts - table with posts
config: private, config for DB config structure:
config.db.host DB:
config.db.port - host
config.db.dbname - port
- dbname
""" """
def __init__(self): def __init__(self):
""" """
Init init Init config
""" """
self.readConfig() self.readConfig()
@ -26,7 +31,7 @@ class Config:
def readConfig(self): def readConfig(self):
""" """
Read config file, if not exists - call Init.createConfig() Read config file, if not exists - call self.createConfig()
""" """
import configparser import configparser
config = configparser.ConfigParser() config = configparser.ConfigParser()
@ -70,12 +75,15 @@ 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}}
@ -86,6 +94,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 bool(posts.delete_one({'name': name}).deleted_count) return bool(posts.delete_one({'name': name}).deleted_count)