Cleaned passing of session data between ChainedForms in EventCreateView.
This commit is contained in:
@@ -2,92 +2,72 @@ import datetime
|
||||
import logging
|
||||
import re
|
||||
|
||||
from django.http import QueryDict
|
||||
|
||||
from . import models
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class SessionDict(QueryDict):
|
||||
_re_iso8601 = re.compile(r'^ISO8601:(?P<date>'
|
||||
r'(?P<year>\d{4})'
|
||||
r'-(?P<mon>(0[1-9])|(1[012]))'
|
||||
r'-(?P<day>(0[1-9])|([12][0-9])|(3[01]))'
|
||||
r')?(?P<sep>.)?'
|
||||
r'(?P<time>'
|
||||
r'(?P<hour>([01][0-9])|(2[0123]))'
|
||||
r':(?P<min>[0-5][0-9])'
|
||||
r':(?P<sec>[0-5][0-9])'
|
||||
r'([\,\.]\d{1,10})?'
|
||||
r')?'
|
||||
r'(?P<offset>[\-+](([01][0-9])|(2[0123]))(:[0-5][0-9])?)?$')
|
||||
class Iso8601Serializer(object):
|
||||
marker = 'ISO8601'
|
||||
separator = ':'
|
||||
|
||||
def __init__(self, instance=None, *args, **kwargs):
|
||||
if 'mutable' in kwargs:
|
||||
raise TypeError('__init__(): got an unexpected keyword argument \'mutable\'')
|
||||
kwargs['mutable'] = True
|
||||
super(SessionDict, self).__init__(*args, **kwargs)
|
||||
_re = re.compile(r'^(?P<date>'
|
||||
r'(?P<year>\d{4})'
|
||||
r'-(?P<mon>(0[1-9])|(1[012]))'
|
||||
r'-(?P<day>(0[1-9])|([12][0-9])|(3[01]))'
|
||||
r')?(?P<sep>.)?'
|
||||
r'(?P<time>'
|
||||
r'(?P<hour>([01][0-9])|(2[0123]))'
|
||||
r':(?P<min>[0-5][0-9])'
|
||||
r':(?P<sec>[0-5][0-9])'
|
||||
r'([,.]\d{1,10})?'
|
||||
r')?'
|
||||
r'(?P<offset>[\-+](([01][0-9])|(2[0123]))(:[0-5][0-9])?)?$')
|
||||
|
||||
def __init__(self, instance=None, text=None):
|
||||
if instance is not None:
|
||||
self.update(instance)
|
||||
self.instance = instance
|
||||
elif text is not None:
|
||||
self.instance = Iso8601Serializer.deserialize(text)
|
||||
|
||||
def _deserialize_value(self, value):
|
||||
if isinstance(value, basestring):
|
||||
m = self._re_iso8601.match(value)
|
||||
@property
|
||||
def str(self):
|
||||
return Iso8601Serializer.serialize(self.instance)
|
||||
|
||||
@classmethod
|
||||
def serialize(cls, value, ignore_unsupported_input=False):
|
||||
strval = None
|
||||
if isinstance(value, datetime.datetime):
|
||||
strval = value.isoformat()
|
||||
elif isinstance(value, datetime.date):
|
||||
strval = value.isoformat()
|
||||
elif isinstance(value, datetime.time):
|
||||
strval = value.isoformat()
|
||||
elif not ignore_unsupported_input:
|
||||
raise ValueError('Expected datetime.datetime, datetime.date or datetime.time,'
|
||||
' not {}'.format(value.__class__.__name__))
|
||||
|
||||
if strval is not None:
|
||||
value = '{marker}{sep}{str}'.format(marker=cls.marker, sep=cls.separator, str=strval)
|
||||
|
||||
return value
|
||||
|
||||
@classmethod
|
||||
def deserialize(cls, value, ignore_unsupported_input=False):
|
||||
prefix = '{marker}{sep}'.format(marker=cls.marker, sep=cls.separator)
|
||||
if isinstance(value, basestring) and value.startswith(prefix):
|
||||
haystack = value[len(prefix):]
|
||||
m = cls._re.match(haystack)
|
||||
if m is not None:
|
||||
# logger.debug('SessionDict._deserialize_value(): found iso8601 date \'%s\'', value)
|
||||
gd = m.groupdict()
|
||||
if gd['hour'] is None:
|
||||
value = datetime.date(int(gd['year']), int(gd['mon']), int(gd['day']))
|
||||
# logger.debug('SessionDict._deserialize_value(): converted into datetime.date \'%s\'', value)
|
||||
elif gd['year'] is None:
|
||||
value = datetime.time(hour=int(gd['hour']), minute=int(gd['min']), second=int(gd['sec']))
|
||||
# logger.debug('SessionDict._deserialize_value(): converted into datetime.time \'%s\'', value)
|
||||
else:
|
||||
value = datetime.datetime(int(gd['year']), int(gd['mon']), int(gd['day']),
|
||||
hour=int(gd['hour']), minute=int(gd['min']), second=int(gd['sec']))
|
||||
# logger.debug('SessionDict._deserialize_value(): converted into datetime.datetime \'%s\'', value)
|
||||
elif not ignore_unsupported_input:
|
||||
raise ValueError('Format not recognized \'{str}\''.format(str=haystack))
|
||||
elif not ignore_unsupported_input:
|
||||
raise ValueError('Expected basestring,'
|
||||
' not {}'.format(value.__class__.__name__))
|
||||
return value
|
||||
|
||||
def _serialize_value(self, value):
|
||||
if isinstance(value, datetime.datetime):
|
||||
# logger.debug('SessionDict._serialize_value(): found datetime.datetime \'%s\'', value)
|
||||
value = u'ISO8601:{}'.format(value.isoformat())
|
||||
# logger.debug('SessionDict._serialize_value(): converted into unicode \'%s\'', value)
|
||||
elif isinstance(value, datetime.date):
|
||||
# logger.debug('SessionDict._serialize_value(): found datetime.date \'%s\'', value)
|
||||
value = u'ISO8601:{}'.format(value.isoformat())
|
||||
# logger.debug('SessionDict._serialize_value(): converted into unicode \'%s\'', value)
|
||||
elif isinstance(value, datetime.time):
|
||||
# logger.debug('SessionDict._serialize_value(): found datetime.time \'%s\'', value)
|
||||
value = u'ISO8601:{}'.format(value.isoformat())
|
||||
# logger.debug('SessionDict._serialize_value(): converted into unicode \'%s\'', value)
|
||||
return value
|
||||
|
||||
@property
|
||||
def session_data(self):
|
||||
ret = dict()
|
||||
for k in self:
|
||||
ret[k] = self._serialize_value(self[k])
|
||||
return ret
|
||||
|
||||
def update(self, data):
|
||||
for k in data:
|
||||
self[k] = self._deserialize_value(data[k])
|
||||
|
||||
|
||||
def event_factory(data):
|
||||
kwargs = dict()
|
||||
if 'deadline' in data:
|
||||
buf = data['deadline']
|
||||
if isinstance(buf, basestring):
|
||||
deadline_choice = buf.lower()
|
||||
deadline_field = 'deadline_{}'.format(deadline_choice)
|
||||
if deadline_field in data:
|
||||
data['deadline'] = data[deadline_field]
|
||||
|
||||
for field in data:
|
||||
if hasattr(models.Event, field):
|
||||
kwargs[field] = data[field]
|
||||
|
||||
return models.Event(**kwargs)
|
||||
Reference in New Issue
Block a user