digitaleagenturnc/users/signals.py

205 lines
8.0 KiB
Python

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 news.models import News
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
from tasks.models import Tasks
from cloud.models import DataFile
import os
from django.conf import settings
from django.utils import timezone
# Deletes all Notifications added to to delete news
@receiver(pre_delete, sender=News)
def del_news_notifications(sender, instance, **kwargs):
UserNotification.objects.filter(elementid=instance.pk).delete()
# SIGNALS FOR USER
'''
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
print(instance)
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)
userid = list(pk_set)[0]
user_touched = User.objects.get(pk=userid)
# 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
)
# SIGNAL FOR NEWS
@receiver(post_save, sender=News)
def save_news(sender, instance, **kwargs):
GLOBALSENDMAILS = True
if(kwargs["created"]):
usersofagency = User.objects.filter(profile__agency__pk=instance.agency.pk)
targeturl = settings.BASE_URL + "news/news/" + str(instance.pk) + "/single"
if(instance.go_online_on < timezone.now() and instance.agnotify):
for user in usersofagency:
if(user.profile.news_mail):
notificationtext = "Neue Agenturnews: " + instance.name
username = user.first_name + " " + user.last_name
msg_html = render_to_string('notificsys/notification_mail.html', {'username': username, 'notificationtext' : notificationtext, 'linktarget' : targeturl})
if(GLOBALSENDMAILS):
send_mail(
'Agentur-Benachrichtigung',
'Hallo ' + user.first_name + ' ' + user.last_name + '! ' + notificationtext,
'support@digitale-agentur.com',
[user.email],
html_message=msg_html,
fail_silently=False
)
if(user.profile.news_push):
newnotification = UserNotification(touser=user, notificationtext="Neue Agenturnews: " + instance.name, notificationtype="agencynews", elementid=instance.pk)
newnotification.save()
else:
instance.agnotify = False
instance.save()
# SIGNALS FOR TASK
@receiver(signal=m2m_changed, sender=Tasks.usersfield.through)
def adjust_group_notifications_task(instance, action, reverse, model, pk_set, using, *args, **kwargs):
# IF FALSE NO MAILS WILL BE SEND - IN PRODUCTIVITY CHANGE TO TRUE #
GLOBALSENDMAILS = True
# A USER WAS TOUCHED ATT HIS TASKS
user_touched = User.objects.get(pk=list(pk_set)[0])
taskname = instance.name
# PUSH NOTIFICATION FOR GROUOPCHANGES
if(user_touched.profile.add_task_push):
if(action == 'post_remove'):
newnotification = UserNotification(touser=user_touched, notificationtext="Sie wurden von der Tätigkeit " + taskname + " entfernt.", notificationtype="taskchange")
newnotification.save()
# A USER WAS ADDED TO A GROUP
elif(action == 'post_add'):
newnotification = UserNotification(touser=user_touched, notificationtext="Sie wurden der Tätigkeit " + taskname + " zugeordnet.", notificationtype="taskchange")
newnotification.save()
# E-MAILNOTIFICATIONS FOR GROUPCHANGES
if(user_touched.profile.add_task_mail):
notificationtext = ""
if(action == 'post_remove'):
notificationtext = "Sie wurden von der Tätigkeit " + taskname + " 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 der Tätigkeit " + taskname + " zugeordnet."
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
)
# SIGNLA WHEN FILE IS TO DELETE
#@receiver(pre_delete, sender=DataFile)
#def auto_delete_file_on_delete(sender, instance, **kwargs):
# if instance.file.file:
# if os.path.isfile(instance.file.path):
# os.remove(instance.file.path)