Moved most form field initial values from forms to settings.
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import importlib
|
||||
import logging
|
||||
import re
|
||||
from django.apps import AppConfig as _AppConfig
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Form and Model Field Config
|
||||
COMMON_CHAR_FIELD_LENGTH = 250
|
||||
NUMBER_MAX_LENGTH = 12
|
||||
@@ -18,6 +21,69 @@ MEALS_OTHER_MAX_LENGTH = COMMON_CHAR_FIELD_LENGTH
|
||||
ADDITIONAL_COSTS_MAX_LENGTH = COMMON_CHAR_FIELD_LENGTH
|
||||
|
||||
|
||||
class FieldInitial(object):
|
||||
_constraint_re = re.compile(r'^(?P<field>[a-z_]+)(?P<op>[=]+)(?P<value>.*)$')
|
||||
|
||||
def __init__(self, *args):
|
||||
self._tuples = []
|
||||
|
||||
if len(args) == 1:
|
||||
self._tuples = [(None, args[0])]
|
||||
else:
|
||||
constraint = None
|
||||
for arg in args:
|
||||
if constraint is None:
|
||||
constraint = arg
|
||||
else:
|
||||
self._tuples.append((constraint, arg))
|
||||
constraint = None
|
||||
|
||||
def get_value(self, session_data):
|
||||
parameters = {
|
||||
'mode': session_data.get('mode', None),
|
||||
'sport': session_data.get('sport', None),
|
||||
'level': session_data.get('level', None),
|
||||
'country': session_data.get('country', None),
|
||||
'terrain': session_data.get('terrain', None),
|
||||
'overnight': str(bool(session_data.get('last_day', None))),
|
||||
}
|
||||
for tuple in self._tuples:
|
||||
constraint, result = tuple
|
||||
|
||||
if not constraint:
|
||||
return result
|
||||
|
||||
match = False
|
||||
constraint_parts = constraint.split(',')
|
||||
for sub_constraint in constraint_parts:
|
||||
c = self._constraint_re.match(sub_constraint)
|
||||
if c is not None:
|
||||
c_field = c.group('field')
|
||||
if c_field not in parameters:
|
||||
logger.error('FieldInitial: Invalid field: \'%s\'', sub_constraint)
|
||||
continue
|
||||
c_op = c.group('op')
|
||||
c_value = c.group('value')
|
||||
if c_op == '==':
|
||||
if parameters[c_field] == c_value:
|
||||
match = True
|
||||
continue
|
||||
else:
|
||||
match = False
|
||||
break
|
||||
else:
|
||||
logger.error('FieldInitial: Invalid operator: \'%s\'', sub_constraint)
|
||||
continue
|
||||
else:
|
||||
logger.error('FieldInitial: Invalid constraint: \'%s\'', constraint)
|
||||
break
|
||||
|
||||
if match:
|
||||
return result
|
||||
|
||||
return None
|
||||
|
||||
|
||||
class DefaultSetting(object):
|
||||
def __init__(self, name, value, key_name=None, validator=None):
|
||||
self.name = name
|
||||
|
||||
Reference in New Issue
Block a user