Source code for apps.posts.validators

from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.files.images import get_image_dimensions
from django.utils.deconstruct import deconstructible
from django.utils.translation import gettext_lazy as _

size = {
    "2.5MB": 2621440,
    "5MB": 5242880,
    "10MB": 10485760,
    "20MB": 20971520,
    "50MB": 5242880,
    "100MB": 104857600,
    "250MB": 214958080,
    "500MB": 429916160,
    "1GB": 859832320,
}


[docs] @deconstructible class FileSizeValidator: message = _("The maximum file size that can be uploaded is %(size)s.") code = "invalid_size"
[docs] def __init__(self, max_size_name=None): max_size = size.get(max_size_name) self.max_size_name = max_size_name self.max_size = max_size
def __call__(self, value): if self.max_size is not None and value.size > self.max_size: raise ValidationError(self.message, code=self.code, params={"size": self.max_size_name}) def __eq__(self, other): return ( isinstance(other, self.__class__) and self.max_size == other.max_size and self.message == other.message and self.code == other.code )
[docs] @deconstructible class ImageResolutionValidator: message = _("Invalid resolution.") code = "invalid_image_resolution"
[docs] def __init__(self, min_width, min_height): self.min_width = min_width self.min_height = min_height
def __call__(self, value): width, height = get_image_dimensions(value) if width < self.min_width or height < self.min_height: raise ValidationError(self.message, code=self.code) def __eq__(self, other): return ( isinstance(other, self.__class__) and self.min_width == other.min_width and self.min_height == other.min_height and self.message == other.message and self.code == other.code )
[docs] @deconstructible class VideoResolutionValidator: message = _("Invalid resolution.") code = "invalid_video_resolution"
[docs] def __init__( self, min_width_horizontal=settings.VIDEO_HORIZONTAL_MIN_RESOLUTION_WIDTH, min_height_horizontal=settings.VIDEO_HORIZONTAL_MIN_RESOLUTION_HEIGHT, max_width_horizontal=settings.IMAGE_MAX_RESOLUTION_WIDTH, min_width_vertical=settings.VIDEO_VERTICAL_MIN_RESOLUTION_WIDTH, min_height_vertical=settings.VIDEO_VERTICAL_MIN_RESOLUTION_HEIGHT, max_width_vertical=settings.VIDEO_VERTICAL_MAX_RESOLUTION_HEIGHT, ): self.min_width_horizontal = min_width_horizontal self.min_height_horizontal = min_height_horizontal self.max_width_horizontal = max_width_horizontal self.min_width_vertical = min_width_vertical self.min_height_vertical = min_height_vertical self.max_height_vertical = max_width_vertical
def __call__(self, value): width, height = value.size if ( width < self.min_width_horizontal or height < self.min_height_horizontal or width > self.max_width_horizontal ): raise ValidationError(self.message, code=self.code) # else: # if ( # width < self.min_width_vertical # or height < self.min_height_vertical # or height > self.max_height_vertical # ): # raise ValidationError(self.message, code=self.code) def __eq__(self, other): return ( isinstance(other, self.__class__) and self.min_width == other.min_width and self.min_height == other.min_height and self.max_width == other.max_width and self.message == other.message and self.code == other.code )
[docs] @deconstructible class VideoDurationValidator: message = _("The max video duration can be less that %(duration)s minutes.") code = "invalid_duration"
[docs] def __init__(self, max_duration): self.max_duration = max_duration
def __call__(self, value): duration = value.duration if duration > self.max_duration: raise ValidationError(self.message, code=self.code, params={"duration": int(self.max_duration / 60)}) def __eq__(self, other): return ( isinstance(other, self.__class__) and self.max_duration == other.max_duration and self.message == other.message and self.code == other.code )