memegen.routes.api_search   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 5

2 Functions

Rating   Name   Duplication   Size   Complexity  
A _get_matches() 0 23 4
A get() 0 6 1
1
from collections import OrderedDict
2
3
from flask import Blueprint, current_app as app
4
5
from ..extensions import cache
6
7
from ._utils import route
8
9
10
blueprint = Blueprint('search', __name__, url_prefix="/api/search/")
11
12
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
    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