Browse Source

fix email send trigger

subDesTagesMitExtraKaese 2 years ago
parent
commit
ba71616c3a
1 changed files with 27 additions and 13 deletions
  1. 27 13
      signals.py

+ 27 - 13
signals.py

@@ -1,36 +1,50 @@
+import json
+from django.contrib.admin.models import LogEntry, CHANGE
 from django.contrib.auth.models import User
+from django.contrib.contenttypes.models import ContentType
+
 from django.core.mail import send_mail
 from django.db.models import signals
 
 from website.settings import *
 
-def post_user_save(sender, instance: User, *args, **kwargs):
-  #oldUser: User = User.objects.get(id=instance.id)
-  #print(oldUser.get_all_permissions(), args, kwargs)
-  #if instance.get_all_permissions() != oldUser.get_all_permissions():
+def post_log_save(sender, instance: LogEntry, *args, **kwargs):
+  if instance.action_flag == CHANGE and instance.change_message:
+    ct: ContentType = instance.content_type
+    if ct.app_label == "auth" and ct.model == "user":
+      user: User = User.objects.get(id=instance.object_id)
+      for msg in json.loads(instance.change_message):
+        if 'changed' in msg:
+          for field in msg['changed']['fields']:
+            if field == "Groups" or field == "Staff status" or field == "User permissions":
+              send_notification(user)
+              return
+    
+signals.post_save.connect(post_log_save, sender=LogEntry, dispatch_uid='post_log_save')
+
+def send_notification(user: User):
     perm_list = []
-    if instance.has_perm("stammbaum.view"):
+    if user.has_perm("stammbaum.view"):
       perm_list.append("Du darfst den gesamten Stammbaum ansehen.")
-    if instance.has_perm("stammbaum.upload_image"):
+    if user.has_perm("stammbaum.upload_image"):
       perm_list.append("Du darfst Bilder für Personen im Stammbaum hochladen.")
-    if instance.is_staff and instance.has_perm("stammbaum.change_person"):
+    if user.is_staff and user.has_perm("stammbaum.change_person"):
       perm_list.append("Du darfst Personen im Stammbaum bearbeiten.")
-    if instance.is_staff and instance.has_perm("stammbaum.add_person"):
+    if user.is_staff and user.has_perm("stammbaum.add_person"):
       perm_list.append("Du darfst Personen zum Stammbaum hinzufügen.")
-    if instance.is_staff and instance.has_perm("stammbaum.add_group"):
+    if user.is_staff and user.has_perm("stammbaum.add_group"):
       perm_list.append("Du darfst deinen eigenen Stammbaum hinzufügen. (Stammbaum bearbeiten -> Gruppe hinzufügen)")
 
     send_mail(
       subject='Deine Berechtigungen wurden geändert',
-      message=f'Hallo {instance.get_full_name()}, \n\n' +
+      message=f'Hallo {user.get_full_name()}, \n\n' +
       "deine Berechtigungen auf der Stammbaum-Webseite wurden gerade angepasst.\n\n" +
       "Jetzt hast du folgende Berechtigungen:\n" + 
       '\n'.join([' - ' + p for p in perm_list]) + '\n\n' +
       "LG\nWebsite-Admins\n\n" +
       "https://pi.justprojects.de/stammbaum\n" + 
-      f"Login vergessen? Dein Benutzername lautet {instance.username}.",
+      f"Login vergessen? Dein Benutzername lautet {user.username}.",
       from_email='stammbaum@justprojects.de',
-      recipient_list=[instance.email],
+      recipient_list=[user.email],
       fail_silently=False
     )
-signals.post_save.connect(post_user_save, sender=User, dispatch_uid='post_user_save')