diff --git a/adim_project/adim_app/forms.py b/adim_project/adim_app/forms.py index fd76910b9e59fc724e723bd921978a204c3ade9c..f045ab6a9d4d02c19c5ee61485591acabb18a26d 100644 --- a/adim_project/adim_app/forms.py +++ b/adim_project/adim_app/forms.py @@ -1,14 +1,33 @@ # coding=utf-8 from __future__ import unicode_literals from django import forms +from django.forms.utils import ErrorDict + +from utils.validators import validate_json_file class UploadImageFileForm(forms.Form): + """ This form is used to validate the image files submitted for upload """ - image_file = forms.ImageField(allow_empty_file=False) + image_file = forms.ImageField(allow_empty_file=False, validators=[validate_json_file]) name = forms.CharField(max_length=125, required=False) sharing_opts = None - allow_public_publishing = None \ No newline at end of file + allow_public_publishing = None + + def _clean_fields(self): + super(UploadImageFileForm,self)._clean_fields() + + # Add control json import + errors = self.errors + if errors and errors.get('image_file'): + fileError = errors.get('image_file') + if fileError: + data = fileError.data + if data and len(data) == 1: + validatorError = data[0] + paramserror = validatorError.params + if validatorError.code == "invalid_extension" and paramserror['extension'] == "" or validatorError.code == "invalid_json_extension": + self._errors = ErrorDict() \ No newline at end of file diff --git a/adim_project/adim_app/utils/validators.py b/adim_project/adim_app/utils/validators.py new file mode 100644 index 0000000000000000000000000000000000000000..73b794514eaeee661437c3656a1114760d2a27cf --- /dev/null +++ b/adim_project/adim_app/utils/validators.py @@ -0,0 +1,32 @@ +from django.core.exceptions import ValidationError +from django.utils.translation import gettext_lazy as _ + +def validate_json_file(value): + message = _( + "File extension '%(extension)s' is not allowed. " + "Allowed extensions are: '%(allowed_extensions)s'." + ) + code = 'invalid_json_extension' + + if value.content_type : + options = { + # the file types which are going to be allowed for upload + # must be a mimetype + "acceptedformats": ( + "image/jpeg", + "image/jpg", + "image/png", + ) + } + + # allowed file type + extension = value.content_type + if extension not in options["acceptedformats"]: + raise ValidationError( + message, + code=code, + params={ + 'extension': extension, + 'allowed_extensions': ', '.join(options["acceptedformats"]) + } + ) \ No newline at end of file diff --git a/adim_project/adim_app/views.py b/adim_project/adim_app/views.py index 7f1736a4bb5434b6f969f116ca5610acfe4c8fff..5cee70b147c6a7792035e22d55cb3194f0c9c850 100644 --- a/adim_project/adim_app/views.py +++ b/adim_project/adim_app/views.py @@ -30,10 +30,8 @@ from .forms import UploadImageFileForm from sendfile import sendfile from .utils import add_image_border, create_image_thumbnail - logger = logging.getLogger(__name__) - def home(request): """ Home page @@ -229,15 +227,15 @@ def annotate(request, anobj_uuid=None): owner_membership = None context.update({'display_shared_annotations': - (anobj.sharing_mode != SHARING_MODE_NONE) and - ( - anobj.is_owned(request.user.id) or - anobj.allow_public_publishing or - ( - owner_membership and owner_membership.publish_mode == 2 - ) - ) - }) + (anobj.sharing_mode != SHARING_MODE_NONE) and + ( + anobj.is_owned(request.user.id) or + anobj.allow_public_publishing or + ( + owner_membership and owner_membership.publish_mode == 2 + ) + ) + }) # ----- Environment specific settings template_path = ['adim'] @@ -323,10 +321,13 @@ def upload_file(request, anobj_uuid=None): response_data['error'] = "invalid" if 'application/json' in request.META.get('HTTP_ACCEPT', ''): - return HttpResponse(json.dumps(response_data), content_type=response_type) + httpResponse = HttpResponse(json.dumps(response_data), content_type=response_type) + if response_data['error'] == "invalid": + httpResponse.status_code=400 + return httpResponse else: return HttpResponseRedirect(response_data['next']) - + def _validate_uploaded_file(image_file): options = { diff --git a/adim_project/templates/base.html b/adim_project/templates/base.html index 4518f61735c8bb46d5ebb34f294d202ea177de19..d6b4352832165971f398e18579f98a45fc75e968 100644 --- a/adim_project/templates/base.html +++ b/adim_project/templates/base.html @@ -21,7 +21,7 @@