Completed
Push — master ( cd0c61...ee130d )
by Jochen
05:47
created

byceps.blueprints.api.snippet.views._get_content()   A

Complexity

Conditions 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nop 1
dl 0
loc 14
rs 9.95
c 0
b 0
f 0
1
"""
2
byceps.blueprints.api.snippet.views
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2019 Jochen Kupperschmidt
6
:License: Modified BSD, see LICENSE for details.
7
"""
8
9
from flask import jsonify
10
11
from ....services.snippet import service as snippet_service
12
from ....services.snippet.transfer.models import Scope
13
from ....util.framework.blueprint import create_blueprint
14
from ....util.views import create_empty_json_response
15
16
from ...snippet.templating import get_snippet_context
17
18
19
blueprint = create_blueprint('api_snippet', __name__)
20
21
22
@blueprint.route('/by_name/<scope_type>/<scope_name>/<snippet_name>')
23
def view_by_name(scope_type, scope_name, snippet_name):
24
    """Return the current version of the snippet with that name in that
25
    scope.
26
    """
27
    scope = Scope(scope_type, scope_name)
28
    version = snippet_service.find_current_version_of_snippet_with_name(
29
        scope, snippet_name
30
    )
31
    if version is None:
32
        return create_empty_json_response(404)
33
34
    content = _get_content(version)
35
36
    return jsonify({
37
        'type': version.snippet.type_.name,
38
        'version': version.id,
39
        'content': content,
40
    })
41
42
43
def _get_content(version):
44
    context = get_snippet_context(version)
45
46
    content = {
47
        'body': context['body'],
48
    }
49
50
    if version.snippet.is_document:
51
        content.update({
52
            'title': context['title'],
53
            'head': context['head'],
54
        })
55
56
    return content
57