class CategorizedFlatPages(object): def __init__(self): self.flat_pages = FlatPages() self.root_category = Category(None, '<root>') def init_app(self, app): self.flat_pages.init_app(app) self._set_categories() def __iter__(self): return iter(sorted(self.root_category.categories.values(), cmp=compare)) def get(self, category_id, article_id): category = self.root_category.categories.get(category_id) if category is None: return None return category.articles.get(article_id) def get_articles_of_category(self, category_id): barticles = [] for a in self.root_category.categories.get( category_id).articles.values(): if a.id != 'index': barticles.append(a) return barticles def get_or_404(self, category_id, article_id): page = self.get(category_id, article_id) if page is None: abort(404) return page def _set_categories(self): for page in self.flat_pages: components = page.path.split('/') parent = self.root_category for category_id in components[:-1]: parent = parent.add_category(category_id) page_name = components[-1] parent.add_article(page_name, page) def reload(self): self.flat_pages.reload() self._set_categories()
class CategorizedFlatPages(object): def __init__(self): self.flat_pages = FlatPages() self.root_category = Category(None, '<root>') def init_app(self, app): self.flat_pages.init_app(app) self._set_categories() def __iter__(self): return iter(sorted(self.root_category.categories.values(), cmp=compare)) def get(self, category_id, article_id): category = self.root_category.categories.get(category_id) if category is None: return None return category.articles.get(article_id) def get_articles_of_category(self, category_id): barticles = [] for a in self.root_category.categories.get(category_id).articles.values(): if a.id != 'index': barticles.append(a) return barticles def get_or_404(self, category_id, article_id): page = self.get(category_id, article_id) if page is None: abort(404) return page def _set_categories(self): for page in self.flat_pages: components = page.path.split('/') parent = self.root_category for category_id in components[:-1]: parent = parent.add_category(category_id) page_name = components[-1] parent.add_article(page_name, page) def reload(self): self.flat_pages.reload() self._set_categories()
from flask import Flask from flask_flatpages import FlatPages, pygments_style_defs app = Flask(__name__) pages = FlatPages() APP_SETTINGS = "app.cfg" app.config.from_pyfile(APP_SETTINGS) pages.init_app(app) import blog.views
class CategorizedFlatPages: """The main interface to gather pages and categories * What is it used for? - Looping: E.g. In the navbar - get news → get_articles_of_category('news') - get static page → get_or_404() """ def __init__(self): self.flat_pages = FlatPages() self.root_category = Category(self, None, '<root>') self.app = None def init_app(self, app): assert self.app is None, "Already initialized with an app" self.app = app app.cf_pages = self self.flat_pages.init_app(app) self._init_categories() @property def categories(self): """Yield all categories as an iterable """ return sorted(self.root_category.categories.values(), key=attrgetter('rank')) def get(self, category_id, article_id): category = self.root_category.categories.get(category_id) if category is None: return None return category._articles.get(article_id) def get_category(self, category_id): """Return the `Category` object from a given name (id) """ return self.root_category.categories.get(category_id) def get_articles_of_category(self, category_id): """Get the articles of a category - ONLY used for fetching news """ category = self.get_category(category_id) if category is None: return [] return [article for article in category._articles.values() if article.id != 'index'] def get_or_404(self, category_id, article_id): """Fetch a static page""" page = self.get(category_id, article_id) if page is None: abort(404) return page def _init_categories(self): # TODO: Store categories, not articles for page in self.flat_pages: # get category + page name # plus, assert that there is nothing more to that. components = page.path.split('/') parent = self.root_category for category_id in components[:-1]: parent = parent.add_child_category(category_id) prefix = components[-1] parent.add_article(prefix, page) def reload(self): self.flat_pages.reload() self._init_categories()
app.login_manager = views.login_session.LoginManager() funnelapp.login_manager = lastuserapp.login_manager = app.login_manager db.init_app(app) db.init_app(funnelapp) db.init_app(lastuserapp) db.app = app migrate = Migrate(app, db) mail.init_app(app) mail.init_app(funnelapp) mail.init_app(lastuserapp) app.config['FLATPAGES_MARKDOWN_EXTENSIONS'] = ['markdown.extensions.nl2br'] pages.init_app(app) redis_store.init_app(app) rq.init_app(app) rq.init_app(funnelapp) rq.init_app(lastuserapp) baseframe.init_app( app, requires=['funnel'], ext_requires=[ 'pygments', 'toastr', 'baseframe-mui', 'jquery.cookie',
class CategorizedFlatPages: """The main interface to gather pages and categories * What is it used for? - Looping: E.g. In the navbar - get news → get_articles_of_category('news') - get static page → get_or_404() """ def __init__(self): self.flat_pages = FlatPages() self.root_category = Category(None, '<root>') def init_app(self, app): self.flat_pages.init_app(app) self._init_categories() @property def categories(self): """Yield all categories as an iterable """ return sorted(self.root_category.categories.values(), key=attrgetter('rank')) def get(self, category_id, article_id): category = self.root_category.categories.get(category_id) if category is None: return None return category._articles.get(article_id) def get_category(self, category_id): """Return the `Category` object from a given name (id) """ return self.root_category.categories.get(category_id) def get_articles_of_category(self, category_id): """Get the articles of a category - ONLY used for fetching news """ articles = [] category = self.get_category(category_id) if category: for a in category._articles.values(): if a.id != 'index': articles.append(a) return articles def get_or_404(self, category_id, article_id): """Fetch a static page""" page = self.get(category_id, article_id) if page is None: abort(404) return page def _init_categories(self): # TODO: Store categories, not articles for page in self.flat_pages: # get category + page name # plus, assert that there is nothing more to that. components = page.path.split('/') parent = self.root_category for category_id in components[:-1]: parent = parent.add_child_category(category_id) basename = components[-1] parent.add_article(basename, page) def reload(self): self.flat_pages.reload() self._init_categories()
# -*- coding: utf-8 -*- '''mysite __init__ ''' from flask import Flask from flask_flatpages import FlatPages from flask_mail import Mail app = Flask(__name__, instance_relative_config=True) app.config.from_object('config') app.config.from_pyfile('config.py') Email = Mail(app) app.config['FLATPAGES_EXTENSION'] = '.md' flatpages = FlatPages() flatpages.init_app(app) # 注册蓝图 # 后import blog 是因为防止循环导入。 #blog中引用了flatpage, 如果放在前面则flatpage还未创建从而引发错误 from .blog import blog #from .api import api app.register_blueprint(blog) #app.register_blueprint(api)