From b8bff8a882a3f7d62ee7677a4a008a2823053c18 Mon Sep 17 00:00:00 2001 From: Joachim Date: Thu, 27 May 2021 21:09:57 +0200 Subject: [PATCH] Use field tracker to launch task on field change --- bookwyrm/models/book.py | 8 ++++++-- bookwyrm/models/site.py | 7 +++++-- bookwyrm/models/status.py | 10 ++++++++-- bookwyrm/models/user.py | 6 ++++-- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py index aa9a56017..398517bab 100644 --- a/bookwyrm/models/book.py +++ b/bookwyrm/models/book.py @@ -3,6 +3,7 @@ import re from django.db import models from django.dispatch import receiver +from model_utils import FieldTracker from model_utils.managers import InheritanceManager from bookwyrm import activitypub @@ -92,6 +93,7 @@ class Book(BookDataModel): published_date = fields.DateTimeField(blank=True, null=True) objects = InheritanceManager() + field_tracker = FieldTracker(fields=['authors', 'title', 'subtitle', 'cover']) @property def author_text(self): @@ -304,7 +306,9 @@ def isbn_13_to_10(isbn_13): @receiver(models.signals.post_save, sender=Edition) # pylint: disable=unused-argument def preview_image(instance, *args, **kwargs): - updated_fields = kwargs["update_fields"] + changed_fields = {} + if instance.field_tracker: + changed_fields = instance.field_tracker.changed() - if not updated_fields or "preview_image" not in updated_fields: + if len(changed_fields) > 0: generate_edition_preview_image_task.delay(instance.id) diff --git a/bookwyrm/models/site.py b/bookwyrm/models/site.py index d0076dc63..c9c015e1e 100644 --- a/bookwyrm/models/site.py +++ b/bookwyrm/models/site.py @@ -6,6 +6,7 @@ from Crypto import Random from django.db import models, IntegrityError from django.dispatch import receiver from django.utils import timezone +from model_utils import FieldTracker from bookwyrm.preview_images import generate_site_preview_image_task from bookwyrm.settings import DOMAIN @@ -48,6 +49,8 @@ class SiteSettings(models.Model): admin_email = models.EmailField(max_length=255, null=True, blank=True) footer_item = models.TextField(null=True, blank=True) + field_tracker = FieldTracker(fields=['name', 'instance_tagline', 'logo']) + @classmethod def get(cls): """gets the site settings db entry or defaults""" @@ -130,7 +133,7 @@ class PasswordReset(models.Model): @receiver(models.signals.post_save, sender=SiteSettings) # pylint: disable=unused-argument def preview_image(instance, *args, **kwargs): - updated_fields = kwargs["update_fields"] + changed_fields = instance.field_tracker.changed() - if not updated_fields or "preview_image" not in updated_fields: + if len(changed_fields) > 0: generate_site_preview_image_task.delay() diff --git a/bookwyrm/models/status.py b/bookwyrm/models/status.py index c55cd8d69..feedc02a5 100644 --- a/bookwyrm/models/status.py +++ b/bookwyrm/models/status.py @@ -8,6 +8,7 @@ from django.db import models from django.dispatch import receiver from django.template.loader import get_template from django.utils import timezone +from model_utils import FieldTracker from model_utils.managers import InheritanceManager from bookwyrm import activitypub @@ -306,6 +307,8 @@ class Review(Status): max_digits=3, ) + field_tracker = FieldTracker(fields=['rating']) + @property def pure_name(self): """clarify review names for mastodon serialization""" @@ -406,5 +409,8 @@ class Boost(ActivityMixin, Status): # pylint: disable=unused-argument def preview_image(instance, sender, *args, **kwargs): if sender in (Review, ReviewRating): - edition = instance.book - generate_edition_preview_image_task.delay(edition.id) + changed_fields = instance.field_tracker.changed() + + if len(changed_fields) > 0: + edition = instance.book + generate_edition_preview_image_task.delay(edition.id) diff --git a/bookwyrm/models/user.py b/bookwyrm/models/user.py index 2c4851520..f2c700b6c 100644 --- a/bookwyrm/models/user.py +++ b/bookwyrm/models/user.py @@ -9,6 +9,7 @@ from django.core.validators import MinValueValidator from django.dispatch import receiver from django.db import models from django.utils import timezone +from model_utils import FieldTracker import pytz from bookwyrm import activitypub @@ -122,6 +123,7 @@ class User(OrderedCollectionPageMixin, AbstractUser): name_field = "username" property_fields = [("following_link", "following")] + field_tracker = FieldTracker(fields=['name', 'avatar']) @property def following_link(self): @@ -453,7 +455,7 @@ def get_remote_reviews(outbox): @receiver(models.signals.post_save, sender=User) # pylint: disable=unused-argument def preview_image(instance, *args, **kwargs): - updated_fields = kwargs["update_fields"] + changed_fields = instance.field_tracker.changed() - if not updated_fields or "preview_image" not in updated_fields: + if len(changed_fields) > 0: generate_user_preview_image_task.delay(instance.id)