Completed
Pull Request — master (#161)
by Jace
02:42
created

links_shortened()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

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