Gitlab CSE Unil

forms.py 1.14 KB
Newer Older
Julien Furrer's avatar
Julien Furrer committed
1
2
3
# coding=utf-8
from __future__ import unicode_literals
from django import forms
4
5
6
from django.forms.utils import ErrorDict

from utils.validators import validate_json_file
Julien Furrer's avatar
Julien Furrer committed
7
8
9


class UploadImageFileForm(forms.Form):
10

Julien Furrer's avatar
Julien Furrer committed
11
12
13
14
    """
    This form is used to validate the image files submitted
    for upload
    """
15
    image_file = forms.ImageField(allow_empty_file=False, validators=[validate_json_file])
16
17
    name = forms.CharField(max_length=125, required=False)
    sharing_opts = None
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    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()