Completed
Pull Request — master (#308)
by Jace
02:14
created

create_meme()   D

Complexity

Conditions 8

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
c 1
b 0
f 0
dl 0
loc 33
rs 4
ccs 21
cts 21
cp 1
crap 8
1 1
from collections import OrderedDict
2
3 1
from flask import Blueprint, current_app as 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 ._settings import CONTRIBUTING_URL
11 1
from ._utils import route
12
13
14 1
blueprint = Blueprint('templates', __name__, url_prefix="/api/templates/")
15
16 1
OPTIONS = {
17
    # pylint: disable=no-member
18
    'top': fields.Str(missing=""),
19
    'bottom': fields.Str(missing=""),
20
    '_redirect': fields.Bool(load_from='redirect', missing=True),
21
}
22
23
24 1
@blueprint.route("")
25
def get():
26
    """Get a list of all meme templates."""
27 1
    data = OrderedDict()
28 1
    for template in sorted(app.template_service.all()):
29 1
        url = route('.create', key=template.key, _external=True)
30 1
        data[template.name] = url
31 1
    return data
32
33
34 1
@blueprint.route("", methods=['POST'])
35
def create_template():
36 1
    raise exceptions.PermissionDenied(CONTRIBUTING_URL)
37
38
39 1
@blueprint.route("<key>", methods=['GET', 'POST'], endpoint='create')
40 1
@parser.use_kwargs(OPTIONS)
41
def create_meme(key, top, bottom, _redirect):
42
    """Generate a meme from a template."""
43 1
    if request.method == 'GET':
44 1
        template = app.template_service.find(key)
45 1
        if template.key != key:
46 1
            return redirect(route('.create', key=template.key))
47
48 1
        data = OrderedDict()
49 1
        data['name'] = template.name
50 1
        data['description'] = template.link
51 1
        data['aliases'] = sorted(template.aliases + [template.key])
52 1
        data['styles'] = template.styles
53 1
        data['example'] = route('links.get', key=key,
54
                                path=template.sample_path, _external=True)
55 1
        return data
56
57 1
    elif request.method == 'POST':
58 1
        if top or bottom:
59 1
            text = Text([top, bottom], translate_spaces=False)
60
        else:
61 1
            text = Text("_")
62
63 1
        url = route('image.get', key=key, path=text.path)
64
65 1
        if _redirect:
66 1
            return redirect(url, 303)
67
        else:
68 1
            return dict(href=url)
69
70
    else:  # pragma: no cover
71
        assert None
72
73
74 1
@blueprint.route("<key>/<path:path>")
75
def get_meme_with_path(key, path):
76
    """Redirect if any additional path is provided."""
77
    template = app.template_service.find(key)
78
    return redirect("/{}/{}".format(template.key, path))
79