mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-21 01:31:08 -05:00
Refactor comments app to use mixins for comment functionality; update admin interfaces and add historical model fixes
This commit is contained in:
19
media/mixins.py
Normal file
19
media/mixins.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db.models import QuerySet
|
||||
|
||||
class PhotoableModel:
|
||||
"""Mixin for models that can have photos attached."""
|
||||
|
||||
def get_photos(self) -> QuerySet:
|
||||
"""Get photos for this instance."""
|
||||
from media.models import Photo
|
||||
ct = ContentType.objects.get_for_model(self.__class__)
|
||||
return Photo.objects.filter(content_type=ct, object_id=self.pk)
|
||||
|
||||
def add_photo(self, photo: 'Photo') -> None:
|
||||
"""Add a photo to this instance."""
|
||||
from media.models import Photo
|
||||
ct = ContentType.objects.get_for_model(self.__class__)
|
||||
photo.content_type = ct
|
||||
photo.object_id = self.pk
|
||||
photo.save()
|
||||
Reference in New Issue
Block a user