memegen.routes.api_templates   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

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