App Engine Python SDK version 1.7.4 (2)
[gae.git] / python / lib / django_1_4 / django / contrib / formtools / wizard / storage / base.py
blob274e07ffbea148c3403599014b75769c39f70b09
1 from django.core.files.uploadedfile import UploadedFile
2 from django.utils.datastructures import MultiValueDict
3 from django.utils.encoding import smart_str
4 from django.utils.functional import lazy_property
6 from django.contrib.formtools.wizard.storage.exceptions import NoFileStorageConfigured
9 class BaseStorage(object):
10 step_key = 'step'
11 step_data_key = 'step_data'
12 step_files_key = 'step_files'
13 extra_data_key = 'extra_data'
15 def __init__(self, prefix, request=None, file_storage=None):
16 self.prefix = 'wizard_%s' % prefix
17 self.request = request
18 self.file_storage = file_storage
20 def init_data(self):
21 self.data = {
22 self.step_key: None,
23 self.step_data_key: {},
24 self.step_files_key: {},
25 self.extra_data_key: {},
28 def reset(self):
29 self.init_data()
31 def _get_current_step(self):
32 return self.data[self.step_key]
34 def _set_current_step(self, step):
35 self.data[self.step_key] = step
37 current_step = lazy_property(_get_current_step, _set_current_step)
39 def _get_extra_data(self):
40 return self.data[self.extra_data_key] or {}
42 def _set_extra_data(self, extra_data):
43 self.data[self.extra_data_key] = extra_data
45 extra_data = lazy_property(_get_extra_data, _set_extra_data)
47 def get_step_data(self, step):
48 # When reading the serialized data, upconvert it to a MultiValueDict,
49 # some serializers (json) don't preserve the type of the object.
50 values = self.data[self.step_data_key].get(step, None)
51 if values is not None:
52 values = MultiValueDict(values)
53 return values
55 def set_step_data(self, step, cleaned_data):
56 # If the value is a MultiValueDict, convert it to a regular dict of the
57 # underlying contents. Some serializers call the public API on it (as
58 # opposed to the underlying dict methods), in which case the content
59 # can be truncated (__getitem__ returns only the first item).
60 if isinstance(cleaned_data, MultiValueDict):
61 cleaned_data = dict(cleaned_data.lists())
62 self.data[self.step_data_key][step] = cleaned_data
64 @property
65 def current_step_data(self):
66 return self.get_step_data(self.current_step)
68 def get_step_files(self, step):
69 wizard_files = self.data[self.step_files_key].get(step, {})
71 if wizard_files and not self.file_storage:
72 raise NoFileStorageConfigured
74 files = {}
75 for field, field_dict in wizard_files.iteritems():
76 field_dict = dict((smart_str(k), v)
77 for k, v in field_dict.iteritems())
78 tmp_name = field_dict.pop('tmp_name')
79 files[field] = UploadedFile(
80 file=self.file_storage.open(tmp_name), **field_dict)
81 return files or None
83 def set_step_files(self, step, files):
84 if files and not self.file_storage:
85 raise NoFileStorageConfigured
87 if step not in self.data[self.step_files_key]:
88 self.data[self.step_files_key][step] = {}
90 for field, field_file in (files or {}).iteritems():
91 tmp_filename = self.file_storage.save(field_file.name, field_file)
92 file_dict = {
93 'tmp_name': tmp_filename,
94 'name': field_file.name,
95 'content_type': field_file.content_type,
96 'size': field_file.size,
97 'charset': field_file.charset
99 self.data[self.step_files_key][step][field] = file_dict
101 @property
102 def current_step_files(self):
103 return self.get_step_files(self.current_step)
105 def update_response(self, response):
106 pass