/
forms.py
145 lines (109 loc) · 4.88 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# Copyright (C) 2014-2016 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Author: Michael Simacek <msimacek@redhat.com>
# Author: Mikolaj Izdebski <mizdebsk@redhat.com>
from __future__ import print_function, absolute_import
import re
from flask_wtf import Form
from wtforms import (
StringField, TextAreaField, IntegerField, BooleanField,
)
from wtforms.validators import Regexp, ValidationError
from wtforms.widgets import HTMLString, HiddenInput
from koschei.config import get_koji_config
from koschei.frontend.util import flash_nak
class CheckBoxField(BooleanField):
"""
Check box field that contains an additional hidden field that esures that
the value is not set to False when the checkbox was not present at all
"""
# pylint: disable=arguments-differ,attribute-defined-outside-init
def process(self, formdata, *args, **kwargs):
super(CheckBoxField, self).process(formdata, *args, **kwargs)
if formdata and not formdata.get(self.name + '__present', None):
self.data = None
def __call__(self, **kwargs):
marker = '<input type="hidden" name="{name}__present" value="1"/>'\
.format(name=self.name)
return HTMLString(self.meta.render_field(self, kwargs) + marker)
class StrippedStringField(StringField):
# pylint:disable=arguments-differ
def process_formdata(self, values):
# pylint:disable=W0201
self.data = values and values[0].strip()
class ListFieldMixin(object):
split_re = re.compile(r'[ \t\n\r,]+')
def process_formdata(self, values):
# pylint:disable=W0201
values = values and values[0]
self.data = [x for x in self.split_re.split(values or '') if x]
class ListField(ListFieldMixin, StringField):
def _value(self):
return ', '.join(self.data or ())
class ListAreaField(ListFieldMixin, TextAreaField):
def _value(self):
return '\n'.join(self.data or ())
name_re = re.compile(r'^[a-zA-Z0-9.+_-]+$')
group_re = re.compile(r'^([a-zA-Z0-9.+_-]+(/[a-zA-Z0-9.+_-]+)?)?$')
class NameListValidator(object):
def __init__(self, message):
self.message = message
def __call__(self, _, field):
if not all(map(name_re.match, field.data)):
raise ValidationError(self.message)
arch_override_re = re.compile(r'\^?(.*)')
class ArchOverrideValidator(object):
def __call__(self, _, field):
allowed = get_koji_config('primary', 'build_arches')
for arch in field.data:
match = arch_override_re.match(arch)
if not match or match.group(1) not in allowed:
raise ValidationError("Unrecognized arch in arch_override")
class NonEmptyList(object):
def __init__(self, message):
self.message = message
def __call__(self, _, field):
if not field.data:
raise ValidationError(self.message)
class EmptyForm(Form):
def validate_or_flash(self):
if self.validate_on_submit():
return True
flash_nak("Validation errors: " +
', '.join(x for i in self.errors.values() for x in i))
return False
class GroupForm(EmptyForm):
name = StrippedStringField('name', [Regexp(name_re, message="Invalid group name")])
packages = ListAreaField('packages', [NonEmptyList("Empty group not allowed"),
NameListValidator("Invalid package list")])
owners = ListField('owners', [NonEmptyList("Group must have an owner"),
NameListValidator("Invalid owner list")])
class AddPackagesForm(EmptyForm):
packages = ListAreaField('packages', [NonEmptyList("No packages given"),
NameListValidator("Invalid package list")])
collection = StrippedStringField('collection')
group = StrippedStringField('group', [Regexp(group_re, message="Invalid group")])
class EditPackageForm(EmptyForm):
tracked = CheckBoxField('tracked')
collection_id = IntegerField(
'collection_id',
widget=HiddenInput(),
)
manual_priority = IntegerField('manual_priority')
arch_override = ListField('arch_override', [ArchOverrideValidator()])
skip_resolution = CheckBoxField('skip_resolution')
# groups checkboxes are processed manually