Changeset View
Changeset View
Standalone View
Standalone View
static_assets/models/static_assets.py
| from typing import Optional | from typing import Optional | ||||
| 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 | ||||
| from sorl.thumbnail import get_thumbnail | |||||
| from storages.backends.gcloud import GoogleCloudStorage | 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, StorageLocationCategoryChoices | ||||
| class StaticAssetFileTypeChoices(models.TextChoices): | class StaticAssetFileTypeChoices(models.TextChoices): | ||||
| Show All 34 Lines | def pre_save(self, model_instance: models.Model, add: bool) -> FieldFile: | ||||
| storage = GoogleCloudStorage() | storage = GoogleCloudStorage() | ||||
| else: | else: | ||||
| storage = FileSystemStorage() | storage = FileSystemStorage() | ||||
| self.storage = storage | self.storage = storage | ||||
| file: FieldFile = super(DynamicStorageFileField, self).pre_save(model_instance, add) | file: FieldFile = super(DynamicStorageFileField, self).pre_save(model_instance, add) | ||||
| return file | return file | ||||
| class StaticAsset(mixins.CreatedUpdatedMixin, models.Model): | class StaticAsset(mixins.CreatedUpdatedMixin, mixins.StaticThumbnailURLMixin, models.Model): | ||||
| source = models.FileField(upload_to=get_upload_to_hashed_path) | source = models.FileField(upload_to=get_upload_to_hashed_path) | ||||
| source_type = models.CharField(choices=StaticAssetFileTypeChoices.choices, max_length=5) | source_type = models.CharField(choices=StaticAssetFileTypeChoices.choices, max_length=5) | ||||
| # TODO(Natalia): source type validation | # TODO(Natalia): source type validation | ||||
| original_filename = models.CharField(max_length=128, editable=False) | original_filename = models.CharField(max_length=128, editable=False) | ||||
| size_bytes = models.BigIntegerField(editable=False) | size_bytes = models.BigIntegerField(editable=False) | ||||
| user = models.ForeignKey( | user = models.ForeignKey( | ||||
| User, on_delete=models.PROTECT, related_name='uploaded_assets', verbose_name='created by' | User, on_delete=models.PROTECT, related_name='uploaded_assets', verbose_name='created by' | ||||
| Show All 23 Lines | class StaticAsset(mixins.CreatedUpdatedMixin, mixins.StaticThumbnailURLMixin, models.Model): | ||||
| def preview(self): | def preview(self): | ||||
| if self.thumbnail: | if self.thumbnail: | ||||
| return self.thumbnail | return self.thumbnail | ||||
| if self.source_type == StaticAssetFileTypeChoices.image: | if self.source_type == StaticAssetFileTypeChoices.image: | ||||
| return self.source | return self.source | ||||
| # TODO(Natalia): Update this once we have auto-generated thumbnails. | # TODO(Natalia): Update this once we have auto-generated thumbnails. | ||||
| @property | @property | ||||
| def thumbnail_m_url(self) -> str: | |||||
| """Return a static URL to a medium-sizes thumbnail.""" | |||||
| # TODO(anna): use StaticThumbnailURLMixin once thumbnails are generated | |||||
| preview = self.preview | |||||
| if not preview: | |||||
| return '' | |||||
| return get_thumbnail( | |||||
| preview, settings.THUMBNAIL_SIZE_M, crop=settings.THUMBNAIL_CROP_MODE | |||||
| ).url | |||||
| @property | |||||
| def thumbnail_s_url(self) -> str: | |||||
| """Return a static URL to a small thumbnail.""" | |||||
| # TODO(anna): use StaticThumbnailURLMixin once thumbnails are generated | |||||
| preview = self.preview | |||||
| if not preview: | |||||
| return '' | |||||
| return get_thumbnail( | |||||
| preview, settings.THUMBNAIL_SIZE_S, crop=settings.THUMBNAIL_CROP_MODE | |||||
| ).url | |||||
| @property | |||||
| def author_name(self) -> str: | def author_name(self) -> str: | ||||
| """Get the asset's author full name. | """Get the asset's author full name. | ||||
| Usually the author of the asset will be the same as the user who uploads the asset.""" | Usually the author of the asset will be the same as the user who uploads the asset.""" | ||||
| if self.author: | if self.author: | ||||
| return self.author.get_full_name() | return self.author.get_full_name() | ||||
| return self.user.get_full_name() | return self.user.get_full_name() | ||||
| ▲ Show 20 Lines • Show All 41 Lines • Show Last 20 Lines | |||||