From 3ed92963c2e2121eab4270129cdc253fb332544d Mon Sep 17 00:00:00 2001 From: Holger Trampe Date: Mon, 6 Jul 2020 10:41:53 +0200 Subject: [PATCH] Agenturverbund BEnachrichtigungen neuer Standard fertig --- .../dasettings/agencynetwork_content.html | 4 +- standards/views.py | 104 +++++++++++++++++- 2 files changed, 105 insertions(+), 3 deletions(-) diff --git a/dasettings/templates/dasettings/agencynetwork_content.html b/dasettings/templates/dasettings/agencynetwork_content.html index 25df9d6..04fbb5f 100644 --- a/dasettings/templates/dasettings/agencynetwork_content.html +++ b/dasettings/templates/dasettings/agencynetwork_content.html @@ -87,7 +87,7 @@ diff --git a/standards/views.py b/standards/views.py index b4cec51..88e3522 100644 --- a/standards/views.py +++ b/standards/views.py @@ -16,6 +16,11 @@ from django.contrib.auth.decorators import login_required import re from django.template import defaultfilters from organizer.models import QuickLinks, AGContacts, AGPassword +import channels.layers +from asgiref.sync import async_to_sync +from django.template.loader import render_to_string +from notificsys.models import UserNotification +from django.core.mail import send_mail # ALLE STANDARDS EINER AGENTUR class StandardsManagement(LoginRequiredMixin, ListView): @@ -327,11 +332,95 @@ def StandardAdd(request, id=False): # ADD TO NETWORKS networks = normalForm.cleaned_data['checked_networks'].split(",") + ''' + Alle Agenturverbunde laden, in denen die Agentur drin ist. + ''' + agencynetworks_all = AgencyNetwork.objects.all() + agencynetworks = [] + for a in agencynetworks_all: + if request.user.profile.agency in a.adminagencys.all() or request.user.profile.agency in a.members.all() or request.user.profile.agency in a.sharemembers.all(): + agencynetworks.append(a) + + agencynetworks_standard_in = [] + + # Agenturverbünde, in denen der Standard drin sein soll for f in networks: if(f.isdigit()): tempnetwork = AgencyNetwork.objects.get(pk=f) - tempnetwork.standards.add(standard) + #tempnetwork.standards.add(standard) + agencynetworks_standard_in.append(tempnetwork) + # Wenn Standard im gewünschten Verbund, dann rein, ansonsten raus + for a in agencynetworks: + if a in agencynetworks_standard_in and standard not in a.standards.all(): + a.standards.add(standard) + else: + a.standards.remove(standard) + + + ''' + Info an die Agenturen über einen neuen Standard + + ''' + agency_send = [] + for agencynetwork_single in agencynetworks_standard_in: + for agn_admin in agencynetwork_single.adminagencys.all(): + if(agn_admin not in agency_send): + agency_send.append(agn_admin) + if agn_admin == request.user.profile.agency: + users_of_other_ag = User.objects.filter(profile__agency=agn_admin) + for u in users_of_other_ag: + if u.has_perm('users.standardmanager'): + if u.usernotifications.agn_standard_created_mail: + notificationtext = "im Agenturverbund " + agencynetwork_single.name + " wurde ein neuer Standard geteilt: " + standard.name + sendMailNoti(notificationtext, u) + + if u.usernotifications.agn_standard_created_push: + newnotification = UserNotification(touser=u, notificationtext="im Agenturverbund " + agencynetwork_single.name + " wurde ein neuer Standard geteilt: " + standard.name, notificationtype="") + newnotification.save() + + channel_layer = channels.layers.get_channel_layer() + async_to_sync(channel_layer.group_send)("user_" + str(u.pk), {'type' : 'pushhandler', 'pushtext' : "pushnotification__Agenturverbund | Im Agenturverbund " + agencynetwork_single.name + " wurde ein neuer Standard geteilt: " + standard.name}) + for agn_admin in agencynetwork_single.members.all(): + if(agn_admin not in agency_send): + agency_send.append(agn_admin) + if agn_admin == request.user.profile.agency: + users_of_other_ag = User.objects.filter(profile__agency=agn_admin) + for u in users_of_other_ag: + if u.has_perm('users.standardmanager'): + if u.usernotifications.agn_standard_created_mail: + notificationtext = "im Agenturverbund " + agencynetwork_single.name + " wurde ein neuer Standard geteilt: " + standard.name + sendMailNoti(notificationtext, u) + + if u.usernotifications.agn_standard_created_push: + newnotification = UserNotification(touser=u, notificationtext="im Agenturverbund " + agencynetwork_single.name + " wurde ein neuer Standard geteilt: " + standard.name, notificationtype="") + newnotification.save() + + channel_layer = channels.layers.get_channel_layer() + async_to_sync(channel_layer.group_send)("user_" + str(u.pk), {'type' : 'pushhandler', 'pushtext' : "pushnotification__Agenturverbund | Im Agenturverbund " + agencynetwork_single.name + " wurde ein neuer Standard geteilt: " + standard.name}) + for agn_admin in agencynetwork_single.sharemembers.all(): + if(agn_admin not in agency_send): + agency_send.append(agn_admin) + if agn_admin == request.user.profile.agency: + users_of_other_ag = User.objects.filter(profile__agency=agn_admin) + for u in users_of_other_ag: + if u.has_perm('users.standardmanager'): + if u.usernotifications.agn_standard_created_mail: + notificationtext = "im Agenturverbund " + agencynetwork_single.name + " wurde ein neuer Standard geteilt: " + standard.name + sendMailNoti(notificationtext, u) + + if u.usernotifications.agn_standard_created_push: + newnotification = UserNotification(touser=u, notificationtext="im Agenturverbund " + agencynetwork_single.name + " wurde ein neuer Standard geteilt: " + standard.name, notificationtype="") + newnotification.save() + + channel_layer = channels.layers.get_channel_layer() + async_to_sync(channel_layer.group_send)("user_" + str(u.pk), {'type' : 'pushhandler', 'pushtext' : "pushnotification__Agenturverbund | Im Agenturverbund " + agencynetwork_single.name + " wurde ein neuer Standard geteilt: " + standard.name}) + # Clear sended Agencys for multiple Networks + agency_send = [] + + + + if request.user.has_perm('users.standardmanager'): messages.success(request, f'Standard {standard.name} aktualisiert!') else: @@ -450,6 +539,19 @@ def StandardAdd(request, id=False): } return render(request, 'standards/standards_add.html', context) + +def sendMailNoti(notificationtext, user_touched, linktarget=""): + + username = user_touched.first_name + " " + user_touched.last_name + msg_html = render_to_string('notificsys/notification_mail.html', {'username': username, 'notificationtext' : notificationtext, 'linktarget' : linktarget}) + send_mail( + 'Agentur-Benachrichtigung', + 'Hallo ' + user_touched.first_name + ' ' + user_touched.last_name + '! ' + notificationtext, + 'noreply@digitale-agentur.com', + [user_touched.email], + html_message=msg_html, + fail_silently=True + ) ''' @login_required def StandardUpdate(request, id):