Use field tracker to launch task on field change

This commit is contained in:
Joachim 2021-05-27 21:09:57 +02:00
parent 2ea1cd8319
commit b8bff8a882
4 changed files with 23 additions and 8 deletions

View file

@ -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)

View file

@ -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()

View file

@ -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):
changed_fields = instance.field_tracker.changed()
if len(changed_fields) > 0:
edition = instance.book
generate_edition_preview_image_task.delay(edition.id)

View file

@ -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)