memegen.routes.api_templates   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 80
rs 10
c 0
b 0
f 0
ccs 40
cts 42
cp 0.9524
wmc 11

4 Functions

Rating   Name   Duplication   Size   Complexity  
A get() 0 9 2
A get_meme_with_path() 0 5 1
A create_template() 0 3 1
B create_meme() 0 33 7
1 1
from collections import OrderedDict
2
3 1
from flask import Blueprint, current_app, request, redirect
0 ignored issues
show
introduced by
Unable to import 'flask'
Loading history...
4 1
from flask_api import exceptions
0 ignored issues
show
introduced by
Unable to import 'flask_api'
Loading history...
5 1
from webargs import fields
0 ignored issues
show
introduced by
Unable to import 'webargs'
Loading history...
6
7 1
from ..domain import Text
8
from ..extensions import cache
9 1
10 1
from ._parser import parser
11
from ._utils import route
12
13 1
14
blueprint = Blueprint('templates', __name__, url_prefix="/api/templates/")
15 1
16
OPTIONS = {
17
    'top': fields.Str(missing=""),
18
    'bottom': fields.Str(missing=""),
19
    '_redirect': fields.Bool(load_from='redirect', missing=True),
20
    '_masked': fields.Bool(load_from='masked', missing=False),
21
}
22
23 1
24
@blueprint.route("")
25
@cache.cached()
26 1
def get():
27 1
    """Get a list of all meme templates."""
28 1
    data = OrderedDict()
29 1
    for template in sorted(current_app.template_service.all()):
30 1
        url = route('.create', key=template.key, _external=True)
31
        data[template.name] = url
32
    return data
33 1
34
35 1
@blueprint.route("", methods=['POST'])
36
def create_template():
37
    raise exceptions.PermissionDenied(current_app.config['CONTRIBUTING_URL'])
38 1
39 1
40
@blueprint.route("<key>", methods=['GET', 'POST'], endpoint='create')
41
@parser.use_kwargs(OPTIONS)
42 1
def create_meme(key, top, bottom, _redirect, _masked):
43 1
    """Generate a meme from a template."""
44 1
    if request.method == 'GET':
45 1
        template = current_app.template_service.find(key)
46
        if template.key != key:
47 1
            return redirect(route('.create', key=template.key))
48 1
49 1
        data = OrderedDict()
50 1
        data['name'] = template.name
51 1
        data['description'] = template.link
52 1
        data['aliases'] = sorted(template.aliases + [template.key])
53
        data['styles'] = template.styles
54 1
        data['example'] = route('links.get', key=key,
55
                                path=template.sample_path, _external=True)
56 1
        return data
57 1
58 1
    if top or bottom:
59
        text = Text([top, bottom], translate_spaces=False)
60 1
    else:
61
        text = Text("_")
62 1
63
    if _masked:
64
        code = current_app.link_service.encode(key, text.path)
65
        url = route('image.get_encoded', code=code, _external=True)
66 1
    else:
67
        url = route('image.get', key=key, path=text.path, _external=True)
68 1
69 1
    if _redirect:
70
        return redirect(url, 303)
71 1
72
    return dict(href=url)
73
74
75
@blueprint.route("<key>/<path:path>")
76
def get_meme_with_path(key, path):
77 1
    """Redirect if any additional path is provided."""
78
    template = current_app.template_service.find(key)
79
    return redirect("/{}/{}".format(template.key, path))
80