Skip to content

Commit

Permalink
more specialized file fields
Browse files Browse the repository at this point in the history
  • Loading branch information
francesco-filicetti committed Mar 20, 2020
1 parent 7905083 commit 20038cb
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
47 changes: 45 additions & 2 deletions django_form_builder/dynamic_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class CustomFileField(FileField, BaseCustomField):
"""
FileField
"""
field_type = _("Allegato PDF")
field_type = _("Allegato (generico)")

def __init__(self, *args, **data_kwargs):
super().__init__(*args, **data_kwargs)
Expand All @@ -163,7 +163,9 @@ def raise_error(self, name, cleaned_data, **kwargs):
errors = []
if data:
msg = ''
permitted_upload_filetype = getattr(settings, 'PERMITTED_UPLOAD_FILETYPE') if hasattr(settings, 'PERMITTED_UPLOAD_FILETYPE') else PERMITTED_UPLOAD_FILETYPE
self_valid_extensions = getattr(self, 'valid_extensions') if hasattr(self, 'valid_extensions') else None
settings_upload_filetype = getattr(settings, 'PERMITTED_UPLOAD_FILETYPE') if hasattr(settings, 'PERMITTED_UPLOAD_FILETYPE') else PERMITTED_UPLOAD_FILETYPE
permitted_upload_filetype = self_valid_extensions or settings_upload_filetype
max_upload_size = getattr(settings, 'MAX_UPLOAD_SIZE') if hasattr(settings, 'MAX_UPLOAD_SIZE') else MAX_UPLOAD_SIZE
attach_max_len = getattr(settings, 'ATTACH_NAME_MAX_LEN') if hasattr(settings, 'ATTACH_NAME_MAX_LEN') else ATTACH_NAME_MAX_LEN

Expand All @@ -181,6 +183,39 @@ def raise_error(self, name, cleaned_data, **kwargs):
return errors


class CustomImageField(CustomFileField):
"""
FileField
"""
field_type = _("Allegato Immagine")

def __init__(self, *args, **data_kwargs):
self.valid_extensions = getattr(settings, 'IMG_FILETYPE') if hasattr(settings, 'IMG_FILETYPE') else IMG_FILETYPE
super().__init__(*args, **data_kwargs)


class CustomDataField(CustomFileField):
"""
FileField
"""
field_type = _("Allegato file dati (JSON, CSV, Excel)")

def __init__(self, *args, **data_kwargs):
self.valid_extensions = getattr(settings, 'DATA_FILETYPE') if hasattr(settings, 'DATA_FILETYPE') else DATA_FILETYPE
super().__init__(*args, **data_kwargs)


class CustomPDFField(CustomFileField):
"""
FileField
"""
field_type = _("Allegato PDF")

def __init__(self, *args, **data_kwargs):
self.valid_extensions = getattr(settings, 'PDF_FILETYPE') if hasattr(settings, 'PDF_FILETYPE') else PDF_FILETYPE
super().__init__(*args, **data_kwargs)


class CustomSignedFileField(CustomFileField):
validation_error = _('Errore di validazione della firma digitale')
fileformat = ''
Expand Down Expand Up @@ -219,13 +254,21 @@ class CustomSignedPdfField(CustomSignedFileField):
field_type = _("Allegato PDF firmato")
fileformat = 'pdf'

def __init__(self, *args, **data_kwargs):
self.valid_extensions = getattr(settings, 'PDF_FILETYPE') if hasattr(settings, 'PDF_FILETYPE') else PDF_FILETYPE
super().__init__(*args, **data_kwargs)


class CustomSignedP7MField(CustomSignedFileField):
"""
"""
field_type = _("Allegato P7M firmato")
fileformat = 'p7m'

def __init__(self, *args, **data_kwargs):
self.valid_extensions = getattr(settings, 'P7M_FILETYPE') if hasattr(settings, 'P7M_FILETYPE') else P7M_FILETYPE
super().__init__(*args, **data_kwargs)


class PositiveIntegerField(DecimalField, BaseCustomField):
"""
Expand Down
20 changes: 16 additions & 4 deletions django_form_builder/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,22 @@
# 250MB - 214958080
# 500MB - 429916160
MAX_UPLOAD_SIZE = 10485760
IMG_PERMITTED_UPLOAD_FILETYPE = ("image/jpeg", "image/png",
"image/gif", "image/x-ms-bmp")
BASE_FILETYPE = ('application/pdf','application/pkcs7-mime')
PERMITTED_UPLOAD_FILETYPE = BASE_FILETYPE + IMG_PERMITTED_UPLOAD_FILETYPE

PDF_FILETYPE = ('application/pdf',)
DATA_FILETYPE = ('text/csv', 'application/json',
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.oasis.opendocument.spreadsheet',
)
TEXT_FILETYPE = ('text/plain',
'application/vnd.oasis.opendocument.text',
'application/msword,'
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
)
IMG_FILETYPE = ('image/jpeg', 'image/png', 'image/gif', 'image/x-ms-bmp')
P7M_FILETYPE = ('application/pkcs7-mime',)
SIGNED_FILETYPE = PDF_FILETYPE + P7M_FILETYPE
PERMITTED_UPLOAD_FILETYPE = TEXT_FILETYPE + DATA_FILETYPE + SIGNED_FILETYPE + IMG_FILETYPE

# maximum permitted filename lengh in attachments, uploads
ATTACH_NAME_MAX_LEN = 50
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name='django-form-builder',
version='0.8.2',
version='0.8.3',
packages=find_packages(),
include_package_data=True,
license='BSD License',
Expand Down

0 comments on commit 20038cb

Please sign in to comment.