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