Passed
Push — main ( b77d07...0a4857 )
by Jochen
07:55
created

byceps.blueprints.admin.snippet.mountpoint.views   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Test Coverage

Coverage 83.52%

Importance

Changes 0
Metric Value
eloc 120
dl 0
loc 194
ccs 76
cts 91
cp 0.8352
rs 10
c 0
b 0
f 0
wmc 14

7 Functions

Rating   Name   Duplication   Size   Complexity  
A site_select() 0 22 2
A site_select_form() 0 23 2
A create() 0 27 2
A _get_sites_to_potentially_mount_to() 0 9 3
A delete() 0 18 2
A index() 0 23 1
A create_form() 0 14 2
1
"""
2
byceps.blueprints.admin.snippet.mountpoint.views
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2021 Jochen Kupperschmidt
6
:License: Revised BSD (see `LICENSE` file for details)
7
"""
8
9 1
from __future__ import annotations
10 1
from typing import Optional
11 1
from flask import abort, request
12 1
from flask_babel import gettext
13
14 1
from .....services.brand.transfer.models import Brand
15 1
from .....services.site import service as site_service
16 1
from .....services.site.transfer.models import Site
17 1
from .....services.snippet import mountpoint_service, service as snippet_service
18 1
from .....services.snippet.transfer.models import Scope
19 1
from .....util.authorization import register_permission_enum
20 1
from .....util.framework.blueprint import create_blueprint
21 1
from .....util.framework.flash import flash_success
22 1
from .....util.framework.templating import templated
23 1
from .....util.views import permission_required, redirect_to, respond_no_content
24
25 1
from ..authorization import SnippetPermission
26 1
from ..helpers import (
27
    find_brand_for_scope,
28
    find_site_by_id,
29
    find_site_for_scope,
30
    find_snippet_by_id,
31
)
32
33 1
from .authorization import SnippetMountpointPermission
34 1
from .forms import CreateForm, SiteSelectForm
35
36
37 1
blueprint = create_blueprint('snippet_mountpoint_admin', __name__)
38
39
40 1
register_permission_enum(SnippetMountpointPermission)
41
42
43 1
@blueprint.get('/for_site/<site_id>')
44 1
@permission_required(SnippetPermission.view)
45 1
@templated
46
def index(site_id):
47
    """List mountpoints for that site."""
48 1
    scope = Scope.for_site(site_id)
49
50 1
    mountpoints = mountpoint_service.get_mountpoints_for_site(site_id)
51
52 1
    snippet_ids = {mp.snippet_id for mp in mountpoints}
53 1
    snippets = snippet_service.get_snippets(snippet_ids)
54 1
    snippets_by_snippet_id = {snippet.id: snippet for snippet in snippets}
55
56 1
    mountpoints_and_snippets = [
57
        (mp, snippets_by_snippet_id[mp.snippet_id]) for mp in mountpoints
58
    ]
59
60 1
    site = find_site_for_scope(scope)
61
62 1
    return {
63
        'scope': scope,
64
        'mountpoints_and_snippets': mountpoints_and_snippets,
65
        'site': site,
66
    }
67
68
69 1
@blueprint.get('/for_snippet/<uuid:snippet_id>/select_site')
70 1
@permission_required(SnippetMountpointPermission.create)
71 1
@templated
72 1
def site_select_form(snippet_id, *, erroneous_form=None):
73
    """Show form to select a site to create a mountpoint for."""
74 1
    snippet = find_snippet_by_id(snippet_id)
75
76 1
    scope = snippet.scope
77
78 1
    brand = find_brand_for_scope(scope)
79 1
    site = find_site_for_scope(scope)
80
81 1
    sites = _get_sites_to_potentially_mount_to(brand, site)
82
83 1
    form = erroneous_form if erroneous_form else SiteSelectForm()
84 1
    form.set_site_id_choices(sites)
85
86 1
    return {
87
        'scope': scope,
88
        'snippet': snippet,
89
        'form': form,
90
        'brand': brand,
91
        'site': site,
92
    }
93
94
95 1
@blueprint.post('/for_snippet/<uuid:snippet_id>/select_site')
96 1
@permission_required(SnippetMountpointPermission.create)
97
def site_select(snippet_id):
98
    """Redirect to form to create a mountpoint."""
99
    snippet = find_snippet_by_id(snippet_id)
100
101
    scope = snippet.scope
102
103
    brand = find_brand_for_scope(scope)
104
    site = find_site_for_scope(scope)
105
106
    sites = _get_sites_to_potentially_mount_to(brand, site)
107
108
    form = SiteSelectForm(request.form)
109
    form.set_site_id_choices(sites)
110
111
    if not form.validate():
112
        return site_select_form(snippet.id, erroneous_form=form)
113
114
    site_id = form.site_id.data
115
116
    return redirect_to('.create_form', snippet_id=snippet.id, site_id=site_id)
117
118
119 1
def _get_sites_to_potentially_mount_to(
120
    brand: Optional[Brand] = None, site: Optional[Site] = None
121
) -> set[Site]:
122 1
    if site is not None:
123
        return {site}
124 1
    elif brand is not None:
125
        return site_service.get_sites_for_brand(brand.id)
126
    else:
127 1
        return site_service.get_all_sites()
128
129
130 1
@blueprint.get('/for_snippet/<uuid:snippet_id>/for_site/<site_id>/create')
131 1
@permission_required(SnippetMountpointPermission.create)
132 1
@templated
133 1
def create_form(snippet_id, site_id, *, erroneous_form=None):
134
    """Show form to create a mountpoint."""
135 1
    snippet = find_snippet_by_id(snippet_id)
136 1
    site = find_site_by_id(site_id)
137
138 1
    form = erroneous_form if erroneous_form else CreateForm()
139
140 1
    return {
141
        'snippet': snippet,
142
        'form': form,
143
        'site': site,
144
    }
145
146
147 1
@blueprint.post('/for_snippet/<uuid:snippet_id>/for_site/<site_id>')
148 1
@permission_required(SnippetMountpointPermission.create)
149
def create(snippet_id, site_id):
150
    """Create a mountpoint."""
151 1
    snippet = find_snippet_by_id(snippet_id)
152 1
    site = find_site_by_id(site_id)
153
154 1
    form = CreateForm(request.form)
155
156 1
    if not form.validate():
157
        return create_form(snippet.id, site.id, erroneous_form=form)
158
159 1
    endpoint_suffix = form.endpoint_suffix.data.strip()
160 1
    url_path = form.url_path.data.strip()
161
162 1
    mountpoint = mountpoint_service.create_mountpoint(
163
        site.id, endpoint_suffix, url_path, snippet.id
164
    )
165
166 1
    flash_success(
167
        gettext(
168
            'Mountpoint for "%(url_path)s" has been created.',
169
            url_path=mountpoint.url_path,
170
        )
171
    )
172
173 1
    return redirect_to('.index', site_id=site.id)
174
175
176 1
@blueprint.delete('/mountpoints/<uuid:mountpoint_id>')
177 1
@permission_required(SnippetMountpointPermission.delete)
178 1
@respond_no_content
179
def delete(mountpoint_id):
180
    """Delete a mountpoint."""
181 1
    mountpoint = mountpoint_service.find_mountpoint(mountpoint_id)
182
183 1
    if mountpoint is None:
184
        abort(404)
185
186 1
    url_path = mountpoint.url_path
187
188 1
    mountpoint_service.delete_mountpoint(mountpoint.id)
189
190 1
    flash_success(
191
        gettext(
192
            'Mountpoint for "%(url_path)s" has been deleted.',
193
            url_path=url_path,
194
        )
195
    )
196