Changeset View
Changeset View
Standalone View
Standalone View
static_assets/models/static_assets.py
| import datetime | |||||
| from typing import Optional | from typing import Optional | ||||
| import datetime | |||||
| import logging | import logging | ||||
| import mimetypes | |||||
| from django.conf import settings | from django.conf import settings | ||||
| from django.contrib.auth.models import User | from django.contrib.auth.models import User | ||||
| from django.core.exceptions import ValidationError | from django.core.exceptions import ValidationError | ||||
| from django.core.files.storage import FileSystemStorage | from django.core.files.storage import FileSystemStorage | ||||
| from django.db import models | from django.db import models | ||||
| from django.db.models import FileField | from django.db.models import FileField | ||||
| from django.db.models.fields.files import FieldFile | from django.db.models.fields.files import FieldFile | ||||
| ▲ Show 20 Lines • Show All 153 Lines • ▼ Show 20 Lines | def clean(self): | ||||
| super().clean() | super().clean() | ||||
| if not self.pk and self.source: | if not self.pk and self.source: | ||||
| # Save the original filename only on asset creation. | # Save the original filename only on asset creation. | ||||
| self.original_filename = self.source.file.name | self.original_filename = self.source.file.name | ||||
| if self.source: | if self.source: | ||||
| # The `if` prevents an unhandled exception if one tries to save without a source | # The `if` prevents an unhandled exception if one tries to save without a source | ||||
| self.size_bytes = self.source.size | self.size_bytes = self.source.size | ||||
| if not self.source_type: | |||||
| content_type, _ = mimetypes.guess_type(self.original_filename) | |||||
| if 'image' in content_type: | |||||
| self.source_type = StaticAssetFileTypeChoices.image | |||||
| elif 'video' in content_type: | |||||
| self.source_type = StaticAssetFileTypeChoices.video | |||||
| if self.source_type == StaticAssetFileTypeChoices.file and not self.thumbnail: | if self.source_type == StaticAssetFileTypeChoices.file and not self.thumbnail: | ||||
| raise ValidationError( | raise ValidationError( | ||||
| f'Source preview has to be provided for `{StaticAssetFileTypeChoices.file}` source type.' | f'Source preview has to be provided for `{StaticAssetFileTypeChoices.file}` source type.' | ||||
| ) | ) | ||||
| def save(self, *args, **kwargs): | def save(self, *args, **kwargs): | ||||
| created = self.pk is None | created = self.pk is None | ||||
| ▲ Show 20 Lines • Show All 93 Lines • Show Last 20 Lines | |||||