def manage(): p = int(request.args.get('p') or 1) tag_id = request.args.get('tag') or None tag_idint = None if tag_id: tag_idint = Obfuscator.restore(tag_id) deleted = int(request.args.get('deleted') or 0) posts, count = Post.query(p=p, num=SITE['num_per_page'], tag_id=tag_idint, author_id=session['author_id'], deleted=deleted) tp = math.ceil(count / SITE['num_per_page']) prev_url = url_for('manage', p=p-1, tag=tag_id) if p > 1 else '' next_url = url_for('manage', p=p+1, tag=tag_id) if p < tp else '' return render_template('manage.html', posts=posts, prev_url=prev_url, next_url=next_url, deleted=deleted)
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 rssfeed(): rss = ET.Element('rss', version='2.0') channel = ET.SubElement(rss, 'channel') ET.SubElement(channel, 'title').text = SITE['title'] ET.SubElement(channel, 'description').text = SITE['title'] + '- A blog powered by jerry.' ET.SubElement(channel, 'link').text = SITE['url'] posts, count = Post.query(p=1, num=SITE['num_per_page']) for p in posts: item = ET.SubElement(channel, 'item') ET.SubElement(item, 'title').text = p.title ET.SubElement(item, 'link').text = SITE['url'] + '/posts/' + p.idstr ET.SubElement(item, 'description').text = p.html ET.SubElement(item, 'author').text = p.author.name ET.SubElement(item, 'pubDate').text = p.published.strftime('%a, %d %b %Y %H:%M:%S GMT') for t in p.tags: ET.SubElement(item, 'category').text = t.name return Response(ET.tostring(rss, encoding='unicode'), mimetype='text/xml')
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)
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'))