From f2f1cbe12f006f26198f4a8a97c4c06a63afef1b Mon Sep 17 00:00:00 2001 From: Holger Trampe Date: Mon, 6 Jul 2020 17:12:40 +0200 Subject: [PATCH] Agenturverbund Benachrichtigungen ENDLICH fertig --- dasettings/forms.py | 5 +- dasettings/views.py | 69 ++++++++++++++++-- standards/views.py | 172 +++++++++++++++----------------------------- users/models.py | 2 +- 4 files changed, 125 insertions(+), 123 deletions(-) diff --git a/dasettings/forms.py b/dasettings/forms.py index 2945e4d..e1a5f61 100644 --- a/dasettings/forms.py +++ b/dasettings/forms.py @@ -203,9 +203,8 @@ class UsersNotificationFormAgn(forms.ModelForm): "agn_standard_copied_mail" : "Standard wurde übernommen", "agn_standard_comment_mail" : "Kommentar zu Standard aus meiner Agentur", "agn_standard_comment_react_mail" : "Reaktion zu meinem Kommentar", - "agn_own_change_mail" : "Änderungen meiner Mitgliedschaften", - "agn_other_change_mail" : "Änderungen anderer Mitgliedschaften", - + "agn_own_change_mail" : "Änderungen meiner Agenturverbunde", + "agn_other_change_mail" : "Änderung anderer Agenturverbunde", } fields = [ 'agn_standard_created_mail', diff --git a/dasettings/views.py b/dasettings/views.py index 54c7796..fa3e558 100644 --- a/dasettings/views.py +++ b/dasettings/views.py @@ -1203,7 +1203,7 @@ def AddMyAgencyToAgn(request, networkid): agn = AgencyNetwork.objects.filter(networkid=networkid) if len(agn) == 0: - messages.info(request, f'Agenturverband nicht gefunden!') + messages.info(request, f'Agenturverbund nicht gefunden!') return redirect('dasettings') else: context = { @@ -1240,6 +1240,23 @@ def JoinAGN(request, pk): if(agn.publicjoin): messages.success(request, f'Verbund erfolgreich beigetreten!') agn.members.add(request.user.profile.agency) + + # Benachrichtigung senden, dass eine neue Agentur dem Verbund beitreten will. DIe Info geht an alle administrativen Agenturen. + for adminagencys in agn.adminagencys.all(): + usersofagency = Users.objects.filter(profile__agency=adminagencys) + + for u in usersofagency: + if u.has_perm('users.agencynetwork') and u.has_perm('users.agencynetwork'): + if u.usernotifications.agn_own_change_mail: + notificationtext = " eine neue Agentur ist dem Verbund " + agn.name + " beigetreten." + sendMailNoti(notificationtext, u) + + if u.usernotifications.agn_own_change_push: + newnotification = UserNotification(touser=u, notificationtext=" eine neue Agentur ist dem Verbund " + agn.name + " beigetreten.", 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 | Eine neue Agentur ist dem Verbund " + agn.name + " beigetreten."}) else: # STATUS @@ -1247,7 +1264,22 @@ def JoinAGN(request, pk): agnp = AgencyNetworkPreperation(target_network=AgencyNetwork.objects.get(pk=pk), wanted_agency=request.user.profile.agency, status=1) agnp.save() + # Benachrichtigung senden, dass eine neue Agentur dem Verbund beitreten will. DIe Info geht an alle administrativen Agenturen. + for adminagencys in agn.adminagencys.all(): + usersofagency = Users.objects.filter(profile__agency=adminagencys) + for u in usersofagency: + if u.has_perm('users.agencynetwork') and u.has_perm('users.agencynetwork'): + if u.usernotifications.agn_own_change_mail: + notificationtext = " eine neue Agentur möchte dem Verbund " + agn.name + " beitreten." + sendMailNoti(notificationtext, u) + + if u.usernotifications.agn_own_change_push: + newnotification = UserNotification(touser=u, notificationtext=" eine neue Agentur möchte dem Verbund " + agn.name + " beitreten.", 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 | Eine neue Agentur möchte dem Verbund " + agn.name + " beitreten."}) messages.success(request, f'Ihre Anfrage zum Beitritt wurde versendet. Sie erhalten eine Information, wenn die Anfrage angenommen wurde!') @@ -1273,8 +1305,7 @@ def ManageAgInAgn(request, pk): allagofagn.append(a) for a in network.adminagencys.all(): - allagofagn.append(a) - + allagofagn.append(a) context = { 'active_link' : 'dasettings', @@ -1288,7 +1319,7 @@ def ManageAgInAgn(request, pk): def AddAgToNetwork(request, network, targetag, aginvpk): if IsAgencyInAgNetwork(Agency.objects.get(pk=targetag).pk, network): - messages.info(request, f'Sie sind bereits in der Agentur!') + messages.info(request, f'Sie sind bereits im Agenturverbund!') return redirect('dasettings') else: @@ -1323,6 +1354,22 @@ def AgencyNetworkAjaxSettings(request): agencynetwork.adminagencys.remove(agency) agencynetwork.sharemembers.remove(agency) success = True + + # Benachrichtigung senden, dass eine neue Agentur dem Verbund beitreten will. DIe Info geht an alle administrativen Agenturen. + usersofagency = Users.objects.filter(profile__agency=agency) + for u in usersofagency: + if u.has_perm('users.agencynetwork') and u.has_perm('users.agencynetwork'): + if u.usernotifications.agn_other_change_mail: + notificationtext = " ihre Agentur wurde aus dem Verbund " + agencynetwork.name + " entfernt." + sendMailNoti(notificationtext, u) + + if u.usernotifications.agn_other_change_push: + newnotification = UserNotification(touser=u, notificationtext=" ihre Agentur wurde aus dem Verbund " + agencynetwork.name + " entfernt.", 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 | Ihre Agentur wurde aus dem Verbund " + agencynetwork.name + " entfernt."}) + elif request.method == 'GET' and request.GET['action'] == "removeinv": AgencyNetworkPreperation.objects.get(pk=request.GET['agn_inv']).delete() success = True @@ -1340,6 +1387,20 @@ def AgencyNetworkAjaxSettings(request): elif (request.GET['newstatus'] == "2"): agn.adminagencys.add(agency) success = True + + usersofagency = Users.objects.filter(profile__agency=agency) + for u in usersofagency: + if u.has_perm('users.agencynetwork') and u.has_perm('users.agencynetwork'): + if u.usernotifications.agn_other_change_mail: + notificationtext = " ihre Agentur hat im Verbund " + agn.name + " andere Rechte erhalten." + sendMailNoti(notificationtext, u) + + if u.usernotifications.agn_other_change_push: + newnotification = UserNotification(touser=u, notificationtext=" ihre Agentur hat im Verbund " + agn.name + " andere Rechte erhalten.", 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 | Ihre Agentur hat im Verbund " + agn.name + " andere Rechte erhalten."}) else: success = False return JsonResponse(data) diff --git a/standards/views.py b/standards/views.py index 88e3522..68e9264 100644 --- a/standards/views.py +++ b/standards/views.py @@ -367,10 +367,10 @@ def StandardAdd(request, id=False): 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: + 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.has_perm('users.standardmanager') and u.has_perm('users.agencynetwork'): if u.usernotifications.agn_standard_created_mail: notificationtext = "im Agenturverbund " + agencynetwork_single.name + " wurde ein neuer Standard geteilt: " + standard.name sendMailNoti(notificationtext, u) @@ -384,10 +384,10 @@ def StandardAdd(request, id=False): 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: + 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.has_perm('users.standardmanager') and u.has_perm('users.agencynetwork'): if u.usernotifications.agn_standard_created_mail: notificationtext = "im Agenturverbund " + agencynetwork_single.name + " wurde ein neuer Standard geteilt: " + standard.name sendMailNoti(notificationtext, u) @@ -401,10 +401,10 @@ def StandardAdd(request, id=False): 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: + 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.has_perm('users.standardmanager') and u.has_perm('users.agencynetwork'): if u.usernotifications.agn_standard_created_mail: notificationtext = "im Agenturverbund " + agencynetwork_single.name + " wurde ein neuer Standard geteilt: " + standard.name sendMailNoti(notificationtext, u) @@ -417,9 +417,6 @@ def StandardAdd(request, id=False): 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!') @@ -540,8 +537,8 @@ def StandardAdd(request, id=False): return render(request, 'standards/standards_add.html', context) -def sendMailNoti(notificationtext, user_touched, linktarget=""): - +# Funktion zum versenden einer Mail inkl. TEmplate +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( @@ -552,110 +549,7 @@ def sendMailNoti(notificationtext, user_touched, linktarget=""): html_message=msg_html, fail_silently=True ) -''' -@login_required -def StandardUpdate(request, id): - standard = Standards.objects.get(pk=id, agency=request.user.profile.agency) - if request.method == 'POST': - #normalForm = StandardUpdateStandard(request.POST, instance=standard) - normalForm = StandardUpdateStandard(request.POST, instance=standard) - editorForm = StandardUpdateStandardEditor(request.POST, instance=standard) - - if editorForm.is_valid() and normalForm.is_valid(): - existing_standard = Standards.objects.get(pk=id) - existing_standard.last_modified_by = request.user - existing_standard.last_modified_on = datetime.now() - existing_standard.task = normalForm.cleaned_data['task'] - existing_standard.area = normalForm.cleaned_data['area'] - existing_standard.name = normalForm.cleaned_data['name'] - existing_standard.content = editorForm.cleaned_data['content'] - existing_standard.freefield_content = normalForm.cleaned_data['freefield_content'] - existing_standard.freefield_title = normalForm.cleaned_data['freefield_title'] - #existing_standard.representative.set(normalForm.cleaned_data['representative']) - #existing_standard.executor.set(normalForm.cleaned_data['executor']) - #existing_standard.authority.set(normalForm.cleaned_data['authority']) - - if request.user.has_perm('users.standardmanager'): - messages.success(request, f'Standard {existing_standard.name} aktualisiert!') - else: - if existing_standard.public: - existing_standard.public = False - messages.warning(request, f'Standard {existing_standard.name} aktualisiert und ist nicht mehr öffentlich, damit Änderungen geprüft werden können.') - else: - messages.success(request, f'Standard {existing_standard.name} aktualisiert!') - existing_standard.save() - return redirect('/standards') - - else: - normalForm = StandardUpdateStandard(instance=standard) - editorForm = StandardUpdateStandardEditor(instance=standard) - - - possibleFilesByVisible = [] - - allfiles = DataFile.objects.filter(agency=request.user.profile.agency) - - for f in allfiles: - actParent = DataDir.objects.get(pk=f.parent.pk, agency=request.user.profile.agency) - if actParent.is_root: - possibleFilesByVisible.append(f) - else: - if(checkUserDirRights(request, actParent, request.user.pk) and f not in standard.addedfiles.all()): - possibleFilesByVisible.append(f) - - - possiblestandards = Standards.objects.filter(agency=request.user.profile.agency, public=True) - possiblestandards_final = [] - for s in possiblestandards: - if s not in standard.linked_standards.all(): - possiblestandards_final.append(s) - - possible_verant = User.objects.filter(profile__agency__pk=request.user.profile.agency.pk) - possible_verant_final = [] - for pv in possible_verant: - if pv not in standard.authority.all(): - possible_verant_final.append(pv) - - possible_ex = User.objects.filter(profile__agency__pk=request.user.profile.agency.pk) - possible_ex_final = [] - for pv in possible_ex: - if pv not in standard.executor.all(): - possible_ex_final.append(pv) - - possible_ver = User.objects.filter(profile__agency__pk=request.user.profile.agency.pk) - possible_ver_final = [] - for pv in possible_ver: - if pv not in standard.representative.all(): - possible_ver_final.append(pv) - - #agencynetworks = AgencyNetwork.objects.filter(creator_agency=request.user.profile.agency) | AgencyNetwork.objects.filter(adminagencys__in=[request.user.profile.agency.pk]) | AgencyNetwork.objects.filter(members__in=[request.user.profile.agency.pk]) | AgencyNetwork.objects.filter(sharemembers__in=[request.user.profile.agency.pk]) - - agencynetworks_all = AgencyNetwork.objects.all() - agencynetworks = [] - - for a in agencynetworks_all: - if self.request.user.profile.agency in a.adminagencys.all() or self.request.user.profile.agency in a.members.all() or self.request.user.profile.agency in a.sharemembers.all(): - agencynetworks.append(a) - - context = { - 'normalForm' : normalForm, - 'editorForm' : editorForm, - 'standard' : standard, - 'active_link' : 'standards', - 'standard_id' : standard.pk, - 'standard_status' : standard.public, - 'possibleFilesByVisible' : possibleFilesByVisible, - 'agencygroups' : AgencyGroup.objects.filter(agency=request.user.profile.agency), - 'parentid' : list(DataDir.objects.filter(agency=request.user.profile.agency, is_root=True))[0].pk, - 'poss_verant' : possible_verant_final, - 'poss_ex' : possible_ex_final, - 'poss_ver' : possible_ver_final, - 'possiblestandards' : possiblestandards_final, - "agencynetworks" : agencynetworks - } - return render(request, 'standards/standards_update.html', context) -''' @login_required def load_tasks(request): areaid = request.GET.get('areaid') @@ -760,6 +654,22 @@ def CopyStandard(request, pk): tempdatafile.save() new_standard.addedfiles.add(tempdatafile) + # Sende Info, dass ein Standard übernommen wurde, an die Erstelleragentur + usersofagency = Users.objects.filter(profile__agency=sc.agency) + + for u in usersofagency: + if u.has_perm('users.standardmanager') and u.has_perm('users.agencynetwork'): + if u.usernotifications.agn_standard_copied_mail: + notificationtext = "der Standard " + sc.name + " wurde von der Agentur " + request.user.profile.agency.name + " übernommen." + sendMailNoti(notificationtext, u) + + if u.usernotifications.agn_standard_copied_push: + newnotification = UserNotification(touser=u, notificationtext="der Standard " + sc.name + " wurde von der Agentur " + request.user.profile.agency.name + " übernommen.", 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 | Der Standard " + sc.name + " wurde von der Agentur " + request.user.profile.agency.name + " übernommen."}) + return redirect('standard-add', new_standard.pk) @login_required @@ -888,8 +798,27 @@ def updatesbyajax_agn(request, pk): content = re.sub('[^A-Za-z0-9,!?_ äüöÄÜÖ]+', '', request.GET.get("content")) sc = StandardComments(standard=Standards.objects.get(pk=pk), content=content, comment_by=request.user, comment_on=datetime.now(), last_modified_on=datetime.now()) sc.save() - + + # Benachrichtigung an Standardagentur + standard_comment = Standards.objects.get(pk=pk) + usersofagency = User.objects.filter(profile__agency=standard_comment.agency) + + for u in usersofagency: + if u.has_perm('users.standardmanager'): + if u.usernotifications.agn_standard_comment_mail: + notificationtext = "der Standard " + standard_comment.name + " wurde kommentiert: " + content + sendMailNoti(notificationtext, u) + + if u.usernotifications.agn_standard_comment_push: + newnotification = UserNotification(touser=u, notificationtext="der Standard " + standard_comment.name + " wurde kommentiert: " + content, 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 | Der Standard " + standard_comment.name + " wurde kommentiert:" + content}) + + return JsonResponse({"success" : "success", "sc_id" : sc.pk, "sc_c" : sc.content, "sc_user" : sc.comment_by.first_name + " " + sc.comment_by.last_name, "sc_date" : defaultfilters.date(sc.last_modified_on, "SHORT_DATETIME_FORMAT") }) + elif(request.GET["action"] == "del_comment"): StandardComments.objects.get(pk=request.GET.get("id")).delete() return JsonResponse({}) @@ -911,6 +840,19 @@ def updatesbyajax_agn(request, pk): counter_up = len(StandardCommentRate.objects.filter(oncomment=comment, rate_stats=1)) counter_down = len(StandardCommentRate.objects.filter(oncomment=comment, rate_stats=0)) + u = comment.comment_by + if u.usernotifications.agn_standard_comment_react_mail: + notificationtext = "der Kommentar zum Standard " + comment.standard.name + " wurde gerated!" + sendMailNoti(notificationtext, u) + + if u.usernotifications.agn_standard_comment_react_push: + newnotification = UserNotification(touser=u, notificationtext="der Kommentar zum Standard " + comment.standard.name + " wurde gerated!", 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__Kommantar | der Kommentar zum Standard " + comment.standard.name + " wurde gerated!"}) + + return JsonResponse({"up" : counter_up, "down" : counter_down}) elif(request.GET["action"] == "markingfavorit"): workingstandard = Standards.objects.get(pk=pk) diff --git a/users/models.py b/users/models.py index 00a1782..0379f8e 100644 --- a/users/models.py +++ b/users/models.py @@ -394,7 +394,7 @@ class UserNotifications(models.Model): agn_standard_comment_react_push = models.BooleanField(default=True) # Änderungen eigener Mitgliedsanfragen in anderen Verbünden - agn_own_change_mail = models.BooleanField(default=True) + agn_own_change_mail = models.BooleanField(default=False) agn_own_change_push = models.BooleanField(default=True) # Änderungen anderer Mitgliedschaften (Beitrittsanfragen!)