2024-04-12 19:22:00 +00:00
|
|
|
# builtins
|
2024-04-23 09:01:18 +00:00
|
|
|
import re
|
2024-04-17 17:30:52 +00:00
|
|
|
import time
|
|
|
|
import copy
|
2024-07-27 01:05:56 +00:00
|
|
|
import datetime
|
2024-04-12 19:22:00 +00:00
|
|
|
import functools
|
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
# third-party
|
2024-07-27 01:05:56 +00:00
|
|
|
import click
|
2024-07-31 02:11:42 +00:00
|
|
|
import click.types as types
|
2024-04-12 19:22:00 +00:00
|
|
|
import flask
|
2024-04-14 13:56:36 +00:00
|
|
|
import peewee
|
2024-04-12 19:22:00 +00:00
|
|
|
|
|
|
|
# internals
|
|
|
|
from application import app
|
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
import util
|
2024-04-12 19:22:00 +00:00
|
|
|
import exceptions
|
2024-04-12 19:22:00 +00:00
|
|
|
import rendering
|
2024-06-17 22:32:06 +00:00
|
|
|
import markdown
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
# instantiate types that don't have default instances
|
|
|
|
types.DATETIME = types.DateTime([
|
2024-07-27 01:05:56 +00:00
|
|
|
'%Y-%m-%d',
|
|
|
|
'%Y-%m-%d %H:%M',
|
|
|
|
'%Y-%m-%d %H:%M:%S',
|
|
|
|
'%Y-%m-%d %H:%M:%S.%f',
|
|
|
|
'%Y-%m-%dT%H:%M',
|
|
|
|
'%Y-%m-%dT%H:%M:%S',
|
|
|
|
'%Y-%m-%dT%H:%M:%S.%f'])
|
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
class DateParamType(types.DateTime):
|
2024-07-27 01:05:56 +00:00
|
|
|
|
|
|
|
def convert(self, value, param, ctx):
|
|
|
|
|
|
|
|
v = super().convert(value, param, ctx)
|
|
|
|
|
|
|
|
if isinstance(v, datetime.datetime):
|
|
|
|
v = v.date()
|
|
|
|
|
|
|
|
return v
|
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
types.DATE = DateParamType(['%Y-%m-%d'])
|
|
|
|
|
|
|
|
class TimeParamType(types.ParamType):
|
|
|
|
|
|
|
|
def convert(self, value, param, ctx):
|
|
|
|
|
|
|
|
components = value.split(':')
|
|
|
|
|
|
|
|
try:
|
|
|
|
components = [int(component) for component in components]
|
|
|
|
except ValueError:
|
|
|
|
self.fail(f"Non-integer component in time input: '{value}'", param, ctx)
|
|
|
|
|
|
|
|
hour, minute, second = [None, None, None]
|
|
|
|
|
|
|
|
if len(components) == 3:
|
|
|
|
hour, minute, second = components
|
|
|
|
elif len(components) == 2:
|
|
|
|
hour, minute = components
|
|
|
|
else:
|
|
|
|
self.fail(f"Invalid time input: '{value}'", param, ctx)
|
|
|
|
|
|
|
|
if hour < 0 or hour > 24:
|
|
|
|
self.fail(f"Hour out of range for time input: {hour}, must be between 0 and 24", param, ctx)
|
|
|
|
|
|
|
|
if hour == 24 and minute > 0:
|
|
|
|
self.fail(f"Time input past 24:00: '{value}'", param, ctx)
|
|
|
|
|
|
|
|
if minute < 0 or minute > 59:
|
|
|
|
self.fail(f"Minute out of range for time input: {minute}", param, ctx)
|
|
|
|
|
|
|
|
if second is not None and ( second < 0 or second > 59 ):
|
|
|
|
self.fail(f"Second out of range for time input: {second}", param, ctx)
|
|
|
|
|
|
|
|
return datetime.time(*components)
|
|
|
|
|
|
|
|
types.TIME = TimeParamType()
|
|
|
|
|
|
|
|
class ColorParamType(types.ParamType):
|
|
|
|
|
|
|
|
def convert(self, value, param, ctx):
|
|
|
|
|
|
|
|
try:
|
|
|
|
return util.Color.from_hex(value)
|
|
|
|
|
|
|
|
except ValueError:
|
|
|
|
self.fail("Invalid value for color.", param, ctx)
|
|
|
|
|
|
|
|
types.COLOR = ColorParamType()
|
2024-07-27 01:05:56 +00:00
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
class ValidationError(exceptions.ExposedException):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def validate_range(min, max, value):
|
|
|
|
|
2024-07-27 01:05:56 +00:00
|
|
|
if min is not None and value < min:
|
2024-04-12 19:22:00 +00:00
|
|
|
raise ValidationError(f"Value below minimum ({min}).")
|
|
|
|
|
2024-07-27 01:05:56 +00:00
|
|
|
if max is not None and value > max:
|
2024-04-12 19:22:00 +00:00
|
|
|
raise ValidationError(f"Value above maximum ({max}).")
|
|
|
|
|
2024-04-23 09:01:18 +00:00
|
|
|
def validate_regexp(pattern, value):
|
|
|
|
|
2024-10-14 14:54:17 +00:00
|
|
|
if value is None or not re.match(pattern, value):
|
2024-04-23 09:01:18 +00:00
|
|
|
raise ValidationError(f"Value does not match '{pattern}'.")
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-05-05 20:10:41 +00:00
|
|
|
class MultiGroup:
|
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
"""
|
|
|
|
A form element group with multiple values (i.e. for checkboxes).
|
|
|
|
"""
|
|
|
|
|
2024-05-05 20:10:41 +00:00
|
|
|
def __init__(self, type, name, label=None, values=None, required=False, validators=None):
|
|
|
|
|
|
|
|
self.type_converter = type
|
|
|
|
self.name = name
|
|
|
|
self.label = label or name.capitalize()
|
|
|
|
self.values = values or []
|
|
|
|
self.required = required
|
|
|
|
self.validators = validators or []
|
|
|
|
self.errors = exceptions.CompoundException()
|
|
|
|
self._bound = False
|
|
|
|
self._validated = False
|
|
|
|
|
|
|
|
def bind(self, values):
|
|
|
|
|
|
|
|
if not self._bound:
|
|
|
|
|
|
|
|
# called by Form.bind
|
|
|
|
|
|
|
|
for value in values:
|
|
|
|
|
|
|
|
if value == '':
|
|
|
|
value = None
|
|
|
|
|
|
|
|
if value is not None:
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
# attempt to convert value to pythonic type
|
|
|
|
value = self.type_converter.convert(value, None, None)
|
|
|
|
|
|
|
|
except click.exceptions.BadParameter:
|
|
|
|
|
2024-06-11 09:54:12 +00:00
|
|
|
app.logger.warning(f"BadParameter for group '{self.label}', this is likely either a bug or someone's fucking with POST values.")
|
2024-05-05 20:10:41 +00:00
|
|
|
value = None
|
|
|
|
|
|
|
|
self.values.append(value)
|
|
|
|
|
|
|
|
self._bound = True
|
|
|
|
|
|
|
|
def validate(self, submit):
|
|
|
|
|
|
|
|
# called by Form.validate (after Form.bind)
|
|
|
|
|
|
|
|
if not self._validated:
|
|
|
|
|
|
|
|
if len(self.values) == 0 and self.required:
|
|
|
|
|
|
|
|
self.errors.append(ValidationError(f"At least one item in group '{self.label}' must be checked."))
|
|
|
|
|
|
|
|
for validator in self.validators:
|
|
|
|
|
|
|
|
for value in self.values:
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
validator(self.value)
|
|
|
|
|
|
|
|
except ValidationError as e:
|
|
|
|
|
|
|
|
self.errors.append(e)
|
|
|
|
|
|
|
|
self._validated = True
|
|
|
|
|
|
|
|
if len(self.errors):
|
|
|
|
|
|
|
|
raise self.errors
|
|
|
|
|
|
|
|
class Group:
|
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
"""
|
|
|
|
A form element group with a single value (i.e. for radiobuttons).
|
|
|
|
"""
|
|
|
|
|
2024-05-05 20:10:41 +00:00
|
|
|
def __init__(self, type, name, label=None, value=None, required=False, validators=None):
|
|
|
|
|
|
|
|
self.type_converter = type
|
|
|
|
self.name = name
|
|
|
|
self.label = label or name.capitalize()
|
|
|
|
self.value = value
|
|
|
|
self.required = required
|
|
|
|
self.validators = validators or []
|
|
|
|
self.errors = exceptions.CompoundException()
|
|
|
|
self._badparameter = False # hint set in bind to let .validate know type conversion failed
|
|
|
|
self._bound = False
|
|
|
|
self._validated = False
|
|
|
|
|
|
|
|
def bind(self, values):
|
|
|
|
|
|
|
|
if not self._bound:
|
|
|
|
|
|
|
|
if len(values):
|
|
|
|
value = values[0]
|
|
|
|
else:
|
|
|
|
value = None
|
|
|
|
|
|
|
|
# from here on, it's the same as Field.bind
|
|
|
|
if value == '':
|
|
|
|
value = None
|
|
|
|
|
|
|
|
if value is not None:
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
# attempt to convert value to pythonic type
|
|
|
|
value = self.type_converter.convert(value, None, None)
|
|
|
|
|
|
|
|
except click.exceptions.BadParameter:
|
|
|
|
|
|
|
|
self._badparameter = True
|
|
|
|
value = None
|
|
|
|
|
|
|
|
self.value = value
|
|
|
|
self._bound = True
|
|
|
|
|
|
|
|
def validate(self, submit):
|
|
|
|
|
|
|
|
# called by Form.validate (after Form.bind)
|
|
|
|
|
|
|
|
if not self._validated:
|
|
|
|
|
|
|
|
if self.value is None and self.required:
|
|
|
|
|
|
|
|
self.errors.append(ValidationError(f"One item in group '{self.label}' must be selected."))
|
|
|
|
|
|
|
|
for validator in self.validators:
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
validator(self.value)
|
|
|
|
|
|
|
|
except ValidationError as e:
|
|
|
|
|
|
|
|
self.errors.append(e)
|
|
|
|
|
|
|
|
self._validated = True
|
|
|
|
|
|
|
|
if len(self.errors):
|
|
|
|
raise self.errors
|
|
|
|
|
2024-05-31 14:41:36 +00:00
|
|
|
class ElementContainer:
|
|
|
|
|
|
|
|
def __init__(self, form):
|
|
|
|
|
|
|
|
self.form = form
|
|
|
|
self.elements = {}
|
2024-06-01 13:43:31 +00:00
|
|
|
self.order = ()
|
2024-05-31 14:41:36 +00:00
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
|
|
|
|
return self.elements[key]
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
|
|
|
|
value.prefix = self.form.name_qualified
|
|
|
|
value.name = key
|
2024-06-01 13:43:31 +00:00
|
|
|
if not key in self.order:
|
|
|
|
# only add key if it doesn't already exist
|
|
|
|
# i.e. replacing fields conserves order
|
|
|
|
new_order = list(self.order)
|
|
|
|
new_order.append(key)
|
|
|
|
self.order = tuple(new_order)
|
|
|
|
|
2024-05-31 14:41:36 +00:00
|
|
|
self.elements[key] = value
|
|
|
|
|
|
|
|
def __delitem__(self, key):
|
|
|
|
|
2024-06-01 13:43:31 +00:00
|
|
|
new_order = list(self.order)
|
|
|
|
new_order.remove(key)
|
|
|
|
self.order = tuple(new_order)
|
2024-05-31 14:41:36 +00:00
|
|
|
del self.elements[key]
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return self.elements.__len__()
|
|
|
|
|
|
|
|
def __iter__(self):
|
2024-06-01 13:43:31 +00:00
|
|
|
return self.order.__iter__()
|
|
|
|
|
|
|
|
def __contains__(self, key):
|
|
|
|
return key in self.elements
|
2024-05-31 14:41:36 +00:00
|
|
|
|
|
|
|
def keys(self):
|
2024-06-01 13:43:31 +00:00
|
|
|
return self.order
|
2024-05-31 14:41:36 +00:00
|
|
|
|
|
|
|
def items(self):
|
2024-06-01 13:43:31 +00:00
|
|
|
return [(key, self.elements[key]) for key in self.order] # TODO: generator-based solution?
|
2024-05-31 14:41:36 +00:00
|
|
|
|
|
|
|
def values(self):
|
2024-06-01 13:43:31 +00:00
|
|
|
return [self.elements[key] for key in self.order]
|
2024-05-31 14:41:36 +00:00
|
|
|
|
|
|
|
def clear(self):
|
2024-06-01 13:43:31 +00:00
|
|
|
self.order = ()
|
2024-05-31 14:41:36 +00:00
|
|
|
self.elements.clear()
|
|
|
|
|
2024-06-01 13:43:31 +00:00
|
|
|
def position(self, key, index):
|
|
|
|
|
|
|
|
if index >= len(self):
|
|
|
|
raise ValueError(f"Position {index} not possible for container with {len(self)} elements. Note that indexes are 0-based.")
|
2024-06-29 03:36:26 +00:00
|
|
|
elif index == -1:
|
|
|
|
index = len(self) - 1
|
2024-06-01 13:43:31 +00:00
|
|
|
|
2024-06-29 03:36:26 +00:00
|
|
|
old_order = list(self.order)
|
|
|
|
old_order.remove(key)
|
2024-06-01 13:43:31 +00:00
|
|
|
new_order = []
|
|
|
|
|
2024-06-29 03:36:26 +00:00
|
|
|
for k in old_order[:index]:
|
|
|
|
new_order.append(k)
|
2024-06-01 13:43:31 +00:00
|
|
|
|
|
|
|
new_order.append(key)
|
|
|
|
|
2024-06-29 03:36:26 +00:00
|
|
|
for k in old_order[index:]:
|
|
|
|
new_order.append(k)
|
2024-06-01 13:43:31 +00:00
|
|
|
|
|
|
|
self.order = tuple(new_order)
|
|
|
|
|
2024-05-31 14:41:36 +00:00
|
|
|
class FieldContainer(ElementContainer):
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
|
|
|
|
if not isinstance(value, Field):
|
|
|
|
raise TypeError("Form elements must be derived from form.Field.")
|
|
|
|
|
|
|
|
super().__setitem__(key, value)
|
|
|
|
|
|
|
|
class ButtonContainer(ElementContainer):
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
|
|
|
|
if not isinstance(value, Button):
|
|
|
|
raise TypeError("Form elements must be derived from form.Button.")
|
|
|
|
|
|
|
|
super().__setitem__(key, value)
|
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
class FormBase(rendering.Renderable):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
def __new__(cls, *args, **kwargs):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
instance = super().__new__(cls)
|
2024-07-27 01:05:56 +00:00
|
|
|
instance._created = time.time()
|
2024-04-17 17:30:52 +00:00
|
|
|
instance._rendered = False
|
|
|
|
|
|
|
|
# set up attributes already needed during __new__/__init__
|
2024-05-31 14:41:36 +00:00
|
|
|
instance.fields = FieldContainer(instance)
|
|
|
|
instance.buttons = ButtonContainer(instance)
|
2024-04-17 17:30:52 +00:00
|
|
|
instance.name = None
|
|
|
|
instance.prefix = None
|
|
|
|
|
|
|
|
for attr_name in dir(cls):
|
|
|
|
|
|
|
|
attr = getattr(cls, attr_name)
|
|
|
|
|
|
|
|
if isinstance(attr, Field):
|
|
|
|
|
|
|
|
clone = copy.deepcopy(attr)
|
|
|
|
clone.name = attr_name
|
|
|
|
|
2024-05-31 14:41:36 +00:00
|
|
|
instance[attr_name] = clone
|
2024-04-17 17:30:52 +00:00
|
|
|
|
|
|
|
elif isinstance(attr, Button):
|
|
|
|
|
|
|
|
clone = copy.deepcopy(attr)
|
|
|
|
clone.name = attr_name
|
|
|
|
|
|
|
|
instance.buttons[attr_name] = clone
|
|
|
|
|
2024-07-27 01:05:56 +00:00
|
|
|
instance.fields.order = tuple([field.name for field in sorted(instance.fields.values(), key=lambda x: x._created)])
|
|
|
|
instance.buttons.order = tuple([button.name for button in sorted(instance.buttons.values(), key=lambda x: x._created)])
|
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
return instance
|
|
|
|
|
2024-06-29 03:36:26 +00:00
|
|
|
def __init__(self, prefix=None, name=None, title=None, id=None, action=None, method='POST', **kwargs):
|
|
|
|
|
|
|
|
super().__init__(**kwargs)
|
2024-04-17 17:30:52 +00:00
|
|
|
|
|
|
|
self.name = name if name else self._lowerclass
|
|
|
|
self.prefix = prefix # NOTE: implicitly sets self.name_qualified through __setattr__
|
2024-04-12 19:22:00 +00:00
|
|
|
self.title = title
|
|
|
|
self.id = id
|
|
|
|
self.action = action
|
|
|
|
self.method = method
|
2024-04-12 19:22:00 +00:00
|
|
|
self.errors = exceptions.CompoundException()
|
2024-04-12 19:22:00 +00:00
|
|
|
self.intro = None
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
def __setattr__(self, name, value):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
if name == 'name':
|
|
|
|
|
|
|
|
if value is None:
|
|
|
|
self.name_qualified = None
|
|
|
|
|
|
|
|
else:
|
|
|
|
if not isinstance(value, str):
|
|
|
|
raise ValueError(f"Form name must be a string or None, {value} is neither.")
|
|
|
|
elif '.' in value:
|
|
|
|
raise ValueError(f"Form name must not contain dots, but '{value}' does.")
|
|
|
|
|
|
|
|
if self.prefix:
|
|
|
|
self.name_qualified = f'{self.prefix}.{value}'
|
|
|
|
else:
|
|
|
|
self.name_qualified = value
|
|
|
|
|
|
|
|
elif name == 'prefix':
|
|
|
|
|
|
|
|
# determine correct prefix for children of this form (fields, fieldsets, buttons)
|
|
|
|
if self.name is not None:
|
|
|
|
|
|
|
|
if value is not None: # new prefix != None
|
|
|
|
self.name_qualified = f'{value}.{self.name}'
|
|
|
|
else:
|
|
|
|
self.name_qualified = self.name
|
|
|
|
|
|
|
|
elif name == 'name_qualified':
|
|
|
|
|
|
|
|
if value is not None: # ignore setting qualified name during construction
|
|
|
|
|
|
|
|
for field in self.fields.values():
|
|
|
|
field.prefix = value
|
|
|
|
|
|
|
|
for button in self.buttons.values():
|
|
|
|
button.prefix = value
|
|
|
|
|
|
|
|
# finally do the actual assignment via super()
|
|
|
|
super().__setattr__(name, value)
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-05-31 14:41:36 +00:00
|
|
|
# start dictionary interface
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
|
|
|
|
return self.fields[key]
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
|
|
|
|
self.fields[key] = value
|
|
|
|
|
|
|
|
def __delitem__(self, key):
|
|
|
|
|
|
|
|
del self.fields[key]
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return self.fields.__len__()
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self.fields.__iter__()
|
|
|
|
|
|
|
|
def keys(self):
|
|
|
|
return self.fields.keys()
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
return self.fields.items()
|
|
|
|
|
|
|
|
def values(self):
|
|
|
|
return self.fields.values()
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
self.fields.clear()
|
|
|
|
|
|
|
|
# end dictionary interface
|
|
|
|
|
2024-06-29 03:36:26 +00:00
|
|
|
@property
|
|
|
|
def css_classes(self):
|
|
|
|
return f'form-{self.name} {self._lowerclass} {super().css_classes}'
|
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
@property
|
|
|
|
def valid(self):
|
2024-04-17 17:30:52 +00:00
|
|
|
if 'errors' in dir(self):
|
|
|
|
return len(self.errors) == 0
|
|
|
|
return True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def fields_pending_render(self):
|
2024-06-01 13:43:31 +00:00
|
|
|
return [field for field in self.fields.values() if not field._rendered]
|
2024-04-17 17:30:52 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def buttons_pending_render(self):
|
2024-06-01 13:43:31 +00:00
|
|
|
return [button for button in self.buttons.values() if not button._rendered]
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-23 13:42:01 +00:00
|
|
|
def template_candidates(self, mode, format):
|
2024-04-14 13:56:36 +00:00
|
|
|
|
|
|
|
candidates = []
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-14 13:56:36 +00:00
|
|
|
for cls in self.__class__.mro():
|
2024-04-17 17:30:52 +00:00
|
|
|
if issubclass(cls, FormBase):
|
2024-04-14 13:56:36 +00:00
|
|
|
candidates += [
|
2024-04-23 13:42:01 +00:00
|
|
|
f'{app.config["THEME"]}/templates/form/{cls._lowerclass}-{mode}.{format}',
|
|
|
|
f'{app.config["THEME"]}/templates/form/{cls._lowerclass}.{format}',
|
2024-04-14 13:56:36 +00:00
|
|
|
]
|
2024-04-12 19:22:00 +00:00
|
|
|
|
|
|
|
if app.config['THEME'] != 'default':
|
2024-04-14 13:56:36 +00:00
|
|
|
for cls in self.__class__.mro():
|
2024-04-17 17:30:52 +00:00
|
|
|
if issubclass(cls, FormBase):
|
2024-04-14 13:56:36 +00:00
|
|
|
candidates += [
|
2024-04-23 13:42:01 +00:00
|
|
|
f'default/templates/form/{self._lowerclass}-{mode}.{format}',
|
|
|
|
f'default/templates/form/{self._lowerclass}.{format}',
|
2024-04-14 13:56:36 +00:00
|
|
|
]
|
2024-04-12 19:22:00 +00:00
|
|
|
|
|
|
|
return candidates
|
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
def bind(self, form, files):
|
|
|
|
|
|
|
|
# form is flask.request.form or equivalent
|
|
|
|
# files is flask.request.files or equivalent
|
2024-04-12 19:22:00 +00:00
|
|
|
|
|
|
|
for field in self.fields.values():
|
|
|
|
try:
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
if isinstance(field, Fieldset):
|
|
|
|
field.bind(form, files)
|
|
|
|
|
2024-05-05 20:10:41 +00:00
|
|
|
elif isinstance(field, GroupableField) and field.group is not None:
|
|
|
|
|
|
|
|
# File isn't groupable, so source is always form
|
|
|
|
field.group.bind(form.getlist(field.group.name))
|
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
else:
|
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
if isinstance(field, File):
|
|
|
|
source = files
|
|
|
|
else:
|
|
|
|
source = form
|
|
|
|
|
|
|
|
field.bind(source.get(field.name_qualified))
|
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
except ValidationError as e:
|
|
|
|
self.errors.append(e)
|
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
def submit_relative(self, submit):
|
|
|
|
|
|
|
|
# get the submit identifier relative to self
|
|
|
|
|
|
|
|
if not submit.startswith(f'{self.name_qualified}.'):
|
|
|
|
raise ValueError(f"Submit '{submit}' not in form/fieldset '{self.name_qualified}'.")
|
|
|
|
|
|
|
|
return submit[len(self.name_qualified) + 1:]
|
|
|
|
|
|
|
|
def submitted_fieldset(self, submit):
|
|
|
|
|
|
|
|
submit_relative = self.submit_relative(submit)
|
|
|
|
|
|
|
|
if '.' in submit_relative: # submit on child fieldset pressed
|
|
|
|
|
|
|
|
# so we found out which fieldset
|
|
|
|
fieldset_name, fieldset_submit = submit_relative.split('.', 1)
|
|
|
|
|
|
|
|
if fieldset_name not in self.fields: # see if it exists
|
|
|
|
raise ValueError(f"Submit '{submit}' in inexistant fieldset '{fieldset_name}'.")
|
|
|
|
|
|
|
|
elif not isinstance(self.fields[fieldset_name], Fieldset): # and is actually a fieldset
|
|
|
|
|
|
|
|
raise ValueError(f"Submit '{submit}' for field '{fieldset_name}' which isn't a fieldset.")
|
|
|
|
|
|
|
|
return self.fields[fieldset_name]
|
|
|
|
|
|
|
|
def validate(self, submit):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
|
|
|
# called after bind, so field elements have their .value set
|
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
for field in self.fields.values(): # we iterate through our own fields
|
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
try:
|
2024-04-17 17:30:52 +00:00
|
|
|
|
2024-05-05 20:10:41 +00:00
|
|
|
if isinstance(field, GroupableField) and field.group is not None:
|
|
|
|
field.group.validate(submit)
|
|
|
|
else:
|
|
|
|
field.validate(submit) # and validate those
|
2024-04-17 17:30:52 +00:00
|
|
|
|
|
|
|
except exceptions.CompoundException as e: # collecting any validation errors
|
|
|
|
|
2024-05-31 14:41:36 +00:00
|
|
|
self.errors.extend(e.exceptions)
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
def process(self, submit):
|
|
|
|
# default processing function is a no-op. this way, route callbacks
|
|
|
|
# can act based directly on field object values, tho this is only
|
|
|
|
# recommended for trivial use-cases.
|
|
|
|
pass
|
|
|
|
|
|
|
|
def handle(self, skip_bind=False):
|
|
|
|
|
|
|
|
if 'submit' not in flask.request.form:
|
|
|
|
self.errors.append(ValidationError("Missing submit information."))
|
|
|
|
return
|
|
|
|
|
|
|
|
submit = flask.request.form.get('submit')
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
if not skip_bind:
|
|
|
|
# bind is called once by the topmost form element being .handle()d.
|
|
|
|
# this is done so form field values input in the browser are always
|
|
|
|
# conserved, no matter on which fieldset/form the submit button was
|
|
|
|
# pushed.
|
|
|
|
self.bind(flask.request.form, flask.request.files)
|
|
|
|
|
|
|
|
# find submitting element, either self or a child fieldset
|
|
|
|
fieldset = self.submitted_fieldset(submit)
|
|
|
|
|
|
|
|
if fieldset is not None:
|
|
|
|
# handle fieldset, but skip binding, as we already did that
|
|
|
|
return fieldset.handle(skip_bind=True)
|
|
|
|
else:
|
|
|
|
submit_relative = self.submit_relative(submit)
|
|
|
|
self.validate(submit_relative)
|
2024-05-05 20:10:41 +00:00
|
|
|
|
|
|
|
if len(self.errors):
|
|
|
|
for e in self.errors:
|
|
|
|
flask.flash(str(e), 'error')
|
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
return self.process(submit_relative)
|
|
|
|
|
|
|
|
class Form(FormBase):
|
|
|
|
pass
|
2024-04-12 19:22:00 +00:00
|
|
|
|
|
|
|
class Button(rendering.Renderable):
|
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
def __new__(cls, *args, **kwargs):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
instance = super().__new__(cls)
|
2024-07-27 01:05:56 +00:00
|
|
|
instance._created = time.time()
|
2024-04-17 17:30:52 +00:00
|
|
|
instance._rendered = False
|
|
|
|
|
|
|
|
return instance
|
|
|
|
|
2024-06-29 03:36:26 +00:00
|
|
|
def __init__(self, prefix=None, type='submit', name=None, label='Submit', id=None, **kwargs):
|
|
|
|
|
|
|
|
super().__init__(**kwargs)
|
2024-04-17 17:30:52 +00:00
|
|
|
|
|
|
|
self.prefix = None
|
|
|
|
self.type = type
|
2024-04-12 19:22:00 +00:00
|
|
|
self.label = label
|
|
|
|
self.id = id
|
2024-04-17 17:30:52 +00:00
|
|
|
self.name = name # NOTE button-submit outputs "submit" for <button name
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-23 13:42:01 +00:00
|
|
|
def template_candidates(self, mode, format):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-14 13:56:36 +00:00
|
|
|
candidates = []
|
|
|
|
|
|
|
|
for cls in self.__class__.mro():
|
|
|
|
if issubclass(cls, Button):
|
|
|
|
candidates += [
|
2024-04-23 13:42:01 +00:00
|
|
|
f'{app.config["THEME"]}/templates/form/button/{cls._lowerclass}-{self.type}.{format}',
|
|
|
|
f'{app.config["THEME"]}/templates/form/button/{cls._lowerclass}.{format}',
|
2024-04-14 13:56:36 +00:00
|
|
|
]
|
2024-04-12 19:22:00 +00:00
|
|
|
|
|
|
|
if app.config['THEME'] != 'default':
|
2024-04-14 13:56:36 +00:00
|
|
|
for cls in self.__class__.mro():
|
|
|
|
if issubclass(cls, Button):
|
|
|
|
candidates += [
|
2024-04-23 13:42:01 +00:00
|
|
|
f'default/templates/form/button/{self._lowerclass}-{self.type}.{format}',
|
|
|
|
f'default/templates/form/button/{self._lowerclass}.{format}',
|
2024-04-14 13:56:36 +00:00
|
|
|
]
|
2024-04-12 19:22:00 +00:00
|
|
|
|
|
|
|
return candidates
|
|
|
|
|
2024-04-23 13:42:01 +00:00
|
|
|
def render(self, mode='full', format='html'):
|
2024-04-17 17:30:52 +00:00
|
|
|
|
|
|
|
self._rendered = True
|
2024-04-23 13:42:01 +00:00
|
|
|
return super().render(mode=mode, format=format)
|
2024-04-17 17:30:52 +00:00
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
class Field(rendering.Renderable):
|
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
type_converter = types.STRING
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
def __new__(cls, *args, **kwargs):
|
|
|
|
|
|
|
|
instance = super().__new__(cls)
|
2024-07-27 01:05:56 +00:00
|
|
|
instance._created = time.time()
|
2024-04-17 17:30:52 +00:00
|
|
|
instance._rendered = False
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
# set up attributes already needed during __new__/__init__
|
|
|
|
instance.name = None
|
|
|
|
instance.prefix = None
|
|
|
|
|
|
|
|
return instance
|
|
|
|
|
2024-06-29 03:36:26 +00:00
|
|
|
def __init__(self, prefix=None, label=None, value=None, id=None, help=None, required=False, validators=None, **kwargs):
|
|
|
|
|
|
|
|
super().__init__(**kwargs)
|
2024-04-17 17:30:52 +00:00
|
|
|
|
|
|
|
self.prefix = prefix
|
2024-05-31 14:41:36 +00:00
|
|
|
self.name = None
|
2024-04-12 19:22:00 +00:00
|
|
|
self.label = label
|
|
|
|
self.value = value
|
2024-06-17 22:32:06 +00:00
|
|
|
self._id = id
|
2024-04-29 16:16:00 +00:00
|
|
|
self.help = help
|
2024-04-12 19:22:00 +00:00
|
|
|
self.required = required
|
2024-04-23 09:01:18 +00:00
|
|
|
self.validators = validators or []
|
2024-04-12 19:22:00 +00:00
|
|
|
self.errors = exceptions.CompoundException()
|
2024-04-21 14:02:58 +00:00
|
|
|
self._badparameter = False # hint set in bind to let .validate know type conversion failed
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
def __setattr__(self, name, value):
|
|
|
|
|
|
|
|
if name == 'name':
|
|
|
|
|
|
|
|
if value is None:
|
|
|
|
self.name_qualified = None
|
|
|
|
|
|
|
|
else:
|
|
|
|
if not isinstance(value, str):
|
|
|
|
raise ValueError(f"Field name must be a string or None, {value} is neither.")
|
|
|
|
elif '.' in value:
|
|
|
|
raise ValueError(f"Field name must not contain dots, but '{value}' does.")
|
|
|
|
|
|
|
|
if self.prefix:
|
|
|
|
self.name_qualified = f'{self.prefix}.{value}'
|
|
|
|
else:
|
|
|
|
self.name_qualified = value
|
|
|
|
|
|
|
|
elif name == 'prefix':
|
|
|
|
|
|
|
|
# determine correct prefix for children of this form (fields, fieldsets, buttons)
|
|
|
|
if self.name is not None:
|
|
|
|
|
|
|
|
if value is not None: # new prefix != None
|
|
|
|
self.name_qualified = f'{value}.{self.name}'
|
|
|
|
else:
|
|
|
|
self.name_qualified = self.name
|
|
|
|
|
2024-06-17 22:32:06 +00:00
|
|
|
elif name == 'help':
|
|
|
|
|
|
|
|
value = markdown.MarkdownString(value)
|
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
# finally do the actual assignment via super()
|
|
|
|
super().__setattr__(name, value)
|
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
@property
|
|
|
|
def valid(self):
|
|
|
|
return len(self.errors) == 0
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-06-17 22:32:06 +00:00
|
|
|
@property
|
|
|
|
def id(self):
|
|
|
|
if self._id:
|
|
|
|
return self._id
|
|
|
|
|
|
|
|
return self.name_qualified.replace('.', '-')
|
|
|
|
|
|
|
|
@id.setter
|
|
|
|
def id(self, value):
|
|
|
|
self._id = value
|
|
|
|
|
2024-06-29 03:36:26 +00:00
|
|
|
@property
|
|
|
|
def css_classes(self):
|
|
|
|
return f'field-{self.name} {self._lowerclass} {super().css_classes}'
|
|
|
|
|
2024-07-27 01:05:56 +00:00
|
|
|
@property
|
|
|
|
def html_value(self):
|
|
|
|
if self.value is None:
|
|
|
|
return ''
|
|
|
|
return self.value
|
|
|
|
|
2024-04-23 13:42:01 +00:00
|
|
|
def template_candidates(self, mode, format):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-14 13:56:36 +00:00
|
|
|
candidates = []
|
|
|
|
|
|
|
|
for cls in self.__class__.mro():
|
|
|
|
if issubclass(cls, Field):
|
|
|
|
candidates += [
|
2024-04-23 13:42:01 +00:00
|
|
|
f'{app.config["THEME"]}/templates/form/field/{cls._lowerclass}-{mode}.{format}',
|
|
|
|
f'{app.config["THEME"]}/templates/form/field/{cls._lowerclass}.{format}',
|
2024-04-14 13:56:36 +00:00
|
|
|
]
|
2024-04-12 19:22:00 +00:00
|
|
|
|
|
|
|
if app.config['THEME'] != 'default':
|
2024-04-14 13:56:36 +00:00
|
|
|
for cls in self.__class__.mro():
|
|
|
|
if issubclass(cls, Field):
|
|
|
|
candidates += [
|
2024-04-23 13:42:01 +00:00
|
|
|
f'default/templates/form/field/{self._lowerclass}-{mode}.{format}',
|
|
|
|
f'default/templates/form/field/{self._lowerclass}.{format}',
|
2024-04-14 13:56:36 +00:00
|
|
|
]
|
2024-04-12 19:22:00 +00:00
|
|
|
|
|
|
|
return candidates
|
|
|
|
|
2024-04-23 13:42:01 +00:00
|
|
|
def render(self, mode='full', format='html'):
|
2024-04-17 17:30:52 +00:00
|
|
|
|
|
|
|
self._rendered = True
|
2024-04-23 13:42:01 +00:00
|
|
|
return super().render(mode=mode, format=format)
|
2024-04-17 17:30:52 +00:00
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
def bind(self, value):
|
|
|
|
|
|
|
|
# called by Form.bind
|
|
|
|
|
2024-04-21 14:02:58 +00:00
|
|
|
if value == '':
|
|
|
|
value = None
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-21 14:02:58 +00:00
|
|
|
if value is not None:
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-21 14:02:58 +00:00
|
|
|
try:
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-21 14:02:58 +00:00
|
|
|
# attempt to convert value to pythonic type
|
|
|
|
value = self.type_converter.convert(value, None, None)
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-21 14:02:58 +00:00
|
|
|
except click.exceptions.BadParameter:
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-21 14:02:58 +00:00
|
|
|
self._badparameter = True
|
|
|
|
value = None
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-21 14:02:58 +00:00
|
|
|
self.value = value
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
def validate(self, submit):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
|
|
|
# called by Form.validate (after Form.bind)
|
|
|
|
|
2024-04-21 14:02:58 +00:00
|
|
|
if self.value is None:
|
|
|
|
|
|
|
|
if self._badparameter:
|
|
|
|
|
2024-04-23 09:01:18 +00:00
|
|
|
self.errors.append(ValidationError(f"Invalid input '{self.value}' for field '{self.label}'"))
|
2024-04-21 14:02:58 +00:00
|
|
|
|
|
|
|
elif self.required:
|
|
|
|
|
|
|
|
self.errors.append(ValidationError(f"Field '{self.label}' is required."))
|
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
for validator in self.validators:
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
try:
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-23 09:01:18 +00:00
|
|
|
validator(self.value)
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
except ValidationError as e:
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
self.errors.append(e)
|
|
|
|
|
2024-05-05 20:10:41 +00:00
|
|
|
if len(self.errors):
|
|
|
|
raise self.errors
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
class ValueField(Field):
|
|
|
|
|
2024-05-02 18:57:38 +00:00
|
|
|
"""
|
|
|
|
Field class which wraps a static value.
|
|
|
|
"""
|
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
def bind(self, value):
|
|
|
|
# ignore values from request in order to use value passed to __init__.
|
|
|
|
# as this is only a "virtual" field never rendered or otherwise sent
|
|
|
|
# to the client, we'd only ever get None here (unless the client fucks
|
|
|
|
# with us) anyhow.
|
2024-04-29 16:16:00 +00:00
|
|
|
# SECURITY: Do NOT remove this function override. Doing so will allow
|
|
|
|
# attackers to POST values to overwrite values considered secure.
|
2024-04-17 17:30:52 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def render(self, mode='full'):
|
|
|
|
return '' # no-op
|
|
|
|
|
2024-05-02 18:57:38 +00:00
|
|
|
class RenderableField(Field):
|
|
|
|
|
|
|
|
"""
|
|
|
|
Field class which just thinly wraps a Renderable object
|
|
|
|
for display in a form.
|
|
|
|
"""
|
|
|
|
|
2024-06-29 03:36:26 +00:00
|
|
|
def __init__(self, renderable, mode='teaser', **kwargs):
|
|
|
|
|
|
|
|
super().__init__(**kwargs)
|
2024-05-02 18:57:38 +00:00
|
|
|
|
|
|
|
self.renderable = renderable
|
|
|
|
self.mode = mode
|
|
|
|
|
|
|
|
def bind(self, value):
|
|
|
|
pass
|
|
|
|
|
2024-05-05 21:19:56 +00:00
|
|
|
def validate(self, submit):
|
|
|
|
pass
|
|
|
|
|
2024-05-05 20:10:41 +00:00
|
|
|
class GroupableField(Field):
|
|
|
|
|
|
|
|
def __init__(self, group=None, **kwargs):
|
|
|
|
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
self.group = group
|
|
|
|
|
|
|
|
if group is not None:
|
|
|
|
self.type_converter = group.type_converter # is this even needed?
|
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
class Input(Field):
|
|
|
|
input_type = 'text'
|
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
class Hidden(Input):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
|
|
|
#TODO: type_converter
|
2024-07-31 02:11:42 +00:00
|
|
|
input_type = 'hidden'
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
class Text(Field):
|
|
|
|
input_type = 'text'
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
class Search(Input):
|
|
|
|
input_type = 'search'
|
2024-07-27 01:05:56 +00:00
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
class Password(Input):
|
|
|
|
input_type = 'password'
|
2024-07-27 01:05:56 +00:00
|
|
|
|
|
|
|
class EMail(Input):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
|
|
|
#TODO: type_converter
|
|
|
|
input_type = 'email'
|
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
class Tel(Input):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
#TODO: type_converter
|
|
|
|
input_type = 'tel'
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
class URL(Input):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
|
|
|
#TODO: type_converter
|
2024-07-31 02:11:42 +00:00
|
|
|
input_type = 'url'
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
class Float(Input):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
type_converter = types.FLOAT
|
2024-04-12 19:22:00 +00:00
|
|
|
input_type = 'number'
|
|
|
|
|
2024-07-27 01:05:56 +00:00
|
|
|
def __init__(self, *args, min=None, max=None, step=0.01, **kwargs):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-27 01:05:56 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.min = min
|
|
|
|
self.max = max
|
|
|
|
self.step = step
|
2024-07-31 02:11:42 +00:00
|
|
|
|
2024-07-27 01:05:56 +00:00
|
|
|
self.validators.append(functools.partial(validate_range, min, max))
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-27 01:05:56 +00:00
|
|
|
class Integer(Float):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
type_converter = types.INT
|
|
|
|
|
2024-07-27 01:05:56 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-27 01:05:56 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.step = 1
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-27 01:05:56 +00:00
|
|
|
class FloatRange(Float):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-27 01:05:56 +00:00
|
|
|
input_type = 'range'
|
2024-04-12 19:22:00 +00:00
|
|
|
|
|
|
|
class IntegerRange(FloatRange):
|
2024-07-31 02:11:42 +00:00
|
|
|
|
2024-07-27 01:05:56 +00:00
|
|
|
input_type = 'range'
|
2024-07-31 02:11:42 +00:00
|
|
|
type_converter = types.INT
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-27 01:05:56 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.step = 1
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
class Color(Input):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
type_converter = types.COLOR
|
|
|
|
input_type = 'color'
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
@property
|
|
|
|
def html_value(self):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
if self.value is None:
|
|
|
|
return None
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
return self.value.hex_rgb()
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
class Date(Input):
|
|
|
|
|
|
|
|
type_converter = types.DATE
|
|
|
|
input_type = 'date'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def html_value(self):
|
|
|
|
|
|
|
|
value = super().html_value
|
|
|
|
|
|
|
|
if isinstance(value, datetime.date):
|
|
|
|
return value.strftime('%Y-%m-%d')
|
2024-04-12 19:22:00 +00:00
|
|
|
|
|
|
|
class Time(Input):
|
|
|
|
|
|
|
|
input_type = 'time'
|
2024-07-31 02:11:42 +00:00
|
|
|
type_converter = types.TIME
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
class DateTime(Input):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
type_converter = types.DATETIME
|
|
|
|
input_type = 'datetime-local'
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
@property
|
|
|
|
def html_value(self):
|
|
|
|
|
|
|
|
value = super().html_value
|
|
|
|
|
|
|
|
if isinstance(value, datetime.datetime):
|
2024-10-14 14:54:17 +00:00
|
|
|
return value.strftime('%Y-%m-%dT%H:%M')
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-05-05 20:10:41 +00:00
|
|
|
class Checkbox(GroupableField, Input):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
type_converter = types.BOOL
|
2024-04-12 19:22:00 +00:00
|
|
|
input_type = 'checkbox'
|
|
|
|
|
2024-05-05 20:10:41 +00:00
|
|
|
@property
|
|
|
|
def checked(self):
|
|
|
|
|
|
|
|
if self.group:
|
|
|
|
|
|
|
|
if self.group.values is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return self.value in self.group.values
|
|
|
|
|
|
|
|
# no group means we're a bool
|
|
|
|
return bool(self.value)
|
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
def bind(self, value):
|
|
|
|
|
|
|
|
# called by Form.bind
|
|
|
|
|
|
|
|
if value is None:
|
|
|
|
value = '' # translate None to empty string because that's what click expects
|
|
|
|
|
|
|
|
if value == '':
|
|
|
|
|
2024-05-05 20:10:41 +00:00
|
|
|
value = False
|
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
if self.required:
|
|
|
|
|
|
|
|
e = ValidationError(f"Field '{self.label}' is required.")
|
|
|
|
self.errors.append(e)
|
|
|
|
|
|
|
|
raise e
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
self.value = self.type_converter.convert(value, None, None)
|
|
|
|
|
|
|
|
except click.exceptions.BadParameter:
|
|
|
|
|
|
|
|
e = ValidationError(f"Invalid input '{value}' for field '{self.label}'")
|
|
|
|
self.errors.append(e)
|
|
|
|
|
|
|
|
raise e
|
|
|
|
|
|
|
|
if self.value == '':
|
|
|
|
# shouldn't be needed/possible to trigger, can probably be removed
|
|
|
|
self.value = None
|
|
|
|
|
2024-05-05 20:10:41 +00:00
|
|
|
class Radiobutton(GroupableField, Input):
|
|
|
|
|
|
|
|
input_type = 'radio'
|
2024-07-31 02:11:42 +00:00
|
|
|
type_converter = types.INT
|
2024-05-05 20:10:41 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def checked(self):
|
|
|
|
|
|
|
|
if self.group:
|
|
|
|
|
|
|
|
if self.group.value is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return self.value == self.group.value
|
|
|
|
|
|
|
|
return bool(self.value)
|
|
|
|
|
2024-07-31 02:11:42 +00:00
|
|
|
class File(Input):
|
|
|
|
|
|
|
|
type_converter = types.File()
|
|
|
|
input_type = 'file'
|
|
|
|
|
|
|
|
class TextArea(Field):
|
|
|
|
pass
|
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
class Select(Field):
|
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
def __init__(self, choices, **kwargs):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
super().__init__(**kwargs)
|
2024-04-12 19:22:00 +00:00
|
|
|
self.choices = choices
|
|
|
|
|
|
|
|
class ForeignKeySelect(Select):
|
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
def __init__(self, db_field, **kwargs):
|
2024-04-12 19:22:00 +00:00
|
|
|
|
|
|
|
if not isinstance(db_field, peewee.ForeignKeyField):
|
|
|
|
raise TypeError("ForeignKeySelect db_field MUST be a ForeignKeyField.")
|
|
|
|
|
2024-04-21 14:02:58 +00:00
|
|
|
if type(db_field.rel_field) in (peewee.IdentityField, peewee.IntegerField):
|
|
|
|
# set type converter for integer types
|
|
|
|
# otherwise the default string converter is fine (for now)
|
2024-07-31 02:11:42 +00:00
|
|
|
self.type_converter = types.INT
|
2024-04-21 14:02:58 +00:00
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
choices = []
|
|
|
|
for instance in db_field.rel_model.select():
|
|
|
|
|
|
|
|
if hasattr(instance, 'title'):
|
2024-04-14 13:56:36 +00:00
|
|
|
choice_label = instance.title
|
2024-04-12 19:22:00 +00:00
|
|
|
elif hasattr(instance, 'name'):
|
2024-04-14 13:56:36 +00:00
|
|
|
choice_label = instance.name
|
2024-04-12 19:22:00 +00:00
|
|
|
elif hasattr(instance, 'id'):
|
2024-04-14 13:56:36 +00:00
|
|
|
choice_label = f"{instance.__class__.__name__} #{instance.id}"
|
2024-04-12 19:22:00 +00:00
|
|
|
else:
|
2024-04-14 13:56:36 +00:00
|
|
|
choice_label = repr(instance) # not pretty, but should be unique
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-14 13:56:36 +00:00
|
|
|
choices.append(( getattr(instance, db_field.rel_field.name), choice_label ))
|
2024-04-12 19:22:00 +00:00
|
|
|
|
2024-04-21 14:02:58 +00:00
|
|
|
if 'label' not in kwargs:
|
|
|
|
kwargs['label'] = db_field.verbose_name or db_field.name
|
|
|
|
|
2024-04-12 19:22:00 +00:00
|
|
|
kwargs['choices'] = choices
|
2024-04-17 17:30:52 +00:00
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
class Fieldset(FormBase, Field):
|
2024-05-05 21:19:56 +00:00
|
|
|
|
|
|
|
def validate(self, submit):
|
|
|
|
|
2024-05-05 21:46:36 +00:00
|
|
|
# do the same validation as Form(Base)
|
2024-05-05 21:19:56 +00:00
|
|
|
super().validate(submit)
|
|
|
|
|
2024-05-05 21:46:36 +00:00
|
|
|
# but escalate any errors upwards like a Field
|
2024-05-05 21:19:56 +00:00
|
|
|
if len(self.errors):
|
|
|
|
raise self.errors
|
2024-04-17 17:30:52 +00:00
|
|
|
|
|
|
|
class ProxyField(Field):
|
|
|
|
|
2024-06-29 03:36:26 +00:00
|
|
|
def __init__(self, field, **kwargs):
|
2024-04-17 17:30:52 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
prefix: parent fieldsets name
|
|
|
|
field: field object to proxy
|
|
|
|
"""
|
|
|
|
|
2024-06-29 03:36:26 +00:00
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
self.field = field
|
|
|
|
self.name = field.name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
|
|
|
return self.field.id
|
|
|
|
|
2024-06-29 03:36:26 +00:00
|
|
|
@property
|
|
|
|
def css_classes(self):
|
|
|
|
return f'{self._lowerclass} {self.field.css_classes }{super().css_classes}'
|
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
@property
|
|
|
|
def label(self):
|
|
|
|
return self.field.label
|
|
|
|
|
|
|
|
@property
|
|
|
|
def value(self):
|
|
|
|
return self.field.value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def required(self):
|
|
|
|
return self.field.required
|
|
|
|
|
|
|
|
@property
|
|
|
|
def errors(self):
|
|
|
|
return self.field.errors
|
|
|
|
|
|
|
|
def bind(self, value):
|
|
|
|
|
|
|
|
self.field.bind(value)
|
|
|
|
|
|
|
|
def validate(self, submit):
|
|
|
|
self.field.validate(submit)
|
|
|
|
|
2024-04-23 13:42:01 +00:00
|
|
|
def render(self, mode='full', format='html'):
|
|
|
|
return self.field.render(mode, format=format)
|
2024-04-17 17:30:52 +00:00
|
|
|
|
|
|
|
class ProxyFieldset(Fieldset):
|
|
|
|
|
|
|
|
def __init__(self, form, **kwargs):
|
|
|
|
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
self.form = form
|
2024-04-17 18:44:16 +00:00
|
|
|
self.intro = form.intro
|
2024-04-17 17:30:52 +00:00
|
|
|
self.form.prefix = self.prefix
|
|
|
|
self.fields = form.fields
|
|
|
|
self.buttons = form.buttons
|
|
|
|
|
|
|
|
if 'name' in kwargs:
|
|
|
|
self.form.name = kwargs['name']
|
|
|
|
|
|
|
|
self.name = self.form.name
|
|
|
|
|
|
|
|
def __setattr__(self, name, value):
|
|
|
|
|
|
|
|
if name == 'name':
|
|
|
|
if hasattr(self, 'form'):
|
|
|
|
self.form.name = value
|
|
|
|
|
|
|
|
elif name == 'prefix':
|
|
|
|
if hasattr(self, 'form'):
|
|
|
|
self.form.prefix = value
|
|
|
|
|
|
|
|
super().__setattr__(name, value)
|
|
|
|
|
2024-06-29 03:36:26 +00:00
|
|
|
@property
|
|
|
|
def css_classes(self):
|
|
|
|
return f'{self._lowerclass} {super().css_classes} {self.form.css_classes}'
|
|
|
|
|
2024-04-17 17:30:52 +00:00
|
|
|
def bind(self, form, files):
|
|
|
|
self.form.bind(form, files)
|
|
|
|
|
|
|
|
def validate(self, submit):
|
|
|
|
self.form.validate(submit)
|
|
|
|
|
|
|
|
def process(self, submit):
|
|
|
|
self.form.process(submit)
|
|
|
|
|
|
|
|
def handle(self, skip_bind=False):
|
|
|
|
return self.form.handle(skip_bind=skip_bind)
|