Changeset View
Changeset View
Standalone View
Standalone View
common/mixins.py
| from typing import Optional, Any, Union, List, Tuple | from typing import Optional, Any, Union, List, Tuple | ||||
| from django.conf import settings | |||||
| from django.contrib import admin | from django.contrib import admin | ||||
| from django.db import models | from django.db import models | ||||
| from django.db.models.base import Model | from django.db.models.base import Model | ||||
| from django.http.request import HttpRequest | from django.http.request import HttpRequest | ||||
| from sorl.thumbnail import get_thumbnail | |||||
| class CreatedUpdatedMixin(models.Model): | class CreatedUpdatedMixin(models.Model): | ||||
| class Meta: | class Meta: | ||||
| abstract = True | abstract = True | ||||
| date_created = models.DateTimeField(auto_now_add=True) | date_created = models.DateTimeField(auto_now_add=True) | ||||
| date_updated = models.DateTimeField(auto_now=True) | date_updated = models.DateTimeField(auto_now=True) | ||||
| Show All 17 Lines | ) -> Union[List[str], Tuple[str]]: | ||||
| readonly_fields = ['user', *super().get_readonly_fields(request, obj)] | readonly_fields = ['user', *super().get_readonly_fields(request, obj)] | ||||
| return readonly_fields | return readonly_fields | ||||
| def save_model(self, request: Any, obj: Any, form: Any, change: Any) -> None: | def save_model(self, request: Any, obj: Any, form: Any, change: Any) -> None: | ||||
| """Associate created object with the current user.""" | """Associate created object with the current user.""" | ||||
| if not obj.pk: | if not obj.pk: | ||||
| obj.user = request.user | obj.user = request.user | ||||
| super().save_model(request, obj, form, change) | super().save_model(request, obj, form, change) | ||||
| class StaticThumbnailURLMixin: | |||||
| """Add `thumbnail_url` property generating a static cacheable thumbnail URL.""" | |||||
| @property | |||||
| def thumbnail_url(self) -> str: | |||||
| """Return a static (as opposed to dynamically generated signed) URL to a thumbnail.""" | |||||
| return get_thumbnail(self.thumbnail, settings.THUMBNAIL_SIZE).url | |||||