Completed
Pull Request — master (#388)
by
unknown
07:45
created

create_meme()   F

Complexity

Conditions 9

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
dl 0
loc 37
ccs 21
cts 21
cp 1
crap 9
rs 3
c 0
b 0
f 0
1 1
from collections import OrderedDict
2
3 1
from flask import Blueprint, current_app, request, redirect
0 ignored issues
show
Configuration introduced by
The import flask could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
4 1
from flask_api import exceptions
0 ignored issues
show
Configuration introduced by
The import flask_api could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
5 1
from webargs import fields
0 ignored issues
show
Configuration introduced by
The import webargs could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

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