forked from mirrors/bookwyrm
Fixing up notifications.
This commit is contained in:
parent
0bf73fef24
commit
22b2a95fd3
6 changed files with 80 additions and 33 deletions
|
@ -5,7 +5,7 @@ from requests import HTTPError
|
|||
from fedireads import outgoing
|
||||
from fedireads.tasks import app
|
||||
from fedireads.models import ImportJob, ImportItem
|
||||
|
||||
from fedireads.status import create_notification
|
||||
|
||||
# TODO: remove or notify about this in the UI
|
||||
MAX_ENTRIES = 20
|
||||
|
@ -25,32 +25,34 @@ def start_import(job):
|
|||
@app.task
|
||||
def import_data(job_id):
|
||||
job = ImportJob.objects.get(id=job_id)
|
||||
user = job.user
|
||||
results = []
|
||||
reviews = []
|
||||
for item in job.items.all():
|
||||
try:
|
||||
item.resolve()
|
||||
except HTTPError:
|
||||
pass
|
||||
if item.book:
|
||||
item.save()
|
||||
results.append(item)
|
||||
if item.rating or item.review:
|
||||
reviews.append(item)
|
||||
else:
|
||||
item.fail_reason = "Could not match book on OpenLibrary"
|
||||
item.save()
|
||||
try:
|
||||
results = []
|
||||
reviews = []
|
||||
for item in job.items.all():
|
||||
try:
|
||||
item.resolve()
|
||||
except HTTPError:
|
||||
pass
|
||||
if item.book:
|
||||
item.save()
|
||||
results.append(item)
|
||||
if item.rating or item.review:
|
||||
reviews.append(item)
|
||||
else:
|
||||
item.fail_reason = "Could not match book on OpenLibrary"
|
||||
item.save()
|
||||
|
||||
outgoing.handle_import_books(user, results)
|
||||
for item in reviews:
|
||||
review_title = "Review of {!r} on Goodreads".format(
|
||||
item.book.title,
|
||||
) if item.review else ""
|
||||
outgoing.handle_review(
|
||||
user,
|
||||
item.book,
|
||||
review_title,
|
||||
item.review,
|
||||
item.rating,
|
||||
)
|
||||
outgoing.handle_import_books(job.user, results)
|
||||
for item in reviews:
|
||||
review_title = "Review of {!r} on Goodreads".format(
|
||||
item.book.title,
|
||||
) if item.review else ""
|
||||
outgoing.handle_review(
|
||||
job.user,
|
||||
item.book,
|
||||
review_title,
|
||||
item.review,
|
||||
item.rating,
|
||||
)
|
||||
finally:
|
||||
create_notification(job.user, 'IMPORT', related_import=job)
|
||||
|
|
43
fedireads/migrations/0033_auto_20200422_1249.py
Normal file
43
fedireads/migrations/0033_auto_20200422_1249.py
Normal file
|
@ -0,0 +1,43 @@
|
|||
# Generated by Django 3.0.3 on 2020-04-22 12:49
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('fedireads', '0032_auto_20200421_1347'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveConstraint(
|
||||
model_name='notification',
|
||||
name='notification_type_valid',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='importitem',
|
||||
name='fail_reason',
|
||||
field=models.TextField(null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='importitem',
|
||||
name='index',
|
||||
field=models.IntegerField(default=1),
|
||||
preserve_default=False,
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='notification',
|
||||
name='related_import',
|
||||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='fedireads.ImportJob'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='notification',
|
||||
name='notification_type',
|
||||
field=models.CharField(choices=[('FAVORITE', 'Favorite'), ('REPLY', 'Reply'), ('TAG', 'Tag'), ('FOLLOW', 'Follow'), ('FOLLOW_REQUEST', 'Follow Request'), ('BOOST', 'Boost'), ('IMPORT', 'Import')], max_length=255),
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name='notification',
|
||||
constraint=models.CheckConstraint(check=models.Q(notification_type__in=['FAVORITE', 'REPLY', 'TAG', 'FOLLOW', 'FOLLOW_REQUEST', 'BOOST', 'IMPORT']), name='notification_type_valid'),
|
||||
),
|
||||
]
|
|
@ -151,7 +151,7 @@ class ReadThrough(FedireadsModel):
|
|||
|
||||
NotificationType = models.TextChoices(
|
||||
'NotificationType',
|
||||
'FAVORITE REPLY TAG FOLLOW FOLLOW_REQUEST BOOST IMPORT_RESULT')
|
||||
'FAVORITE REPLY TAG FOLLOW FOLLOW_REQUEST BOOST IMPORT')
|
||||
|
||||
class Notification(FedireadsModel):
|
||||
''' you've been tagged, liked, followed, etc '''
|
||||
|
@ -163,6 +163,8 @@ class Notification(FedireadsModel):
|
|||
on_delete=models.PROTECT, null=True, related_name='related_user')
|
||||
related_status = models.ForeignKey(
|
||||
'Status', on_delete=models.PROTECT, null=True)
|
||||
related_import = models.ForeignKey(
|
||||
'ImportJob', on_delete=models.PROTECT, null=True)
|
||||
read = models.BooleanField(default=False)
|
||||
notification_type = models.CharField(
|
||||
max_length=255, choices=NotificationType.choices)
|
||||
|
|
|
@ -203,7 +203,6 @@ def handle_import_books(user, items):
|
|||
status.status_type = 'Update'
|
||||
status.save()
|
||||
|
||||
create_notification(user, 'IMPORT_RESULT', related_status=status)
|
||||
create_activity = activitypub.get_create(
|
||||
user, activitypub.get_status(status))
|
||||
broadcast(user, create_activity)
|
||||
|
|
|
@ -234,7 +234,7 @@ def create_tag(user, possible_book, name):
|
|||
|
||||
|
||||
def create_notification(user, notification_type, related_user=None, \
|
||||
related_book=None, related_status=None):
|
||||
related_book=None, related_status=None, related_import=None):
|
||||
''' let a user know when someone interacts with their content '''
|
||||
if user == related_user:
|
||||
# don't create notification when you interact with your own stuff
|
||||
|
@ -244,6 +244,7 @@ def create_notification(user, notification_type, related_user=None, \
|
|||
related_book=related_book,
|
||||
related_user=related_user,
|
||||
related_status=related_status,
|
||||
related_import=related_import,
|
||||
notification_type=notification_type,
|
||||
)
|
||||
|
||||
|
|
|
@ -38,7 +38,7 @@
|
|||
boosted your <a href="{{ notification.related_status.absolute_id}}">status</a>
|
||||
{% endif %}
|
||||
{% else %}
|
||||
your <a href="{{ notification.related_status.absolute_id }}">import</a> succeeded.
|
||||
your <a href="/import_status/{{ notification.related_import.id }}">import</a> completed.
|
||||
|
||||
{% endif %}
|
||||
</div>
|
||||
|
|
Loading…
Reference in a new issue