Coverage for website/photos/validators.py: 62.50%
18 statements
« prev ^ index » next coverage.py v7.6.7, created at 2025-08-14 10:31 +0000
« prev ^ index » next coverage.py v7.6.7, created at 2025-08-14 10:31 +0000
1from django.core.exceptions import ValidationError
2from django.utils.deconstruct import deconstructible
3from django.utils.translation import gettext_lazy as _
5import puremagic as magic
8@deconstructible
9class ArchiveFileTypeValidator:
10 """Validator class for archive files."""
12 types = ["application/gzip", "application/zip", "application/x-gzip"]
13 message = _("Only zip and tar files are allowed.")
15 def __init__(self, types=None, message=None):
16 """Initialize ArchiveFileTypeValidator with allowed types and warning message."""
17 if types is not None: 17 ↛ 18line 17 didn't jump to line 18 because the condition on line 17 was never true
18 self.types = types
19 if message is not None: 19 ↛ 20line 19 didn't jump to line 20 because the condition on line 19 was never true
20 self.message = message
22 def __call__(self, value):
23 """Validate that the input contains (or does *not* contain, if inverse_match is True) a match for the regular expression."""
24 if (
25 magic.from_stream(
26 value.temporary_upload.file,
27 mime=True,
28 filename=value.temporary_upload.upload_name,
29 )
30 not in self.types
31 ):
32 raise ValidationError(self.message)
34 def __eq__(self, other):
35 """Check whether the current object and other are equal."""
36 return (
37 isinstance(other, ArchiveFileTypeValidator)
38 and self.types == other.types
39 and (self.message == other.message)
40 )