Created
July 10, 2012 00:51
-
-
Save glarrain/3080251 to your computer and use it in GitHub Desktop.
Custom Django SessionStorage. Removed all the functionality that dealt with files. Based on Django-1.4's django.contrib.formtools.wizard.storage.base.BaseStorage django.contrib.formtools.wizard.storage.session.SessionStorage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.utils.datastructures import MultiValueDict | |
class SessionStorage(object): | |
"""Custom SessionStorage class. Removed all the functionality that dealt | |
with files. | |
Based in Django-1.4's: | |
`django.contrib.formtools.wizard.storage.base.BaseStorage` | |
`django.contrib.formtools.wizard.storage.session.SessionStorage` | |
""" | |
step_key = 'step' | |
step_data_key = 'step_data' | |
extra_data_key = 'extra_data' | |
def __init__(self, prefix, request=None): | |
self.prefix = 'wizard_%s' % prefix | |
self.request = request | |
if self.prefix not in self.request.session: | |
self.init_data() | |
def init_data(self): | |
self.data = { | |
self.step_key: None, | |
self.step_data_key: {}, | |
self.extra_data_key: {}, | |
} | |
def _get_data(self): | |
self.request.session.modified = True | |
return self.request.session[self.prefix] | |
def _set_data(self, value): | |
self.request.session[self.prefix] = value | |
self.request.session.modified = True | |
data = property(_get_data, _set_data) | |
def reset(self): | |
self.init_data() | |
def _get_current_step(self): | |
return self.data[self.step_key] | |
def _set_current_step(self, step): | |
self.data[self.step_key] = step | |
current_step = property(_get_current_step, _set_current_step) | |
def _get_extra_data(self): | |
return self.data[self.extra_data_key] or {} | |
def _set_extra_data(self, extra_data): | |
self.data[self.extra_data_key] = extra_data | |
extra_data = property(_get_extra_data, _set_extra_data) | |
def get_step_data(self, step): | |
# When reading the serialized data, upconvert it to a MultiValueDict, | |
# some serializers (json) don't preserve the type of the object. | |
values = self.data[self.step_data_key].get(step, None) | |
if values is not None: | |
values = MultiValueDict(values) | |
return values | |
def set_step_data(self, step, cleaned_data): | |
# If the value is a MultiValueDict, convert it to a regular dict of the | |
# underlying contents. Some serializers call the public API on it (as | |
# opposed to the underlying dict methods), in which case the content | |
# can be truncated (__getitem__ returns only the first item). | |
if isinstance(cleaned_data, MultiValueDict): | |
cleaned_data = dict(cleaned_data.lists()) | |
self.data[self.step_data_key][step] = cleaned_data | |
@property | |
def current_step_data(self): | |
return self.get_step_data(self.current_step) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome file!