Skip to content

Instantly share code, notes, and snippets.

@exileed
Created March 3, 2018 13:26
Show Gist options
  • Save exileed/d71d8eda98767a64836247ddadff4645 to your computer and use it in GitHub Desktop.
Save exileed/d71d8eda98767a64836247ddadff4645 to your computer and use it in GitHub Desktop.
"""
Application router decoretor.
"""
from functools import wraps
ROUTES = dict()
def bluprint_add_routes(blueprint, routes):
"""Assign url route function configuration to given blueprint."""
for route in routes:
blueprint.add_url_rule(
route['rule'],
endpoint=route.get('endpoint', None),
view_func=route['view_func'],
**route.get('options', {}))
def get_routes(module):
"""Filter routes by given module name."""
if module in ROUTES.keys():
return list(ROUTES[module])
else:
return ()
def application_route(rule, **kargs):
"""Decorator function that collects application routes."""
def wrapper(funcion): # pylint: disable=missing-docstring
if funcion.__module__ not in ROUTES.keys():
ROUTES[funcion.__module__] = []
ROUTES[funcion.__module__].append(dict(
rule=rule,
view_func=funcion,
options=kargs,
))
@wraps(funcion)
def wrapped(*args, **kwargs): # pylint: disable=missing-docstring
return funcion(*args, **kwargs)
return wrapped
return wrapper
"""
Public module.
"""
from flask import Blueprint
from app.utils.route import bluprint_add_routes
from app.public.pages import ROUTES as PAGE_ROUTES
from app.public.contact_us import ROUTES as CONTACT_US_ROUTES
BLUEPRINT = Blueprint('public', __name__,
template_folder='../templates/public')
bluprint_add_routes(BLUEPRINT, PAGE_ROUTES + CONTACT_US_ROUTES)
"""Public pages for unathorised users."""
from flask import render_template
from app.utils.route import application_route, get_routes
@application_route("/")
def home():
"""Show main landing page."""
return render_template('home.jade', page=None)
@application_route("/<slug>")
def page(slug):
"""Show custom public page."""
return render_template('page.jade', page=None)
ROUTES = get_routes("app.public.pages")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment