Работает на mongodb.
This commit is contained in:
parent
9ec4879ac5
commit
6b4325f835
BIN
.main.py.swp
BIN
.main.py.swp
Binary file not shown.
10
README.md
10
README.md
@ -1 +1,11 @@
|
|||||||
Здесь будет CMS на питоне с MongoDB.
|
Здесь будет CMS на питоне с MongoDB.
|
||||||
|
|
||||||
|
DB: pythoncms
|
||||||
|
Table: posts
|
||||||
|
Format:
|
||||||
|
|
||||||
|
{
|
||||||
|
"_id": "0123456789",
|
||||||
|
"name": "Title",
|
||||||
|
"text": "Hello, this is post"
|
||||||
|
}
|
||||||
|
26
main.py
26
main.py
@ -1,9 +1,10 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
from bottle import route, run, template, debug, request
|
from bottle import route, run, template, debug, request
|
||||||
import json
|
import pymongo
|
||||||
import os
|
|
||||||
|
|
||||||
database = dict(posts=dict(sdf='hello'))
|
mongoclient = pymongo.MongoClient('localhost', 27017)
|
||||||
|
database = mongoclient['pycms']
|
||||||
|
posts = database['posts']
|
||||||
|
|
||||||
|
|
||||||
# /post [GET]
|
# /post [GET]
|
||||||
@ -11,31 +12,36 @@ database = dict(posts=dict(sdf='hello'))
|
|||||||
|
|
||||||
@route('/post/<name>')
|
@route('/post/<name>')
|
||||||
def post(name):
|
def post(name):
|
||||||
# return template('<b>Hello {{name}}</b>!', name=database['posts'][name])
|
return posts.find_one({'name':name})['text']
|
||||||
return database['posts'][name]
|
|
||||||
|
|
||||||
# /post POST
|
# /post [POST]
|
||||||
|
|
||||||
|
|
||||||
@route('/post/<name>', method='POST')
|
@route('/post/<name>', method='POST')
|
||||||
def post(name):
|
def post(name):
|
||||||
body = request.forms.get('body')
|
body = request.forms.get('body')
|
||||||
newPost = {name: body}
|
# If post exists, update it
|
||||||
return database['posts'].update(newPost)
|
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)
|
||||||
|
|
||||||
|
|
||||||
# /post [DELETE]
|
# /post [DELETE]
|
||||||
|
|
||||||
@route('/post/<name>', method='DELETE')
|
@route('/post/<name>', method='DELETE')
|
||||||
def post(name):
|
def post(name):
|
||||||
return database['posts'].pop(name)
|
return str(posts.delete_one({'name':name}))
|
||||||
|
|
||||||
# /debug (database)
|
# /debug (database)
|
||||||
|
|
||||||
|
|
||||||
@route('/debug')
|
@route('/debug')
|
||||||
def debug():
|
def debug():
|
||||||
return database
|
return type(posts)
|
||||||
|
|
||||||
|
|
||||||
@route('/')
|
@route('/')
|
||||||
|
Loading…
Reference in New Issue
Block a user