Completed
Push — master ( 0264a9...69cfd2 )
by Jace
19s
created

create_meme()   F

Complexity

Conditions 9

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 9.0468

Importance

Changes 0
Metric Value
cc 9
dl 0
loc 37
ccs 22
cts 24
cp 0.9167
crap 9.0468
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
23 1
@blueprint.route("")
24
def get():
25
    """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 1
    return data
31
32
33 1
@blueprint.route("", methods=['POST'])
34
def create_template():
35 1
    raise exceptions.PermissionDenied(current_app.config['CONTRIBUTING_URL'])
36
37
38 1
@blueprint.route("<key>", methods=['GET', 'POST'], endpoint='create')
39 1
@parser.use_kwargs(OPTIONS)
40
def create_meme(key, top, bottom, _redirect, _masked):
41
    """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 1
            return redirect(route('.create', key=template.key))
46
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 1
        data['example'] = route('links.get', key=key,
53
                                path=template.sample_path, _external=True)
54 1
        return data
55
56 1
    elif request.method == 'POST':
57 1
        if top or bottom:
58 1
            text = Text([top, bottom], translate_spaces=False)
59
        else:
60 1
            text = Text("_")
61
62 1
        if _masked:
63
            code = current_app.link_service.encode(key, text.path)
64
            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 1
        if _redirect:
69 1
            return redirect(url, 303)
70
        else:
71 1
            return dict(href=url)
72
73
    else:  # pragma: no cover
74
        assert None
75
76
77 1
@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