-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcool.py
95 lines (76 loc) · 2.47 KB
/
cool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import kev
import formy
from envs import env
from flask import Flask, render_template, request, redirect, url_for
from kev.loading import KevHandler
from formy.kev import KEVForm
app = Flask(__name__)
kev_handler = KevHandler({
's3':{
'backend':'kev.backends.s3.db.S3DB',
'connection':{
'bucket':env('DATA_BUCKET')
}
},
})
ADMIN_PREFIX = env('ADMIN_PREFIX','/admin')
class Post(kev.Document):
title = kev.CharProperty(required=True)
slug = kev.SlugProperty(required=True,unique=True)
body = kev.CharProperty(required=True)
class Meta:
use_db = 's3'
handler = kev_handler
class PostForm(KEVForm):
_template = 'formy.templates.form.bootstrap_template'
title = formy.CharField(required=True)
slug = formy.SlugField(required=True)
body = formy.CKEditor(required=True)
class Meta:
document_class = Post
@app.route('/')
def posts():
p = Post.all()
return render_template('home.html',posts=p)
@app.route('/post/<slug>')
def post(slug):
p = Post.objects().get({'slug':slug})
return render_template('post.html',post=p)
@app.route('{}/posts/create/'.format(ADMIN_PREFIX),methods=['GET','POST'])
def create_post():
if request.method == 'POST':
data = {k:v for k,v in request.form.items()}
f = PostForm(**data)
f.validate()
if f._is_valid:
p = f.save()
return redirect(url_for('list_posts'))
else:
f = PostForm()
return render_template('admin/create.html',form=f)
@app.route('{}/posts/'.format(ADMIN_PREFIX))
def list_posts():
p = Post.all()
return render_template('admin/list.html', posts=p)
@app.route('{}/update/<slug>'.format(ADMIN_PREFIX),methods=['GET','POST'])
def update_post(slug):
p = Post.objects().get({'slug':slug})
if request.method == 'POST':
data = {k: v for k, v in request.form.items()}
f = PostForm(_initial=p,**data)
f.validate()
if f._is_valid:
p = f.save()
return redirect(url_for('list_posts'))
else:
f = PostForm(_initial=p)
return render_template('admin/update.html',form=f,post=p)
@app.route('{}/delete/<slug>'.format(ADMIN_PREFIX),methods=['GET','POST'])
def delete_post(slug):
p = Post.objects().get({'slug': slug})
if request.method == 'POST':
p.delete()
return redirect(url_for('list_posts'))
return render_template('admin/delete.html',post=p)
if __name__ == '__main__':
app.run()