def post_add(): title = request.form['title'].strip() md_content = request.form['markdown'].strip() tag_names = request.form.getlist('tags') post = Post() post.title = title post.markdown = md_content post.html = htmlmin.minify(markdown.markdown(md_content, extensions=['extra', 'codehilite', 'nl2br', 'toc'])) post.author_id = session['author_id'] post.tags = Tag.query_and_create(tag_names) post.published = post.modified = datetime.utcnow() db = g.db db.add(post) db.commit() return redirect(url_for('manage'))
def post_update(post_id): title = request.form.get('title') md_content = request.form.get('markdown') tag_names = request.form.getlist('tags') p = Post.get(post_id) if title: p.title = title.strip() if md_content: p.markdown = md_content.strip() p.html = htmlmin.minify(markdown.markdown(md_content, extensions=['extra', 'codehilite', 'nl2br', 'toc'])) if tag_names: p.tags = Tag.query_and_create(tag_names) db = g.db if db.dirty: db.commit() return redirect(url_for('manage'))
def index(): p = int(request.args.get('p') or 1) tag_id = request.args.get('tag') author_id = request.args.get('author') month_str = request.args.get('month') tag_idint = None author_idint = None tag_name = None author_name = None month = None if tag_id: tag_idint = Obfuscator.restore(tag_id) tag_name = getattr(Tag.get(tag_idint), 'name', None) if author_id: author_idint = Obfuscator.restore(author_id) author_name = getattr(Author.get(author_idint), 'name', None) if month_str: try: month = datetime.strptime(month_str, '%Y-%m') except ValueError: month = None posts, count = Post.query(p=p, num=SITE['num_per_page'], tag_id=tag_idint, author_id=author_idint, month=month) page_num = math.ceil(count / SITE['num_per_page']) next_url = url_for('index', p=p+1, tag=tag_id, author=author_id, month=month_str) if p < page_num else None prev_url = url_for('index', p=p-1, tag=tag_id, author=author_id, month=month_str) if p > 1 else None context = dict( posts=posts, tag_name=tag_name, author_name=author_name, month=month, next_url=next_url, prev_url=prev_url, p=p, title=SITE['title'], ) return render_template('index.html', **context)