#!/usr/bin/env python # -*- coding=UTF-8 -*- # ************************************************************************** # Copyright © 2016 jianglin # File Name: views.py # Author: jianglin # Email: xiyang0807@gmail.com # Created: 2016-05-20 13:18:19 (CST) # Last Update:星期五 2016-6-17 13:28:58 (CST) # By: # Description: # ************************************************************************** from flask import Blueprint, render_template, request, url_for from maple import app from maple.helpers import is_num from maple.topic.models import Tags, Topic from urllib.parse import urljoin from werkzeug.contrib.atom import AtomFeed from maple.filters import Filters site = Blueprint('tag', __name__) @site.route('', defaults={'tag': None}) @site.route('/') def tag(tag): if tag is None: tags = Tags.query.distinct(Tags.tagname).all() data = {'tags': tags} return render_template('forums/tag_list.html', **data) else: page = is_num(request.args.get('page')) topics = Topic.query.join(Topic.tags).filter( Tags.tagname == tag).paginate(page, app.config['PER_PAGE'], error_out=True) tag = Tags.query.filter_by(tagname=tag).first_or_404() data = {'tag': tag, 'topics': topics} return render_template('forums/tag.html', **data) @site.route('//feed') def rss(tag): feed = AtomFeed('Recent Topics', feed_url=request.url, url=request.url_root, subtitle='I like solitude, yearning for freedom') topics = Topic.query.join(Topic.tags).filter(Tags.tagname == tag).limit( 10).all() for topic in topics: feed.add( topic.title, Filters.safe_markdown(topic.content) if topic.is_markdown else topic.content, content_type='html', author=topic.author.username, url=urljoin(request.url_root, url_for('topic.topic', uid=topic.uid)), updated=topic.publish, published=topic.publish) return feed.get_response()