memegen.routes.api_aliases.get()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
crap 3
1 1
from collections import OrderedDict
2
3 1
from flask import Blueprint, current_app as app, redirect, request
0 ignored issues
show
introduced by
Unable to import 'flask'
Loading history...
4 1
from webargs import fields, flaskparser
0 ignored issues
show
introduced by
Unable to import 'webargs'
Loading history...
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))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable request does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable bool does not seem to be defined.
Loading history...
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