| Total Complexity | 6 |
| Total Lines | 49 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | from collections import OrderedDict |
||
| 2 | |||
| 3 | from flask import Blueprint, current_app as app, redirect, request |
||
| 4 | from webargs import fields, flaskparser |
||
| 5 | |||
| 6 | from ..extensions import cache |
||
| 7 | |||
| 8 | from ._utils import route |
||
| 9 | |||
| 10 | |||
| 11 | blueprint = Blueprint('aliases', __name__, url_prefix="/api/aliases/") |
||
| 12 | |||
| 13 | FILTER = { |
||
| 14 | 'name': fields.Str(missing=None) |
||
| 15 | } |
||
| 16 | |||
| 17 | |||
| 18 | @blueprint.route("") |
||
| 19 | @flaskparser.use_kwargs(FILTER) |
||
| 20 | @cache.cached(unless=lambda: bool(request.args)) |
||
| 21 | def get(name): |
||
| 22 | """Get a list of all matching aliases.""" |
||
| 23 | if name: |
||
| 24 | return redirect(route('.get_with_name', name=name)) |
||
| 25 | else: |
||
| 26 | return _get_aliases() |
||
| 27 | |||
| 28 | |||
| 29 | @blueprint.route("<name>") |
||
| 30 | def get_with_name(name): |
||
| 31 | """Get a list of all matching aliases.""" |
||
| 32 | return _get_aliases(name) |
||
| 33 | |||
| 34 | |||
| 35 | def _get_aliases(name=None): |
||
| 36 | items = OrderedDict() |
||
| 37 | |||
| 38 | for alias in sorted(app.template_service.aliases(name)): |
||
| 39 | template = app.template_service.find(alias) |
||
| 40 | |||
| 41 | data = OrderedDict() |
||
| 42 | data['styles'] = sorted(template.styles) |
||
| 43 | data['template'] = \ |
||
| 44 | route('templates.create', key=template.key, _external=True) |
||
| 45 | |||
| 46 | items[alias] = data |
||
| 47 | |||
| 48 | return items |
||
| 49 |