Completed
Pull Request — master (#290)
by Jace
03:06
created

_get_matches()   B

Complexity

Conditions 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 17.1835

Importance

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