|
1
|
1 |
|
from flask import Blueprint, current_app as app, redirect |
|
|
|
|
|
|
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
|
|
|
|
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.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.