Completed
Push — master ( eac9d6...cda706 )
by Jace
11s
created

image()   B

Complexity

Conditions 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5
Metric Value
cc 5
dl 0
loc 22
rs 8.3411
ccs 15
cts 15
cp 1
crap 5
1 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...
2
3 1
from ..domain import Text
4
5 1
from ._common import route
6
7
8 1
blueprint = Blueprint('magic', __name__)
9
10
11 1
@blueprint.route("/magic/")
12
def get():
13
    """Get a list of all matching links."""
14 1
    return []
15
16
17 1
@blueprint.route("/magic/<pattern>")
18
def links(pattern):
19
    """Get a list of all matching links."""
20 1
    text = Text(pattern)
21
22 1
    if text.path != pattern:
23 1
        return redirect(route('.links', pattern=text.path))
24
25 1
    return _get_matches(str(text).lower())
26
27
28 1
def _get_matches(pattern):
29 1
    items = []
30
31 1
    for template in app.template_service.all():
32 1
        ratio, path = template.match(pattern)
33 1
        if not ratio:
34 1
            continue
35
36 1
        data = {}
37 1
        data['ratio'] = ratio
38 1
        data['template'] = route('links.get', key=template.key,
39
                                 path=path, _external=True)
40
41 1
        items.append(data)
42
43 1
    return sorted(items, key=lambda item: item['ratio'])
44
45
46 1
@blueprint.route("/m/<pattern>")
47
def links_shortened(pattern):
48
    """Redirect to the full magic route."""
49 1
    return redirect(route('.links', pattern=pattern))
50
51
52 1
@blueprint.route("/magic/<pattern>.jpg")
53
def image(pattern):
54
    """Get the first matching image."""
55 1
    items = []
56
57 1
    for template in app.template_service.all():
58 1
        ratio, path = template.match(pattern)
59 1
        if not ratio:
60 1
            continue
61
62 1
        data = {}
63 1
        data['ratio'] = ratio
64 1
        data['image'] = route('image.get', key=template.key, path=path)
65
66 1
        items.append(data)
67
68 1
    try:
69 1
        url = max(items, key=lambda item: item['ratio'])['image']
70 1
    except ValueError:
71 1
        url = route('image.get', key="unknown", path="_")
72
73
    return redirect(url)
74