Completed
Pull Request — master (#200)
by Jace
03:25
created

create_meme()   B

Complexity

Conditions 5

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
dl 0
loc 24
ccs 16
cts 16
cp 1
crap 5
rs 8.1671
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 ._common import CONTRIBUTING_URL, route
8 1
from ._parser import parser
9
10
11 1
blueprint = Blueprint('templates', __name__, url_prefix="/templates/")
12
13 1
OPTIONS = {
14
    # pylint: disable=no-member
15
    'top': fields.Str(missing="_"),
16
    'bottom': fields.Str(missing="_"),
17
}
18
19
20 1
@blueprint.route("")
21
def get():
22
    """Get a list of all meme templates."""
23 1
    data = OrderedDict()
24 1
    for template in sorted(app.template_service.all()):
25 1
        url = route('.create', key=template.key, _external=True)
26 1
        data[template.name] = url
27 1
    return data
28
29
30 1
@blueprint.route("", methods=['POST'])
31
def create_template():
32
    # TODO: https://github.com/jacebrowning/memegen/issues/119
33 1
    raise exceptions.PermissionDenied(CONTRIBUTING_URL)
34
35
36 1
@blueprint.route("<key>", methods=['GET', 'POST'], endpoint='create')
37 1
@parser.use_kwargs(OPTIONS)
38
def create_meme(key, top, bottom):
39
    """Generate a meme from a template."""
40 1
    if request.method == 'GET':
41 1
        template = app.template_service.find(key)
42 1
        if template.key != key:
43 1
            return redirect(route('.create', key=template.key))
44
45 1
        data = OrderedDict()
46 1
        data['name'] = template.name
47 1
        data['description'] = template.link
48 1
        data['aliases'] = sorted(template.aliases + [template.key])
49 1
        data['styles'] = template.styles
50 1
        data['example'] = route('links.get', key=key,
51
                                path=template.sample_path, _external=True)
52 1
        return data
53
54 1
    elif request.method == 'POST':
55 1
        path = "/".join([top, bottom])
56 1
        return redirect(route('image.get', key=key, path=path), 303)
57
58
    else:  # pragma: no cover
59
        assert None
60
61
62 1
@blueprint.route("<key>/<path:path>")
63
def get_meme_with_path(key, path):
64
    """Redirect if any additional path is provided."""
65 1
    template = app.template_service.find(key)
66
    return redirect("/{}/{}".format(template.key, path))
67