mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 17:51: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
reviews/mixins.py
Normal file
19
reviews/mixins.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db.models import QuerySet
|
||||
|
||||
class ReviewableMixin:
|
||||
"""Mixin for models that can have reviews."""
|
||||
|
||||
def get_reviews(self) -> QuerySet:
|
||||
"""Get reviews for this instance."""
|
||||
from reviews.models import Review
|
||||
ct = ContentType.objects.get_for_model(self.__class__)
|
||||
return Review.objects.filter(content_type=ct, object_id=self.pk)
|
||||
|
||||
def add_review(self, review: 'Review') -> None:
|
||||
"""Add a review to this instance."""
|
||||
from reviews.models import Review
|
||||
ct = ContentType.objects.get_for_model(self.__class__)
|
||||
review.content_type = ct
|
||||
review.object_id = self.pk
|
||||
review.save()
|
||||
Reference in New Issue
Block a user