Skip to content

Commit d9be067

Browse files
committed
More refactor
1 parent 6e8a1a9 commit d9be067

File tree

2 files changed

+118
-129
lines changed

2 files changed

+118
-129
lines changed

validatedfile/models.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def clean(self, *args, **kwargs):
2323
file.seek(0)
2424

2525
if not content_type_headers in self.content_types or not content_type_magic in self.content_types:
26-
raise forms.ValidationError(_('Filetype %s not supported.') % (file.content_type))
26+
raise forms.ValidationError(_('Filetype %s not supported.') % (content_type_magic))
2727

2828
if self.max_upload_size:
2929
if file._size > self.max_upload_size:
@@ -68,32 +68,3 @@ def __call__(self, file):
6868
(filesizeformat(self.quota.max_usage),
6969
filesizeformat(self.quota.current_usage + file_size))))
7070

71-
72-
#
73-
#
74-
# def __init__(self, items = [], file_attr_name = None):
75-
# self.items = items
76-
# self.file_attr_name = file_attr_name
77-
#
78-
# def current_usage(self):
79-
# usage = 0
80-
# for item in self.items:
81-
# the_file = getattr(item, self.file_attr_name, None)
82-
# if the_file:
83-
# usage += the_file.size
84-
# return usage
85-
#
86-
#class QuotaValidator(object):
87-
# def __init__(self, max_usage):
88-
# self.quota = FileQuota()
89-
# self.max_usage = max_usage
90-
#
91-
# def set_quota(self, quota):
92-
# self.quota = quota
93-
#
94-
# def __call__(self, file):
95-
# tried_usage = self.quota.current_usage() + file.size
96-
# if tried_usage > self.max_usage:
97-
# raise forms.ValidationError(_('Please keep the total uploaded files under %s. With this file, the total would be %s' %
98-
# (filesizeformat(self.max_usage), filesizeformat(tried_usage))))
99-
#

validatedfile/tests/__init__.py

Lines changed: 117 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@ def test_create_instance_with_file(self):
3131

3232

3333
def test_form_ok(self):
34-
uploaded_file = SimpleUploadedFile(
35-
name = 'the_file.png',
36-
content = self._get_sample_file('image2k.png').read(),
37-
content_type = 'image/png',
38-
)
39-
form = TestModelForm(data = {}, files = {'the_file': uploaded_file})
34+
form = self._create_bound_test_model_form(form_class = TestModelForm,
35+
orig_filename = 'image2k.png',
36+
dest_filename = 'the_file.png',
37+
content_type = 'image/png')
4038
self.assertTrue(form.is_valid())
4139
instance = form.save()
4240

@@ -45,62 +43,55 @@ def test_form_ok(self):
4543
instance.the_file.delete()
4644
instance.delete()
4745

48-
4946
def test_form_invalid_size(self):
50-
uploaded_file = SimpleUploadedFile(
51-
name = 'the_file.png',
52-
content = self._get_sample_file('image15k.png').read(),
53-
content_type = 'image/png',
54-
)
55-
form = TestModelForm(data = {}, files = {'the_file': uploaded_file})
47+
form = self._create_bound_test_model_form(form_class = TestModelForm,
48+
orig_filename = 'image15k.png',
49+
dest_filename = 'the_file.png',
50+
content_type = 'image/png')
5651
self.assertFalse(form.is_valid())
5752
self.assertEqual(len(form.errors), 1)
5853
self.assertEqual(len(form.errors['the_file']), 1)
54+
self.assertEqual(form.errors['the_file'][0], u'Please keep filesize under 10.0 KB. Current filesize 14.2 KB')
5955

6056

6157
def test_form_invalid_filetype(self):
62-
uploaded_file = SimpleUploadedFile(
63-
name = 'the_file.pdf',
64-
content = self._get_sample_file('document1k.pdf').read(),
65-
content_type = 'application/pdf',
66-
)
67-
form = TestModelForm(data = {}, files = {'the_file': uploaded_file})
58+
form = self._create_bound_test_model_form(form_class = TestModelForm,
59+
orig_filename = 'document1k.pdf',
60+
dest_filename = 'the_file.pdf',
61+
content_type = 'application/pdf')
6862
self.assertFalse(form.is_valid())
6963
self.assertEqual(len(form.errors), 1)
7064
self.assertEqual(len(form.errors['the_file']), 1)
65+
self.assertEqual(form.errors['the_file'][0], u'Filetype application/pdf not supported.')
7166

7267

7368
def test_form_invalid_filetype_and_size(self):
74-
uploaded_file = SimpleUploadedFile(
75-
name = 'the_file.pdf',
76-
content = self._get_sample_file('document15k.pdf').read(),
77-
content_type = 'application/pdf',
78-
)
79-
form = TestModelForm(data = {}, files = {'the_file': uploaded_file})
69+
form = self._create_bound_test_model_form(form_class = TestModelForm,
70+
orig_filename = 'document15k.pdf',
71+
dest_filename = 'the_file.pdf',
72+
content_type = 'application/pdf')
8073
self.assertFalse(form.is_valid())
8174
self.assertEqual(len(form.errors), 1)
8275
self.assertEqual(len(form.errors['the_file']), 1)
76+
self.assertEqual(form.errors['the_file'][0], u'Filetype application/pdf not supported.')
8377

8478

8579
def test_form_fake_filetype(self):
86-
uploaded_file = SimpleUploadedFile(
87-
name = 'the_file.png',
88-
content = self._get_sample_file('document1k.pdf').read(),
89-
content_type = 'image/png',
90-
)
91-
form = TestModelForm(data = {}, files = {'the_file': uploaded_file})
80+
form = self._create_bound_test_model_form(form_class = TestModelForm,
81+
orig_filename = 'document1k.pdf',
82+
dest_filename = 'the_file.pdf',
83+
content_type = 'image/png')
9284
self.assertFalse(form.is_valid())
9385
self.assertEqual(len(form.errors), 1)
9486
self.assertEqual(len(form.errors['the_file']), 1)
87+
self.assertEqual(form.errors['the_file'][0], u'Filetype application/pdf not supported.')
9588

9689

9790
def test_form_no_validate(self):
98-
uploaded_file = SimpleUploadedFile(
99-
name = 'the_file.pdf',
100-
content = self._get_sample_file('document15k.pdf').read(),
101-
content_type = 'application/pdf',
102-
)
103-
form = TestModelNoValidateForm(data = {}, files = {'the_file': uploaded_file})
91+
form = self._create_bound_test_model_form(form_class = TestModelNoValidateForm,
92+
orig_filename = 'document15k.pdf',
93+
dest_filename = 'the_file.pdf',
94+
content_type = 'application/pdf')
10495
self.assertTrue(form.is_valid())
10596
instance = form.save()
10697

@@ -111,7 +102,7 @@ def test_form_no_validate(self):
111102

112103

113104
def test_form_null_file(self):
114-
form = TestModelNoValidateForm(data = {}, files = {})
105+
form = self._create_bound_test_model_form(form_class = TestModelNoValidateForm)
115106
self.assertTrue(form.is_valid())
116107
instance = form.save()
117108

@@ -121,7 +112,7 @@ def test_form_null_file(self):
121112

122113

123114
def test_quota_empty(self):
124-
container = TestContainer.objects.create(name = 'container1')
115+
container = self._create_container(name = 'container1')
125116

126117
quota = FileQuota()
127118
quota.update(container.test_elements.all(), 'the_file')
@@ -132,11 +123,10 @@ def test_quota_empty(self):
132123

133124

134125
def test_quota_one_file(self):
135-
container = TestContainer.objects.create(name = 'container1')
136-
element = TestElement.objects.create(
137-
container = container,
138-
the_file = File(self._get_sample_file('image2k.png'), 'the_file.png')
139-
)
126+
container = self._create_container(name = 'container')
127+
element = self._add_element(container = container,
128+
orig_filename = 'image2k.png',
129+
dest_filename = 'the_file.png')
140130

141131
quota = FileQuota()
142132
quota.update(container.test_elements.all(), 'the_file')
@@ -149,20 +139,17 @@ def test_quota_one_file(self):
149139

150140

151141
def test_quota_several_files_several_containers(self):
152-
container1 = TestContainer.objects.create(name = 'container1')
153-
element1 = TestElement.objects.create(
154-
container = container1,
155-
the_file = File(self._get_sample_file('image2k.png'), 'the_file1.png')
156-
)
157-
element2 = TestElement.objects.create(
158-
container = container1,
159-
the_file = File(self._get_sample_file('image15k.png'), 'the_file2.png')
160-
)
161-
container2 = TestContainer.objects.create(name = 'container2')
162-
element3 = TestElement.objects.create(
163-
container = container2,
164-
the_file = File(self._get_sample_file('document15k.pdf'), 'the_file3.pdf')
165-
)
142+
container1 = self._create_container(name = 'container1')
143+
element1 = self._add_element(container = container1,
144+
orig_filename = 'image2k.png',
145+
dest_filename = 'the_file1.png')
146+
element2 = self._add_element(container = container1,
147+
orig_filename = 'image15k.png',
148+
dest_filename = 'the_file2.png')
149+
container2 = self._create_container(name = 'container2')
150+
element3 = self._add_element(container = container2,
151+
orig_filename = 'document15k.pdf',
152+
dest_filename = 'the_file3.pdf')
166153

167154
quota = FileQuota(max_usage = 20000)
168155
quota.update(container1.test_elements.all(), 'the_file')
@@ -182,16 +169,15 @@ def test_quota_several_files_several_containers(self):
182169
def test_quota_exceeds(self):
183170
quota = FileQuota(max_usage = 1000)
184171

185-
container = TestContainer.objects.create(name = 'container1')
172+
container = self._create_container(name = 'container1')
186173
quota.update(container.test_elements.all(), 'the_file')
187174
self.assertEqual(quota.current_usage, 0)
188175
self.assertFalse(quota.exceeds())
189176
self.assertTrue(quota.exceeds(2120))
190177

191-
element = TestElement.objects.create(
192-
container = container,
193-
the_file = File(self._get_sample_file('image2k.png'), 'the_file.png')
194-
)
178+
element = self._add_element(container = container,
179+
orig_filename = 'image2k.png',
180+
dest_filename = 'the_file.png')
195181
quota.update(container.test_elements.all(), 'the_file')
196182
self.assertEqual(quota.current_usage, 2120)
197183
self.assertTrue(quota.exceeds())
@@ -202,17 +188,16 @@ def test_quota_exceeds(self):
202188

203189

204190
def test_form_quota_check(self):
205-
container = TestContainer.objects.create(name = 'container1')
191+
container = self._create_container(name = 'container1')
206192

207-
form1 = TestElementForm(container = container)
193+
form1 = self._create_unbound_test_element_form(container = container)
208194
self.assertFalse(form1.exceeds_quota())
209195

210-
element = TestElement.objects.create(
211-
container = container,
212-
the_file = File(self._get_sample_file('image15k.png'), 'the_file.png')
213-
)
196+
element = self._add_element(container = container,
197+
orig_filename = 'image15k.png',
198+
dest_filename = 'the_file.png')
214199

215-
form2 = TestElementForm(container = container)
200+
form2 = self._create_unbound_test_element_form(container = container)
216201
self.assertTrue(form2.exceeds_quota())
217202

218203
element.the_file.delete()
@@ -221,43 +206,32 @@ def test_form_quota_check(self):
221206

222207

223208
def test_form_quota_ok(self):
224-
container = TestContainer.objects.create(name = 'container1')
225-
226-
uploaded_file = SimpleUploadedFile(
227-
name = 'the_file.png',
228-
content = self._get_sample_file('image2k.png').read(),
229-
content_type = 'image/png',
230-
)
231-
form = TestElementForm(
232-
container = container,
233-
data = {},
234-
files = {'the_file': uploaded_file}
235-
)
209+
container = self._create_container(name = 'container1')
236210

211+
form = self._create_bound_test_element_form(container = container,
212+
orig_filename = 'image2k.png',
213+
dest_filename = 'the_file.png',
214+
content_type = 'image/png')
237215
self.assertTrue(form.is_valid())
238216

239217
container.delete()
240218

241219

242220
def test_form_quota_exceeded(self):
243-
container = TestContainer.objects.create(name = 'container1')
244-
element = TestElement.objects.create(
245-
container = container,
246-
the_file = File(self._get_sample_file('image2k.png'), 'the_file.png')
247-
)
248-
249-
uploaded_file = SimpleUploadedFile(
250-
name = 'the_file.png',
251-
content = self._get_sample_file('image15k.png').read(),
252-
content_type = 'image/png',
253-
)
254-
form = TestElementForm(
255-
container = container,
256-
data = {},
257-
files = {'the_file': uploaded_file}
258-
)
259-
221+
container = self._create_container(name = 'container1')
222+
element = self._add_element(container = container,
223+
orig_filename = 'image2k.png',
224+
dest_filename = 'the_file.png')
225+
226+
form = self._create_bound_test_element_form(container = container,
227+
orig_filename = 'image15k.png',
228+
dest_filename = 'the_file.png',
229+
content_type = 'image/png')
260230
self.assertFalse(form.is_valid())
231+
self.assertEqual(len(form.errors), 1)
232+
self.assertEqual(len(form.errors['the_file']), 1)
233+
self.assertEqual(form.errors['the_file'][0],
234+
u'Please keep the total uploaded files under 9.8 KB. With this file, the total would be 16.3 KB')
261235

262236
element.the_file.delete()
263237
element.delete()
@@ -279,3 +253,47 @@ def _check_file_url(self, filefield, filename):
279253
def _get_file_url(self, filename):
280254
return os.path.join(MEDIA_ROOT, prefix, filename)
281255

256+
257+
def _create_bound_test_model_form(self, form_class, orig_filename = None,
258+
dest_filename = None, content_type = None):
259+
if orig_filename and dest_filename and content_type:
260+
uploaded_file = SimpleUploadedFile(
261+
name = dest_filename,
262+
content = self._get_sample_file(orig_filename).read(),
263+
content_type = content_type,
264+
)
265+
files = {'the_file': uploaded_file}
266+
else:
267+
files = {}
268+
form = form_class(data = {}, files = files)
269+
return form
270+
271+
272+
def _create_container(self, name):
273+
return TestContainer.objects.create(name = name)
274+
275+
276+
def _add_element(self, container, orig_filename, dest_filename):
277+
return container.test_elements.create(
278+
the_file = File(self._get_sample_file(orig_filename), dest_filename)
279+
)
280+
281+
282+
def _create_unbound_test_element_form(self, container):
283+
return TestElementForm(container = container)
284+
285+
286+
def _create_bound_test_element_form(self, container, orig_filename = None,
287+
dest_filename = None, content_type = None):
288+
if orig_filename and dest_filename and content_type:
289+
uploaded_file = SimpleUploadedFile(
290+
name = dest_filename,
291+
content = self._get_sample_file(orig_filename).read(),
292+
content_type = content_type,
293+
)
294+
files = {'the_file': uploaded_file}
295+
else:
296+
files = {}
297+
form = TestElementForm(container = container, data = {}, files = files)
298+
return form
299+

0 commit comments

Comments
 (0)