Completed
Push — master ( 3f9dd1...d1c2b3 )
by Jace
01:51
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.5455
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 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 OPTIONS, url_for
9
10 1
blueprint = Blueprint('image', __name__, url_prefix="/")
11
12
13 1
@blueprint.route("latest.jpg")
14
def get_latest():
15 1
    path = app.image_service.image_store.latest
16 1
    try:
17 1
        return send_file(path, mimetype='image/jpeg')
18 1
    except FileNotFoundError:
19 1
        return send_file("static/images/apple-touch-icon.png",
20
                         mimetype='image/png')
21
22
23 1
@blueprint.route("<key>.jpg")
24 1
@flaskparser.use_kwargs(OPTIONS)
25
def get_without_text(key, alt):
26 1
    template = app.template_service.find(key)
27 1
    text = domain.Text(template.default_path)
28 1
    return redirect(url_for('.get', key=key, path=text.path, alt=alt))
29
30
31 1
@blueprint.route("<key>.jpeg")
32
def get_without_text_jpeg(key):
33 1
    return redirect(url_for('.get_without_text', key=key))
34
35
36 1
@blueprint.route("<key>/<path:path>.jpg", endpoint='get')
37 1
@flaskparser.use_kwargs(OPTIONS)
38
def get_with_text(key, path, alt):
39 1
    text = domain.Text(path)
40 1
    track_request(text)
41
42 1
    template = app.template_service.find(key)
43 1
    if template.key != key:
44 1
        return redirect(url_for('.get', key=template.key, path=path, alt=alt))
45
46 1
    if alt and template.path == template.get_path(alt):
47 1
        return redirect(url_for('.get', key=key, path=path))
48
49 1
    if path != text.path:
50 1
        return redirect(url_for('.get', key=key, path=text.path, alt=alt))
51
52 1
    image = app.image_service.create_image(template, text, style=alt)
53
54 1
    track_request(text)
55 1
    return send_file(image.path, mimetype='image/jpeg')
56
57
58 1
@blueprint.route("<key>/<path:path>.jpeg")
59
def get_with_text_jpeg(key, path):
60 1
    return redirect(url_for('.get', key=key, path=path))
61
62
63 1
@blueprint.route("_<code>.jpg")
64
def get_encoded(code):
65 1
    track_request(code)
66
67 1
    key, path = app.link_service.decode(code)
68 1
    template = app.template_service.find(key)
69 1
    text = domain.Text(path)
70 1
    image = app.image_service.create_image(template, text)
71
72 1
    track_request(text)
73 1
    return send_file(image.path, mimetype='image/jpeg')
74
75
76 1
def track_request(title):
77 1
    data = dict(
78
        v=1,
79
        tid=app.config['GOOGLE_ANALYTICS_TID'],
80
        cid=request.remote_addr,
81
82
        t='pageview',
83
        dh='memegen.link',
84
        dp=request.path,
85
        dt=str(title),
86
87
        uip=request.remote_addr,
88
        ua=request.user_agent.string,
89
        dr=request.referrer,
90
    )
91
    if not app.config['TESTING']:  # pragma: no cover (manual)
92
        requests.post("http://www.google-analytics.com/collect", data=data)
93