diff options
author | Weitian LI <liweitianux@gmail.com> | 2014-04-27 01:58:27 +0800 |
---|---|---|
committer | Weitian LI <liweitianux@gmail.com> | 2014-04-27 01:58:27 +0800 |
commit | c4d4764da9eb15322ab4207b5efd5f3dac36d75f (patch) | |
tree | d9f41e9f107d4ad5594f1e1d707063761486671b | |
parent | e166ea9dd833f5d22f8a32d36fd4ed539c902410 (diff) | |
download | django-skaschool-c4d4764da9eb15322ab4207b5efd5f3dac36d75f.tar.bz2 |
* updated ALLOWED_CONTENT_TYPES
* implemented ContentTypeRestrictedFileField based on FileField
-rw-r--r-- | account/extra.py | 50 | ||||
-rw-r--r-- | django_skaschool/settings.py | 44 |
2 files changed, 81 insertions, 13 deletions
diff --git a/account/extra.py b/account/extra.py new file mode 100644 index 0000000..c247b8f --- /dev/null +++ b/account/extra.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- + +""" +Extra models for app account +""" + +from django.db import models +from django import forms +from django.template.defaultfilters import filesizeformat +from django.utils.translation import ugettext_lazy as _ + +class ContentTypeRestrictedFileField(models.FileField): + """ + Same as FileField, but you can specify: + * content_types - list containing allowed content_types. + Example: ['application/pdf', 'image/jpeg'] + * max_upload_size - a number indicating the maximum file + size allowed for upload. + 2.5MB - 2621440 + 5MB - 5242880 + 10MB - 10485760 + 20MB - 20971520 + 50MB - 52428800 + 100MB - 104857600 + 250MB - 214958080 + 500MB - 429916160 + """ + def __init__(self, *args, **kwargs): + self.content_types = kwargs.pop("content_types") + self.max_upload_size = kwargs.pop("max_upload_size") + super(ContentTypeRestrictedFileField, self).__init__(*args, **kwargs) + + def clean(self, *args, **kwargs): + data = super(ContentTypeRestrictedFileField, self).clean(*args, **kwargs) + file = data.file + # check content type and file size + try: + content_type = file.content_type + #print content_type + #raise forms.ValidationError(_("Invalid filetype."), code='invalid') + if content_type in self.content_types: + if file._size > self.max_upload_size: + raise forms.ValidationError(_('Please keep filesize under %s. Current filesize %s') % (filesizeformat(self.max_upload_size), filesizeformat(file._size)), code='invalid') + else: + raise forms.ValidationError(_("Invalid filetype."), code='invalid') + except AttributeError: + pass + # + return data + diff --git a/django_skaschool/settings.py b/django_skaschool/settings.py index 0a8326d..f64e098 100644 --- a/django_skaschool/settings.py +++ b/django_skaschool/settings.py @@ -175,25 +175,43 @@ RECAPTCHA_PRIVATE_KEY = '6Lf8dvISAAAAAH75mmLlVWOp6JB9Gx6WARR_6HXb' ################################################# ## allowed content types to be uploaded by user ALLOWED_CONTENT_TYPES = [ - 'application/octet-stream', # arbitrary binary data: doc, ppt, etc. - 'application/pdf', # pdf - 'application/postscript', # postscript - 'application/zip', # zip - 'application/gzip', # gzip + 'application/gzip', # gzip + 'application/msword', # doc + 'application/pdf', # pdf + 'application/postscript', # postscript + 'application/rar', # rar + 'application/vnd.oasis.opendocument.spreadsheet', # ods + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', # xlsx + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', # docx + 'application/wps-office.doc', # wps doc/rtf + 'application/wps-office.dps', # wps dps + 'application/wps-office.et', # wps et + 'application/wps-office.ppt', # wps ppt + 'application/wps-office.pptx', # wps pptx + 'application/wps-office.wps', # wps wps + 'application/wps-office.xls', # wps xls + 'application/zip', # zip 'application/x-7z-compressed', # 7z + 'application/x-bzip2', # bz2 'application/x-dvi', # dvi 'application/x-latex', # latex 'application/x-rar-compressed', # rar 'application/x-tar', # tar - 'image/gif', # gif - 'image/jpeg', # jpg - 'image/png', # png - 'text/html', # html - 'text/plain', # txt - 'text/rtf', # rtf - 'text/xml', # xml - 'text/x-markdown', # markdown + 'image/bmp', # bmp + 'image/gif', # gif + 'image/jpeg', # jpg + 'image/png', # png + 'image/tiff', # tif + 'text/csv', # csv + 'text/html', # html + 'text/plain', # txt + 'text/rtf', # rtf + 'text/xml', # xml + 'text/x-markdown', # markdown + 'text/x-tex', # latex ] +## allowed filesize of uploaded files +ALLOWED_MAX_UPLOAD_SIZE = 10485760 # 10 MB # vim: set ts=4 sw=4 tw=0 fenc=utf-8 ft=python: |