Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move utility/setup functions in frontend to separate modules
The modules currently have the following responsibilities: - `__init__` - only imports the other modules (including plugins) to ensure that all routes, filters etc. have been registered - `base` - initialization of the app and db, definition of the session and basic globals - `filters` - jinja2 filters for templates - `model_additions` - properties monkey-patched on top of models, such as state icons - `template_functions` - utility functions primarily aimed to be used within templates - `util` - utility functions for flash messages and ordering - `tabs` - definition of tab menu - `forms` - form definitions and utilities - `auth` - authentication - `api` - API - `views` - the actual controller part, could be further split
- Loading branch information
Showing
17 changed files
with
554 additions
and
409 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
# Copyright (C) 2014-2016 Red Hat, Inc. | ||
# | ||
# This program is free software; you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation; either version 2 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License along | ||
# with this program; if not, write to the Free Software Foundation, Inc., | ||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
# | ||
# Author: Michael Simacek <msimacek@redhat.com> | ||
|
||
from __future__ import print_function, absolute_import | ||
|
||
import logging | ||
|
||
from flask import Flask, abort, request, g | ||
from flask_sqlalchemy import BaseQuery, Pagination | ||
from sqlalchemy.orm import scoped_session, sessionmaker | ||
|
||
from koschei.config import get_config | ||
from koschei.db import Query, get_engine | ||
from koschei.models import LogEntry, Collection, Package, Build, ResolutionChange | ||
from koschei.session import KoscheiSession | ||
|
||
dirs = get_config('directories') | ||
app = Flask('koschei', template_folder=dirs['templates'], | ||
static_folder=dirs['static_folder'], | ||
static_url_path=dirs['static_url']) | ||
app.config.update(get_config('flask')) | ||
|
||
frontend_config = get_config('frontend') | ||
|
||
|
||
class FrontendQuery(Query, BaseQuery): | ||
# pylint:disable=arguments-differ | ||
def paginate(self, items_per_page): | ||
try: | ||
page = int(request.args.get('page', 1)) | ||
except ValueError: | ||
abort(400) | ||
if page < 1: | ||
abort(404) | ||
items = self.limit(items_per_page)\ | ||
.offset((page - 1) * items_per_page).all() | ||
if not items and page != 1: | ||
abort(404) | ||
if page == 1 and len(items) < items_per_page: | ||
total = len(items) | ||
else: | ||
total = self.order_by(None).count() | ||
return Pagination(self, page, items_per_page, total, items) | ||
|
||
db = scoped_session(sessionmaker(autocommit=False, bind=get_engine(), | ||
query_cls=FrontendQuery)) | ||
|
||
|
||
class KoscheiFrontendSession(KoscheiSession): | ||
db = db | ||
log = logging.getLogger('koschei.frontend') | ||
|
||
def log_user_action(self, message, **kwargs): | ||
self.db.add( | ||
LogEntry(environment='frontend', user=g.user, message=message, **kwargs), | ||
) | ||
|
||
|
||
session = KoscheiFrontendSession() | ||
|
||
|
||
@app.teardown_appcontext | ||
def shutdown_session(exception=None): | ||
db.remove() | ||
|
||
|
||
@app.context_processor | ||
def inject_fedmenu(): | ||
if 'fedmenu_url' in frontend_config: | ||
return { | ||
'fedmenu_url': frontend_config['fedmenu_url'], | ||
'fedmenu_data_url': frontend_config['fedmenu_data_url'], | ||
} | ||
return {} | ||
|
||
|
||
@app.before_request | ||
def get_collections(): | ||
if request.endpoint == 'static': | ||
return | ||
collection_name = request.args.get('collection') | ||
g.collections = db.query(Collection)\ | ||
.order_by(Collection.order.desc(), Collection.name.desc())\ | ||
.all() | ||
for collection in g.collections: | ||
db.expunge(collection) | ||
if not g.collections: | ||
abort(500, "No collections setup") | ||
g.collections_by_name = {c.name: c for c in g.collections} | ||
g.collections_by_id = {c.id: c for c in g.collections} | ||
g.current_collections = [] | ||
if collection_name: | ||
try: | ||
for component in collection_name.split(','): | ||
g.current_collections.append(g.collections_by_name[component]) | ||
except KeyError: | ||
abort(404, "Collection not found") | ||
else: | ||
g.current_collections = g.collections | ||
|
||
|
||
def secondary_koji_url(collection): | ||
if collection.secondary_mode: | ||
return get_config('secondary_koji_config.weburl') | ||
return get_config('koji_config.weburl') | ||
|
||
|
||
app.jinja_env.globals.update( | ||
# configuration variables | ||
koschei_version=get_config('version'), | ||
primary_koji_url=get_config('koji_config.weburl'), | ||
secondary_koji_url=secondary_koji_url, | ||
fedora_assets_url=frontend_config['fedora_assets_url'], | ||
# builtin python functions | ||
inext=next, iter=iter, min=min, max=max, | ||
# model classes | ||
Package=Package, | ||
Build=Build, | ||
ResolutionChange=ResolutionChange, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright (C) 2014-2017 Red Hat, Inc. | ||
# | ||
# This program is free software; you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation; either version 2 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License along | ||
# with this program; if not, write to the Free Software Foundation, Inc., | ||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
# | ||
# Author: Michael Simacek <msimacek@redhat.com> | ||
|
||
from datetime import datetime | ||
|
||
import humanize | ||
from jinja2 import Markup | ||
|
||
from koschei.frontend.base import app | ||
|
||
app.add_template_filter(humanize.intcomma, 'intcomma') | ||
app.add_template_filter(humanize.naturaltime, 'naturaltime') | ||
app.add_template_filter(humanize.naturaldelta, 'naturaldelta') | ||
|
||
|
||
@app.template_filter() | ||
def percentage(val): | ||
return format(val * 10000, '.4f') + Markup(' ‱') | ||
|
||
|
||
@app.template_filter('date') | ||
def date_filter(date): | ||
return date.strftime("%F %T") if date else '' | ||
|
||
|
||
@app.template_filter() | ||
def epoch(dt): | ||
return int((dt - datetime.fromtimestamp(0)).total_seconds()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.