Completed
Pull Request — master (#132)
by Jace
01:42
created

create_meme()   B

Complexity

Conditions 5

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.073
Metric Value
cc 5
dl 0
loc 23
ccs 12
cts 14
cp 0.8571
crap 5.073
rs 8.2508
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
6 1
from ._common import CONTRIBUTING, url_for
7
8
9 1
blueprint = Blueprint('templates', __name__, url_prefix="/templates/")
10
11
12 1
@blueprint.route("")
13
def get():
14
    """Get a list of all meme templates."""
15 1
    data = OrderedDict()
16 1
    for template in sorted(app.template_service.all()):
17 1
        url = url_for('.create', key=template.key, _external=True)
18 1
        data[template.name] = url
19 1
    return data
20
21
22 1
@blueprint.route("", methods=['POST'])
23
def create_template():
24 1
    raise exceptions.PermissionDenied(CONTRIBUTING)
25
26
27 1
@blueprint.route("<key>", methods=['GET', 'POST'], endpoint='create')
28
def create_meme(key):
29
    """Generate a meme from a template."""
30 1
    if request.method == 'GET':
31 1
        template = app.template_service.find(key)
32 1
        if template.key != key:
33 1
            return redirect(url_for('.create', key=template.key))
34
35 1
        data = OrderedDict()
36 1
        data['name'] = template.name
37 1
        data['description'] = template.link
38 1
        data['aliases'] = sorted(template.aliases + [template.key])
39 1
        data['styles'] = template.styles
40 1
        data['example'] = url_for('links.get', key=key,
41
                                  path=template.sample_path, _external=True)
42 1
        return data
43
44
    elif request.method == 'POST':
45
        # TODO: https://github.com/jacebrowning/memegen/issues/12
46
        raise exceptions.PermissionDenied("Feature not implemented.")
47
48
    else:  # pragma: no cover
49
        assert None
50
51
52 1
@blueprint.route("<key>/<path:path>")
53
def get_meme_with_path(key, path):
54
    """Redirect if any additional path is provided."""
55 1
    template = app.template_service.find(key)
56
    return redirect("/{}/{}".format(template.key, path))
57