From 12220a9e18a90d7c935a600b099e1cd0333dc703 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Pasquier Date: Mon, 16 Nov 2020 11:44:15 +0100 Subject: [PATCH] minor: notification settings per user --- .../migrations/0010_notificationsetting.py | 38 +++++++++++++++++ djangoldp_notification/models.py | 41 +++++++++++++++---- djangoldp_notification/settings.py | 2 +- 3 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 djangoldp_notification/migrations/0010_notificationsetting.py diff --git a/djangoldp_notification/migrations/0010_notificationsetting.py b/djangoldp_notification/migrations/0010_notificationsetting.py new file mode 100644 index 0000000..3c0f147 --- /dev/null +++ b/djangoldp_notification/migrations/0010_notificationsetting.py @@ -0,0 +1,38 @@ +# Generated by Django 2.2.16 on 2020-11-16 09:19 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import djangoldp.fields + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('djangoldp_notification', '0009_auto_20200619_0802'), + ] + + def create_settings(apps, schema_editor): + Users = apps.get_model('djangoldp_account', 'LDPUser') + Settings = apps.get_model('djangoldp_notification', 'NotificationSetting') + + # iterate over all objects + for user in Users.objects.all(): + if user.urlid.startswith(settings.SITE_URL): + Settings.objects.create(user=user) + + operations = [ + migrations.CreateModel( + name='NotificationSetting', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('urlid', djangoldp.fields.LDPUrlField(blank=True, null=True, unique=True)), + ('is_backlink', models.BooleanField(default=False, help_text='set automatically to indicate the Model is a backlink')), + ('allow_create_backlink', models.BooleanField(default=True, help_text='set to False to disable backlink creation after Model save')), + ('receiveMail', models.BooleanField(default=True)), + ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='settings', to=settings.AUTH_USER_MODEL)), + ], + ), + migrations.RunPython(create_settings), + ] diff --git a/djangoldp_notification/models.py b/djangoldp_notification/models.py index e99fcda..066cb06 100644 --- a/djangoldp_notification/models.py +++ b/djangoldp_notification/models.py @@ -55,6 +55,22 @@ class Notification(Model): super(Notification, self).save(*args, **kwargs) +class NotificationSetting(Model): + user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="settings") + receiveMail = models.BooleanField(default=True) + + class Meta: + auto_author = 'user' + owner_field = 'user' + anonymous_perms = [] + authenticated_perms = [] + owner_perms = ['view', 'change'] + container_path = 'settings/' + serializer_fields = ['@id', 'receiveMail'] + rdf_type = 'sib:usersettings' + + def __str__(self): + return '{} ({})'.format(self.user.get_full_name(), self.user.urlid) class Subscription(Model): object = models.URLField() @@ -221,11 +237,20 @@ def send_email_on_notification(sender, instance, created, **kwargs): } ) - send_mail( - 'Notification sur ' + on, - instance.summary, - (getattr(settings, 'EMAIL_HOST_USER', False) or "noreply@" + settings.JABBER_DEFAULT_HOST), - [instance.user.email], - fail_silently=True, - html_message=html_message - ) + if instance.user.settings.receiveMail: + send_mail( + 'Notification sur ' + on, + instance.summary, + (getattr(settings, 'EMAIL_HOST_USER', False) or "noreply@" + settings.JABBER_DEFAULT_HOST), + [instance.user.email], + fail_silently=True, + html_message=html_message + ) + +@receiver(post_save, sender=settings.AUTH_USER_MODEL) +def create_user_settings(sender, instance, created, **kwargs): + try: + if created and instance.urlid.startswith(settings.SITE_URL): + NotificationSetting.objects.create(user=instance) + except: + pass diff --git a/djangoldp_notification/settings.py b/djangoldp_notification/settings.py index 3428c3a..a5ed1cc 100644 --- a/djangoldp_notification/settings.py +++ b/djangoldp_notification/settings.py @@ -1 +1 @@ -USER_NESTED_FIELDS = ['inbox'] \ No newline at end of file +USER_NESTED_FIELDS = ['inbox', 'settings'] \ No newline at end of file