Completed
Pull Request — master (#135)
by Jace
09:44
created

get_with_text()   B

Complexity

Conditions 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5
Metric Value
cc 5
dl 0
loc 20
ccs 14
cts 14
cp 1
crap 5
rs 8.5454
1 1
from flask import Blueprint, redirect, send_file
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...
2 1
from flask import current_app as app, request
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...
3 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...
4 1
import requests
5
6 1
from .. import domain
7
8 1
from ._common import url_for
9
10 1
blueprint = Blueprint('image', __name__, url_prefix="/")
11
12 1
OPTIONS = {
13
    'alt': fields.Str(missing=None)  # pylint: disable=no-member
14
}
15
16
17 1
@blueprint.route("latest.jpg")
18
def get_latest():
19 1
    path = app.image_service.image_store.latest
20 1
    try:
21 1
        return send_file(path, mimetype='image/jpeg')
22 1
    except FileNotFoundError:
23 1
        return send_file("static/images/missing.png", mimetype='image/png')
24
25
26 1
@blueprint.route("<key>.jpg")
27 1
@flaskparser.use_kwargs(OPTIONS)
28
def get_without_text(key, alt):
29 1
    template = app.template_service.find(key)
30 1
    text = domain.Text(template.default_path)
31 1
    return redirect(url_for('.get', key=key, path=text.path, alt=alt))
32
33
34 1
@blueprint.route("<key>.jpeg")
35
def get_without_text_jpeg(key):
36 1
    return redirect(url_for('.get_without_text', key=key))
37
38
39 1
@blueprint.route("<key>/<path:path>.jpg", endpoint='get')
40 1
@flaskparser.use_kwargs(OPTIONS)
41
def get_with_text(key, path, alt):
42 1
    text = domain.Text(path)
43 1
    track_request(text)
44
45 1
    template = app.template_service.find(key, allow_missing=True)
46 1
    if template.key != key:
47 1
        return redirect(url_for('.get', key=template.key, path=path, alt=alt))
48
49 1
    if alt and template.path == template.get_path(alt):
50 1
        return redirect(url_for('.get', key=key, path=path))
51
52 1
    if path != text.path:
53 1
        return redirect(url_for('.get', key=key, path=text.path, alt=alt))
54
55 1
    image = app.image_service.create_image(template, text, style=alt)
56
57 1
    track_request(text)
58 1
    return send_file(image.path, mimetype='image/jpeg')
59
60
61 1
@blueprint.route("<key>/<path:path>.jpeg")
62
def get_with_text_jpeg(key, path):
63 1
    return redirect(url_for('.get', key=key, path=path))
64
65
66 1
@blueprint.route("_<code>.jpg")
67
def get_encoded(code):
68 1
    track_request(code)
69
70 1
    key, path = app.link_service.decode(code)
71 1
    template = app.template_service.find(key)
72 1
    text = domain.Text(path)
73 1
    image = app.image_service.create_image(template, text)
74
75 1
    track_request(text)
76 1
    return send_file(image.path, mimetype='image/jpeg')
77
78
79 1
def track_request(title):
80 1
    data = dict(
81
        v=1,
82
        tid=app.config['GOOGLE_ANALYTICS_TID'],
83
        cid=request.remote_addr,
84
85
        t='pageview',
86
        dh='memegen.link',
87
        dp=request.path,
88
        dt=str(title),
89
90
        uip=request.remote_addr,
91
        ua=request.user_agent.string,
92
        dr=request.referrer,
93
    )
94
    if not app.config['TESTING']:  # pragma: no cover (manual)
95
        requests.post("http://www.google-analytics.com/collect", data=data)
96