Coverage for dashboard\custom_modules\validators.py: 42%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import magic
3from django.core.exceptions import ValidationError
4from django.utils.deconstruct import deconstructible
5from django.template.defaultfilters import filesizeformat
7from django.conf import settings
10@deconstructible # further investigate this function
11class FileValidator(object):
12 error_messages = {
13 'max_size': ("Ensure this file size is not greater than %(max_size)s."
14 " Your file size is %(size)s."),
15 'content_type': "Files of type %(content_type)s are not supported.",
16 }
18 def __init__(self, max_size=None, content_types=settings.ACCEPTABLE_FILE_TYPES):
19 self.max_size = max_size
20 self.content_types = content_types
22 def __call__(self, data):
23 if self.max_size is not None and data.size > self.max_size:
24 params = {
25 'max_size': filesizeformat(self.max_size),
26 'size': filesizeformat(data.size),
27 }
28 raise ValidationError(self.error_messages['max_size'],
29 'max_size', params)
31 if self.content_types:
32 content_type = magic.from_buffer(data.read(), mime=True)
33 data.seek(0)
34 print(content_type)
35 print(data.seek(0))
38 if content_type not in self.content_types:
39 params = { 'content_type': content_type }
40 raise ValidationError(self.error_messages['content_type'],
41 'content_type', params)
42 else:
43 return content_type