Notify when import completes
This commit is contained in:
@ -38,13 +38,18 @@ class ImportJob(models.Model):
|
||||
include_reviews = models.BooleanField(default=True)
|
||||
mappings = models.JSONField()
|
||||
updated_date = models.DateTimeField(default=timezone.now)
|
||||
completed_count = models.IntegerField(default=0)
|
||||
complete = models.BooleanField(default=False)
|
||||
source = models.CharField(max_length=100)
|
||||
privacy = models.CharField(
|
||||
max_length=255, default="public", choices=PrivacyLevels.choices
|
||||
)
|
||||
retry = models.BooleanField(default=False)
|
||||
|
||||
@property
|
||||
def pending_items(self):
|
||||
"""items that haven't been processed yet"""
|
||||
return self.items.filter(fail_reason__isnull=True, book__isnull=True)
|
||||
|
||||
|
||||
class ImportItem(models.Model):
|
||||
"""a single line of a csv being imported"""
|
||||
@ -67,10 +72,13 @@ class ImportItem(models.Model):
|
||||
)
|
||||
|
||||
def update_job(self):
|
||||
"""this user is here! they are doing things!"""
|
||||
self.job.completed_count += 1
|
||||
self.job.updated_date = timezone.now()
|
||||
self.job.save()
|
||||
"""let the job know when the items get work done"""
|
||||
job = self.job
|
||||
job.updated_date = timezone.now()
|
||||
job.save()
|
||||
if not job.pending_items.exists() and not job.complete:
|
||||
job.complete = True
|
||||
job.save(update_fields=["complete"])
|
||||
|
||||
def resolve(self):
|
||||
"""try various ways to lookup a book"""
|
||||
|
@ -157,9 +157,12 @@ def notify_user_on_unboost(sender, instance, *args, **kwargs):
|
||||
|
||||
@receiver(models.signals.post_save, sender=ImportJob)
|
||||
# pylint: disable=unused-argument
|
||||
def notify_user_on_import_complete(sender, instance, *args, **kwargs):
|
||||
def notify_user_on_import_complete(
|
||||
sender, instance, *args, update_fields=None, **kwargs
|
||||
):
|
||||
"""we imported your books! aren't you proud of us"""
|
||||
if not instance.complete:
|
||||
update_fields = update_fields or []
|
||||
if not instance.complete or "complete" not in update_fields:
|
||||
return
|
||||
Notification.objects.create(
|
||||
user=instance.user,
|
||||
|
Reference in New Issue
Block a user