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

byceps.blueprints.admin.snippet.mountpoint.forms   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 38
ccs 15
cts 16
cp 0.9375
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A SiteSelectForm.set_site_id_choices() 0 4 2
A CreateForm.validate_url_path() 0 5 2
1
"""
2
byceps.blueprints.admin.snippet.mountpoint.forms
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
11 1
from flask_babel import lazy_gettext
12 1
from wtforms import SelectField, StringField
13 1
from wtforms.validators import InputRequired, ValidationError
14
15 1
from .....services.site.transfer.models import Site
16 1
from .....util.l10n import LocalizedForm
17
18
19 1
class SiteSelectForm(LocalizedForm):
20 1
    site_id = SelectField(lazy_gettext('Site'), [InputRequired()])
21
22 1
    def set_site_id_choices(self, sites: set[Site]) -> None:
23 1
        self.site_id.choices = [
24
            (site.id, site.title)
25
            for site in sorted(sites, key=lambda site: site.title)
26
        ]
27
28
29 1
class CreateForm(LocalizedForm):
30 1
    endpoint_suffix = StringField(lazy_gettext('Identifier'), [InputRequired()])
31 1
    url_path = StringField(lazy_gettext('URL path'), [InputRequired()])
32
33 1
    @staticmethod
34
    def validate_url_path(form, field):
35 1
        if not field.data.startswith('/'):
36
            raise ValidationError(
37
                lazy_gettext('URL path has to start with a slash.')
38
            )
39