minor: notification settings per user
This commit is contained in:
parent
78b22cbe9f
commit
12220a9e18
@ -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),
|
||||||
|
]
|
@ -55,6 +55,22 @@ class Notification(Model):
|
|||||||
|
|
||||||
super(Notification, self).save(*args, **kwargs)
|
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):
|
class Subscription(Model):
|
||||||
object = models.URLField()
|
object = models.URLField()
|
||||||
@ -221,11 +237,20 @@ def send_email_on_notification(sender, instance, created, **kwargs):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
send_mail(
|
if instance.user.settings.receiveMail:
|
||||||
'Notification sur ' + on,
|
send_mail(
|
||||||
instance.summary,
|
'Notification sur ' + on,
|
||||||
(getattr(settings, 'EMAIL_HOST_USER', False) or "noreply@" + settings.JABBER_DEFAULT_HOST),
|
instance.summary,
|
||||||
[instance.user.email],
|
(getattr(settings, 'EMAIL_HOST_USER', False) or "noreply@" + settings.JABBER_DEFAULT_HOST),
|
||||||
fail_silently=True,
|
[instance.user.email],
|
||||||
html_message=html_message
|
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
|
||||||
|
@ -1 +1 @@
|
|||||||
USER_NESTED_FIELDS = ['inbox']
|
USER_NESTED_FIELDS = ['inbox', 'settings']
|
Loading…
Reference in New Issue
Block a user