Completed
Pull Request — master (#175)
by Josh
02:09
created

get_with_text()   B

Complexity

Conditions 5

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5
Metric Value
cc 5
dl 0
loc 18
ccs 12
cts 12
cp 1
crap 5
rs 8.5454
1 1
import logging
2
3 1
from flask import Blueprint, current_app as app, 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 webargs import fields, flaskparser
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...
5
6 1
from .. import domain
7
8 1
from ._common import route, display
9
10 1
blueprint = Blueprint('image', __name__, url_prefix="/")
11 1
log = logging.getLogger(__name__)
12
13 1
OPTIONS = {
14
    'alt': fields.Str(missing=None)  # pylint: disable=no-member
15
}
16
17
18 1
@blueprint.route("latest.jpg")
19
def get_latest():
20 1
    title = "Latest Meme"
21 1
    try:
22 1
        return display(title, app.image_service.latest)
23 1
    except FileNotFoundError:
24 1
        return display(title, "static/images/missing.png", mimetype='image/png')
25
26
27 1
@blueprint.route("<key>.jpg")
28 1
@flaskparser.use_kwargs(OPTIONS)
29
def get_without_text(key, alt):
30 1
    template = app.template_service.find(key)
31 1
    text = domain.Text(template.default_path)
32 1
    return redirect(route('.get', key=key, path=text.path, alt=alt))
33
34
35 1
@blueprint.route("<key>.jpeg")
36
def get_without_text_jpeg(key):
37 1
    return redirect(route('.get_without_text', key=key))
38
39
40 1
@blueprint.route("<key>/<path:path>.jpg", endpoint='get')
41 1
@flaskparser.use_kwargs(OPTIONS)
42
def get_with_text(key, path, alt):
43 1
    text = domain.Text(path)
44
45 1
    template = app.template_service.find(key, allow_missing=True)
46 1
    if template.key != key:
47 1
        return redirect(route('.get', key=template.key, path=path, alt=alt))
48
49 1
    if alt and template.path == template.get_path(alt):
50 1
        return redirect(route('.get', key=key, path=path))
51
52 1
    if path != text.path:
53 1
        return redirect(route('.get', key=key, path=text.path, alt=alt))
54
55 1
    image = app.image_service.create(template, text, style=alt)
56
57 1
    return display(image.text, image.path)
58
59
60 1
@blueprint.route("<key>/<path:path>.jpeg")
61
def get_with_text_jpeg(key, path):
62 1
    return redirect(route('.get', key=key, path=path))
63
64
65 1
@blueprint.route("_<code>.jpg")
66
def get_encoded(code):
67
68 1
    key, path = app.link_service.decode(code)
69 1
    template = app.template_service.find(key)
70 1
    text = domain.Text(path)
71 1
    image = app.image_service.create(template, text)
72
73
    return display(image.text, image.path)
74