Newer
Older
Rajmund Hruška
committed
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# -------------------------------------------------------------------------------
# This file is part of Mentat system (https://mentat.cesnet.cz/).
#
# Copyright (C) since 2011 CESNET, z.s.p.o (http://www.ces.net/)
# Use of this source is governed by the MIT license, see LICENSE file.
# -------------------------------------------------------------------------------
"""
This module contains custom detector management forms for Hawat.
"""
__author__ = "Rajmund Hruška <rajmund.hruska@cesnet.cz>"
__credits__ = "Jan Mach <jan.mach@cesnet.cz>, Pavel Kácha <pavel.kacha@cesnet.cz>, Andrea Kropáčová <andrea.kropacova@cesnet.cz>"
import wtforms
#
# Flask related modules.
#
from flask_babel import lazy_gettext, gettext
#
# Custom modules.
#
import hawat.const
import hawat.forms
import hawat.db
from mentat.datatype.sqldb import DetectorModel
def get_available_sources():
"""
Query the database for list of network record sources.
"""
result = hawat.db.db_query(DetectorModel) \
.distinct(DetectorModel.source) \
.order_by(DetectorModel.source) \
.all()
return [x.source for x in result]
def check_name_uniqueness(form, field):
"""
Callback for validating names during detector update action.
"""
item = hawat.db.db_get().session.query(DetectorModel). \
filter(DetectorModel.name == field.data). \
filter(DetectorModel.id != form.db_item_id). \
all()
if not item:
return
raise wtforms.validators.ValidationError(gettext('Detector with this name already exists.'))
class BaseDetectorForm(hawat.forms.BaseItemForm):
"""
Class representing base detector record form.
"""
source = wtforms.HiddenField(
default='manual',
validators=[
wtforms.validators.DataRequired(),
wtforms.validators.Length(min=3, max=50)
]
)
credibility = wtforms.FloatField(
lazy_gettext('Credibility:'),
validators=[
wtforms.validators.Optional(),
wtforms.validators.NumberRange(min=0, max=1)
]
)
description = wtforms.TextAreaField(
lazy_gettext('Description:')
)
submit = wtforms.SubmitField(
lazy_gettext('Submit')
)
cancel = wtforms.SubmitField(
lazy_gettext('Cancel')
)
class AdminCreateDetectorForm(BaseDetectorForm):
"""
Class representing detector record create form.
"""
name = wtforms.StringField(
lazy_gettext('Name:'),
validators=[
wtforms.validators.DataRequired(),
wtforms.validators.Length(min=3, max=250),
hawat.forms.check_null_character,
check_name_uniqueness
]
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.db_item_id = None
class AdminUpdateDetectorForm(BaseDetectorForm):
"""
Class representing detector record create form.
"""
name = wtforms.StringField(
lazy_gettext('Name:'),
validators=[
wtforms.validators.DataRequired(),
wtforms.validators.Length(min=3, max=250),
hawat.forms.check_null_character,
check_name_uniqueness
]
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Store the ID of original item in database to enable the ID uniqueness
# check with check_name_uniqueness() validator.
self.db_item_id = kwargs['db_item_id']
class DetectorSearchForm(hawat.forms.BaseSearchForm):
"""
Class representing simple user search form.
"""
search = wtforms.StringField(
lazy_gettext('Name, description:'),
validators=[
wtforms.validators.Optional(),
wtforms.validators.Length(min=3, max=100),
hawat.forms.check_null_character
],
description=lazy_gettext(
'Detector`s name or description. Search is performed even in the middle of the strings.')
)
dt_from = hawat.forms.SmartDateTimeField(
lazy_gettext('Creation time from:'),
validators=[
wtforms.validators.Optional()
],
description=lazy_gettext(
'Lower time boundary for item creation time. Timestamp is expected to be in the format <code>YYYY-MM-DD hh:mm:ss</code> and in the timezone according to the user`s preferences.')
)
dt_to = hawat.forms.SmartDateTimeField(
lazy_gettext('Creation time to:'),
validators=[
wtforms.validators.Optional()
],
description=lazy_gettext(
'Upper time boundary for item creation time. Timestamp is expected to be in the format <code>YYYY-MM-DD hh:mm:ss</code> and in the timezone according to the user`s preferences.')
)
source = wtforms.SelectField(
lazy_gettext('Record source:'),
validators=[
wtforms.validators.Optional()
],
default=''
)
sortby = wtforms.SelectField(
lazy_gettext('Sort by:'),
validators=[
wtforms.validators.Optional()
],
choices=[
('createtime.desc', lazy_gettext('by creation time descending')),
('createtime.asc', lazy_gettext('by creation time ascending')),
('name.desc', lazy_gettext('by name descending')),
('name.asc', lazy_gettext('by name ascending')),
('hits.desc', lazy_gettext('by number of hits descending')),
('hits.asc', lazy_gettext('by number of hits ascending')),
Rajmund Hruška
committed
('credibility.asc', lazy_gettext('by credibility ascending')),
('credibility.desc', lazy_gettext('by credibility descending'))
Rajmund Hruška
committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
],
default='name.asc'
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
#
# Handle additional custom keywords.
#
# The list of choices for 'roles' attribute comes from outside of the
# form to provide as loose tie as possible to the outer application.
# Another approach would be to load available choices here with:
#
# roles = flask.current_app.config['ROLES']
#
# That would mean direct dependency on flask.Flask application.
source_list = get_available_sources()
self.source.choices = [('', lazy_gettext('Nothing selected'))] + list(zip(source_list, source_list))
@staticmethod
def is_multivalue(field_name):
"""
Check, if given form field is a multivalue field.
:param str field_name: Name of the form field.
:return: ``True``, if the field can contain multiple values, ``False`` otherwise.
:rtype: bool
"""
return False