-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.py
executable file
·354 lines (284 loc) · 10.3 KB
/
admin.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Description {{{
"""
imposter.admin
~~~~~~~~~~~~~~
Admin interface for the Imposter weblog app
:copyright: (c) 2010-2011 by Jochem Kossen.
:license: BSD, see LICENSE.txt for more details.
"""
# }}}
# Imports {{{
from __future__ import with_statement
from flask import Flask, request, session, abort, redirect, url_for, flash
from functools import wraps
from database import DB
from models import User, Tag, Format, Status, Post, Page
from datetime import datetime
from sqlalchemy.sql import and_
from flaskjk import Viewer, Paginator, validate_password, slugify
from frontend import filter_public
from forms import PostForm, PageForm, LoginForm
# }}}
# Initialization {{{
app = Flask(__name__, static_path=None)
app.config.from_pyfile('config_admin.py')
app.config.from_envvar('IMPOSTER_ADMIN_CONFIG', silent=True)
db_session = DB(app.config['DATABASE']).get_session()
viewer = Viewer(app, 'admin')
# base query used in all frontend retrieve queries
public_posts_base = db_session.query(Post, Status, User).filter(filter_public())
# }}}
# Helper functions {{{
def login_required(fun):
"""Decorator for functions which require an authorized user"""
@wraps(fun)
def decorated_function(*args, **kwargs):
"""Decorated function"""
if not session.get('user_id'):
return login()
return fun(*args, **kwargs)
return decorated_function
def get_format(value):
"""Retrieve Format object based on given String"""
query = Format.query.filter(Format.value==value)
return query.one()
def get_status(value):
"""Retrieve Status object based on given String"""
query = Status.query.filter(Status.value==value)
return query.one()
def get_post(post_id):
""" Retrieve Post object based on given Post id"""
post = Post.query.filter(and_(Post.user_id==session['user_id'],
Post.id==post_id)).first()
if post is None:
abort(404)
return post
def get_page(page_id):
""" Retrieve Page object based on given Page id"""
page = Page.query.filter(and_(Page.user_id==session['user_id'],
Page.id==page_id)).first()
if page is None:
abort(404)
return page
def recalculate_tagcount(tag):
"""Calculate how many times a given tag is used in public Posts"""
tag.count = public_posts_base.filter(Post.tags.contains(tag)).count()
# }}}
# Template filters {{{
@app.template_filter('strftime')
def strftime(value, format='%a, %d %b %Y %H:%M:%S %Z'):
"""Template filter for human-readable date formats"""
return value.strftime(format)
# }}}
# Views {{{
@viewer.view('static_files')
def static_files(filename):
"""Send static files such as style sheets, JavaScript, etc."""
return viewer.static(filename)
@viewer.view('login', methods=['POST'])
def login():
"""Check user credentials and initialize session"""
# use a single error message for all failures so the "user" doesn't see what
# specifically went wrong
default_error = 'ERROR: Unknown user'
form = LoginForm()
if request.method == 'POST':
form = LoginForm(request.form)
if form.validate():
try:
user = User.query.filter(
User.username==request.form['username']).one()
except:
flash(default_error, category='error')
return viewer.render('login.html', form=form)
if validate_password(app.config['SECRET_KEY'],
request.form['password'],
user.password):
# We have a valid login, initialize session
session['username'] = user.username
session['user_id'] = user.id
flash('Logged in successfully', category='info')
return redirect(url_for('index'))
else:
flash(default_error, category='error')
else:
flash('Incorrect form input', category='error')
return viewer.render('login.html', form=form)
@viewer.view('logout')
@login_required
def logout():
"""Clear the session"""
session.clear()
flash('You were logged out', category='info')
return redirect(url_for('index'))
@viewer.view('posts_list')
@login_required
def posts_list(page=1):
"""Paginated view of all posts"""
p_order = Post.pubdate.desc()
posts = db_session.query(Post).filter(
Post.user_id==session['user_id']).order_by(Post.pubdate.desc())
paginator = Paginator(posts, app.config['ENTRIES_PER_PAGE'], page,
'posts_list')
return viewer.render('posts_list.html', posts=posts, paginator=paginator)
@viewer.view('pages_list')
@login_required
def pages_list(page=1):
"""Paginated view of all pages"""
p_order = Page.pubdate.desc()
pages = db_session.query(Page).filter(
Page.user_id==session['user_id']).order_by(Page.pubdate.desc())
paginator = Paginator(pages, app.config['ENTRIES_PER_PAGE'], page,
'pages_list')
return viewer.render('pages_list.html', pages=pages, paginator=paginator)
@viewer.view('index')
@login_required
def index():
"""The front page of this application"""
posts = db_session.query(Post).filter(
Post.user_id==session['user_id']).order_by(Post.pubdate.desc())
pages = db_session.query(Page).filter(
Page.user_id==session['user_id'])
return viewer.render('index.html', posts=posts, pages=pages)
@viewer.view('recalculate_tagcounts')
@login_required
def recalculate_tagcounts():
"""Recount all tag uses"""
for tag in db_session.query(Tag):
recalculate_tagcount(tag)
db_session.commit()
return redirect(url_for('index'))
@viewer.view('new_post')
@viewer.view('edit_post')
@login_required
def edit_post(post_id=None, post_form=None):
"""Render form to edit a Post"""
formats = db_session.query(Format).all()
statuses = db_session.query(Status).all()
post = None
form = None
if post_id:
post = get_post(post_id)
if post_form is None:
form = PostForm(obj=post)
else:
form = post_form
return viewer.render('edit_post.html',
form=form,
post=post,
formats=formats,
statuses=statuses)
@viewer.view('save_new_post', methods=['POST'])
@viewer.view('save_post', methods=['POST'])
@login_required
def save_post(post_id=None):
"""Save Post to database
If post_id is None a new Post will be inserted in the database. Otherwise
the existing Post will be updated.
"""
message = 'Post updated'
orig_tags = []
post_form = PostForm(request.form)
if not post_form.validate():
flash('ERROR: errors detected. Post NOT saved!', category='error')
return edit_post(post_id=post_id, post_form=post_form)
# test if we're creating a new post, or updating an existing one
if post_id is None:
post = Post()
post.status_id = 1
post.user_id = session['user_id']
post.createdate = datetime.now()
else:
post = get_post(post_id)
orig_tags = [tag for tag in post.tags]
post_form.populate_obj(post)
post.lastmoddate = datetime.now()
# compile input to html
post.compile(app.config['REPL_TAGS'])
# update pubdate if post's pubdate is None and its status is set
# to public
if request.form['status'] == 'public' and \
unicode(post.status) != 'public' and \
post.pubdate is None:
post.pubdate = datetime.now()
post.status = get_status(request.form['status'])
if post.slug is None:
post.slug = slugify(post.title)
if post_id is None:
db_session.add(post)
message = 'New post was successfully added'
db_session.commit()
for tag in orig_tags:
recalculate_tagcount(tag)
for tag in post.tags:
if tag not in orig_tags:
recalculate_tagcount(tag)
db_session.commit()
flash(message, category='info')
return redirect(url_for('edit_post', post_id=post.id))
@viewer.view('new_page')
@viewer.view('edit_page')
@login_required
def edit_page(page_id=None, page_form=None):
"""Render form to edit a Page"""
formats = db_session.query(Format).all()
statuses = db_session.query(Status).all()
page = None
form = None
if page_id:
page = get_page(page_id)
if page_form is None:
form = PageForm(obj=page)
else:
form = page_form
return viewer.render('edit_page.html',
form=form,
page=page,
formats=formats,
statuses=statuses)
@viewer.view('save_new_page', methods=['POST'])
@viewer.view('save_page', methods=['POST'])
@login_required
def save_page(page_id=None):
"""Save Page to database
If page_id is None a new Page will be inserted in the database. Otherwise
the existing Page will be updated.
"""
message = 'Page updated'
page_form = PageForm(request.form)
if not page_form.validate():
flash('ERROR: errors detected. Page NOT saved!', category='error')
return edit_page(page_id=page_id, page_form=page_form)
if page_id is None:
page = Page(request.form['title'], request.form['content'])
page.status_id = 1
page.user_id = session['user_id']
page.createdate = datetime.now()
else:
page = get_page(page_id)
page_form.populate_obj(page)
page.lastmoddate = datetime.now()
# compile input to html
page.compile(app.config['REPL_TAGS'])
# update pubdate if page's pubdate is None and its status is set
# to public
if request.form['status'] == 'public' and \
unicode(page.status) != 'public' and \
page.pubdate is None:
page.pubdate = datetime.now()
page.status = get_status(request.form['status'])
if page.slug is None:
page.slug = slugify(page.title)
if page_id is None:
db_session.add(page)
message = 'New page was successfully added'
db_session.commit()
flash(message)
return redirect(url_for('edit_page', page_id=page.id))
# }}}
# Main run loop {{{
if __name__ == '__main__':
app.run(host=app.config['HOST'], port=app.config['PORT'])
# }}}