Gitlab CSE Unil

validators.py 1002 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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"])
                }
            )