memegen.routes.api_search._get_matches()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 19
nop 1
dl 0
loc 23
rs 9.45
c 0
b 0
f 0
ccs 0
cts 14
cp 0
crap 20
1 1
from collections import OrderedDict
2
3 1
from flask import Blueprint, current_app as app
0 ignored issues
show
introduced by
Unable to import 'flask'
Loading history...
4
5 1
from ..extensions import cache
6
7
from ._utils import route
8 1
9
10
blueprint = Blueprint('search', __name__, url_prefix="/api/search/")
11 1
12 1
13
@blueprint.route("<query>")
14
@blueprint.route("", defaults={'query': None})
15
@cache.cached()
16
def get(query):
17
    """Get a list of all matching links."""
18 1
    return _get_matches(query)
19
20
21
def _get_matches(query):
22
    items = []
23
24
    for template in app.template_service.all():
25
        count = template.search(query)
26
27
        if not count:
28
            continue
29
30
        data = OrderedDict()
31
        data['count'] = count
32
        data['template'] = OrderedDict()
33
        data['template']['name'] = template.name
34
        data['template']['description'] = template.link
35
        data['template']['keywords'] = sorted(template.keywords)
36
        data['template']['blank'] = route('image.get', key=template.key,
37
                                          path='_', _external=True)
38
        data['template']['example'] = route('image.get', key=template.key,
39
                                            path=template.sample_path,
40
                                            _external=True)
41
        items.append(data)
42
43
    return sorted(items, key=lambda item: item['count'], reverse=True)
44