Completed
Push — master ( a0d752...d0190b )
by Jace
02:04
created

get_with_text()   B

Complexity

Conditions 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

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