Changeset View
Changeset View
Standalone View
Standalone View
static_assets/models/static_assets.py
| from typing import Optional | |||||
| import datetime | import datetime | ||||
| import logging | import logging | ||||
| import mimetypes | 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.db import models | from django.db import models | ||||
| from django.db.models import FileField | |||||
| from django.db.models.fields.files import FieldFile | |||||
| from django.urls.base import reverse | from django.urls.base import reverse | ||||
| from sorl.thumbnail import get_thumbnail | from sorl.thumbnail import get_thumbnail | ||||
| from storages.backends.gcloud import GoogleCloudStorage | |||||
| from common import mixins | from common import mixins | ||||
| from common.upload_paths import get_upload_to_hashed_path | from common.upload_paths import get_upload_to_hashed_path | ||||
| from static_assets.models import License, StorageLocationCategoryChoices | from static_assets.models import License | ||||
| from static_assets.tasks import create_video_processing_job | from static_assets.tasks import create_video_processing_job | ||||
| log = logging.getLogger(__name__) | log = logging.getLogger(__name__) | ||||
| class StaticAssetFileTypeChoices(models.TextChoices): | class StaticAssetFileTypeChoices(models.TextChoices): | ||||
| file = 'file', 'File' | file = 'file', 'File' | ||||
| image = 'image', 'Image' | image = 'image', 'Image' | ||||
| video = 'video', 'Video' | video = 'video', 'Video' | ||||
| class DynamicStorageFieldFile(FieldFile): | |||||
| def __init__(self, instance: models.Model, field: FileField, name: Optional[str]): | |||||
| super(DynamicStorageFieldFile, self).__init__(instance, field, name) | |||||
| assert hasattr(instance.__class__, 'storage_location'), ( | |||||
| f'{self.__class__.__name__} cannot be used in {instance.__class__.__name__}, ' | |||||
| f'which does not have the `storage_location` field.' | |||||
| ) | |||||
| if instance.storage_location_id: # type: ignore[attr-defined] | |||||
| # The `if` prevents an unhandled exception if one tries to save without a storage_location | |||||
| if instance.storage_location.category == StorageLocationCategoryChoices.gcs: # type: ignore[attr-defined] | |||||
| self.storage: GoogleCloudStorage = GoogleCloudStorage() | |||||
| if instance.storage_location.bucket_name: # type: ignore[attr-defined] | |||||
| self.storage.bucket_name = instance.storage_location.bucket_name # type: ignore[attr-defined] | |||||
| else: | |||||
| self.storage = FileSystemStorage() | |||||
| class DynamicStorageFileField(models.FileField): | class DynamicStorageFileField(models.FileField): | ||||
| attr_class = DynamicStorageFieldFile | pass # kept because migrations refer to this field | ||||
| def pre_save(self, model_instance: models.Model, add: bool) -> FieldFile: | |||||
| assert hasattr(model_instance, 'storage_location'), ( | |||||
| f'{self.__class__.__name__} cannot be used in {model_instance.__class__.__name__}, ' | |||||
| f'which does not have the `storage_location` field.' | |||||
| ) | |||||
| if model_instance.storage_location.category == StorageLocationCategoryChoices.gcs: # type: ignore[attr-defined] | |||||
| storage = GoogleCloudStorage() | |||||
| else: | |||||
| storage = FileSystemStorage() | |||||
| self.storage = storage | |||||
| file: FieldFile = super(DynamicStorageFileField, self).pre_save(model_instance, add) | |||||
| return file | |||||
| class StaticAsset(mixins.CreatedUpdatedMixin, mixins.StaticThumbnailURLMixin, models.Model): | class StaticAsset(mixins.CreatedUpdatedMixin, mixins.StaticThumbnailURLMixin, models.Model): | ||||
| class Meta: | class Meta: | ||||
| ordering = ['-date_created'] | ordering = ['-date_created'] | ||||
| source = models.FileField(upload_to=get_upload_to_hashed_path, blank=True, max_length=256) | source = models.FileField(upload_to=get_upload_to_hashed_path, blank=True, max_length=256) | ||||
| source_type = models.CharField(choices=StaticAssetFileTypeChoices.choices, max_length=5) | source_type = models.CharField(choices=StaticAssetFileTypeChoices.choices, max_length=5) | ||||
| ▲ Show 20 Lines • Show All 209 Lines • Show Last 20 Lines | |||||