from django.db.models.signals import post_save, pre_delete, m2m_changed from django.contrib.auth.models import User, Group from django.dispatch import receiver from .models import Profile, Agency, AgencyGroup from django.contrib.auth.models import Permission from notificsys.models import UserNotification from django.core.mail import send_mail from django.template.loader import render_to_string # SIGNALS FOR USER @receiver(post_save, sender=User) def create_profile(sender, instance, created, **kwargs): if created: Profile.objects.create(user=instance, agency=instance.agency, parent=instance.parent) ''' Wenn ein neuer Nutzer angelegt wird und dies der erste der Agentur ist, erhält dieser automatisch alle verfügbaren Rechte! ''' user_agency = User.objects.filter(profile__agency__pk=instance.agency.pk) if len(user_agency) == 1: tempuser = user_agency[0] temprof = Profile for ele in temprof._meta.permissions: tempperm = Permission.objects.get(codename=ele[0]) tempuser.user_permissions.add(tempperm) #tempuser.profile.func = 'lead' tempuser.save() @receiver(post_save, sender=User) def save_profile(sender, instance, **kwargs): instance.profile.save() # SIGNALS FOR GROUPS @receiver(signal=m2m_changed, sender=User.groups.through) def adjust_group_notifications(instance, action, reverse, model, pk_set, using, *args, **kwargs): # IF FALSE NO MAILS WILL BE SEND - IN PRODUCTIVITY CHANGE TO TRUE # GLOBALSENDMAILS = True # GROUPSETTINGS FOR SOME USER WAS CHANGED if isinstance(instance, Group): group_touched = AgencyGroup.objects.get(group=instance) user_touched = User.objects.get(pk=list(pk_set)[0]) # PUSH NOTIFICATION FOR GROUOPCHANGES if(user_touched.profile.add_new_group_push): if(action == 'post_remove'): newnotification = UserNotification(touser=user_touched, notificationtext="Sie wurden aus der Gruppe " + group_touched.agencygroupname + " entfernt.", notificationtype="groupchanges") newnotification.save() # A USER WAS ADDED TO A GROUP elif(action == 'post_add'): newnotification = UserNotification(touser=user_touched, notificationtext="Sie wurden zur Gruppe " + group_touched.agencygroupname + " hinzugefügt.", notificationtype="groupchanges") newnotification.save() # E-MAILNOTIFICATIONS FOR GROUPCHANGES if(user_touched.profile.add_new_group_mail): notificationtext = "" if(action == 'post_remove'): notificationtext = "Sie wurden aus der Gruppe " + group_touched.agencygroupname + " entfernt." username = user_touched.first_name + " " + user_touched.last_name msg_html = render_to_string('notificsys/notification_mail.html', {'username': username, 'notificationtext' : notificationtext}) if(GLOBALSENDMAILS): send_mail( 'Agentur-Benachrichtigung', 'Hallo ' + user_touched.first_name + ' ' + user_touched.last_name + '! ' + notificationtext, 'support@digitale-agentur.com', [user_touched.email], html_message=msg_html, fail_silently=False ) # A USER WAS ADDED TO A GROUP elif(action == 'post_add'): notificationtext = "Sie wurden zur Gruppe " + group_touched.agencygroupname + " hinzugefügt." username = user_touched.first_name + " " + user_touched.last_name msg_html = render_to_string('notificsys/notification_mail.html', {'username': username, 'notificationtext' : notificationtext}) if(GLOBALSENDMAILS): send_mail( 'Agentur-Benachrichtigung', 'Hallo ' + user_touched.first_name + ' ' + user_touched.last_name + '! ' + notificationtext, 'support@digitale-agentur.com', [user_touched.email], html_message=msg_html, fail_silently=False )