Compare commits
2 Commits
f72c941a3a
...
85a860eb1a
Author | SHA1 | Date | |
---|---|---|---|
85a860eb1a | |||
5aecf2d643 |
30
main.py
30
main.py
@ -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,7 +94,8 @@ 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):
|
||||||
return posts.delete_one({'name': name})
|
# TODO: return RESTful error/success result
|
||||||
|
return bool(posts.delete_one({'name': name}).deleted_count)
|
||||||
|
|
||||||
|
|
||||||
@route('/post/<name>')
|
@route('/post/<name>')
|
||||||
@ -97,7 +106,7 @@ def post(name):
|
|||||||
return str(back.getPost(name))
|
return str(back.getPost(name))
|
||||||
|
|
||||||
|
|
||||||
@route('/post/<name>', method='POST')
|
@route('/admin/post/<name>', method='POST')
|
||||||
def postUpd(name):
|
def postUpd(name):
|
||||||
'''
|
'''
|
||||||
Insert/Update post
|
Insert/Update post
|
||||||
@ -106,12 +115,11 @@ def postUpd(name):
|
|||||||
return back.updatePost(name=name, body=body)
|
return back.updatePost(name=name, body=body)
|
||||||
|
|
||||||
|
|
||||||
@route('/post/<name>', method='DELETE')
|
@route('/admin/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))
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user