Completed
Push — master ( 122b74...8e563c )
by Jace
02:11 queued 26s
created

route()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
cc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1 1
import pprint
2 1
import logging
3 1
from urllib.parse import unquote
4
5 1
import requests
6 1
from flask import (Response, url_for as _url_for, render_template, 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...
7
                   current_app, request)
8
9 1
GITHUB_BASE = "https://raw.githubusercontent.com/jacebrowning/memegen/master/"
10 1
CONTRIBUTING_URL = GITHUB_BASE + "CONTRIBUTING.md"
11 1
CHANGES_URL = GITHUB_BASE + "CHANGES.md"
12
13 1
log = logging.getLogger(__name__)
14
15
16 1
def route(*args, **kwargs):
17
    """Unquoted version of Flask's `url_for`."""
18 1
    return unquote(_url_for(*args, **kwargs))
19
20
21 1
def samples():
22
    """Generate dictionaries of sample image data for template rendering."""
23 1
    for template in sorted(current_app.template_service.all()):
24 1
        path = template.sample_path
25 1
        url = route('image.get', key=template.key, path=path, _external=True)
26 1
        link = route('links.get', key=template.key, path=path)
27 1
        yield {
28
            'key': template.key,
29
            'name': template.name,
30
            'url': url,
31
            'link': link
32
        }
33
34
35 1
def display(title, path, mimetype='image/jpeg'):
36
    """Render a webpage or raw image based on request."""
37 1
    mimetypes = request.headers.get('Accept', "").split(',')
38 1
    browser = 'text/html' in mimetypes
39
40 1
    if browser:
41
        log.info("Rending image on page: %s", request.path)
42
43
        html = render_template(
44
            'image.html',
45
            src=request.path,
46
            title=title,
47
            ga_tid=get_tid(),
48
        )
49
50
        return Response(html)
51
52
    else:
53 1
        log.info("Sending image: %s", path)
54
55 1
        _track(title)
56
57 1
        return send_file(path, mimetype=mimetype)
58
59
60 1
def _track(title):
61
    """Log the requested content, server-side."""
62 1
    data = dict(
63
        v=1,
64
        tid=get_tid(),
65
        cid=request.remote_addr,
66
67
        t='pageview',
68
        dh='memegen.link',
69
        dp=request.path,
70
        dt=str(title),
71
72
        uip=request.remote_addr,
73
        ua=request.user_agent.string,
74
        dr=request.referrer,
75
    )
76 1
    if get_tid(default=None):
77
        requests.post("http://www.google-analytics.com/collect", data=data)
78
    else:
79 1
        log.debug("Analytics data:\n%s", pprint.pformat(data))
80
81
82 1
def get_tid(*, default='local'):
83
    """Get the analtyics tracking identifier."""
84
    return current_app.config['GOOGLE_ANALYTICS_TID'] or default
85