| Total Complexity | 5 |
| Total Lines | 44 |
| Duplicated Lines | 0 % |
| Coverage | 30.43% |
| Changes | 0 | ||
| 1 | 1 | from collections import OrderedDict |
|
| 2 | |||
| 3 | 1 | from flask import Blueprint, current_app as app |
|
|
|
|||
| 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 |