From 97852d95688ec2eebf6f2bf45f1de3a7e86e4677 Mon Sep 17 00:00:00 2001 From: Raoni Smaneoto Date: Thu, 26 Apr 2018 11:01:21 -0300 Subject: [PATCH 1/2] Removing verification from setup_current_institution; notifying the institution removal; removing useless function --- backend/handlers/institution_handler.py | 12 ------ backend/utils.py | 3 -- backend/worker.py | 42 +++++++++++++++++++ .../notification/notificationDirective.js | 1 - frontend/test/specs/userSpec.js | 40 ------------------ frontend/user/user.js | 13 ------ 6 files changed, 42 insertions(+), 69 deletions(-) diff --git a/backend/handlers/institution_handler.py b/backend/handlers/institution_handler.py index 5c71c7e29..2713064ba 100644 --- a/backend/handlers/institution_handler.py +++ b/backend/handlers/institution_handler.py @@ -16,7 +16,6 @@ from models.institution import Institution from util.json_patch import JsonPatch from service_entities import enqueue_task -from send_email_hierarchy.remove_institution_email_sender import RemoveInstitutionEmailSender from handlers.base_handler import BaseHandler @@ -204,17 +203,6 @@ def delete(self, user, institution_key): enqueue_task('remove-inst', params) - email_params = { - "justification": self.request.get('justification'), - "body": """Lamentamos informar que a instituição %s foi removida - pelo administrador %s """ % (institution.name, user.name), - "subject": "Remoção de instituição", - "inst_key": institution_key - } - - email_sender = RemoveInstitutionEmailSender(**email_params) - email_sender.send_email() - notification_entity = { 'key': institution_key, 'institution_name': institution.name, diff --git a/backend/utils.py b/backend/utils.py index b7e7fbeaa..91682efad 100644 --- a/backend/utils.py +++ b/backend/utils.py @@ -110,9 +110,6 @@ def setup_current_institution(user, request): try: institution_header = request.headers['Institution-Authorization'] institution_key = ndb.Key(urlsafe=institution_header) - Utils._assert(not user.is_member(institution_key), - "Invalid Current Institution! User is not an active member.", - NotAuthorizedException) user.current_institution = institution_key except KeyError as error: user.current_institution = None diff --git a/backend/worker.py b/backend/worker.py index 7cb31ee3e..353a5d58d 100644 --- a/backend/worker.py +++ b/backend/worker.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """Send notifications handler.""" import webapp2 import json @@ -15,6 +16,7 @@ from jinja2 import Environment, FileSystemLoader from permissions import DEFAULT_SUPER_USER_PERMISSIONS from permissions import DEFAULT_ADMIN_PERMISSIONS +from send_email_hierarchy.remove_institution_email_sender import RemoveInstitutionEmailSender def should_remove(user, institution_key, current_inst_key): @@ -120,6 +122,45 @@ def remove_permissions(remove_hierarchy, institution): admin.remove_permission(permission, institution_key) +def notify_institution_removal(institution, remove_hierarchy, user): + """This method has two possibilities of flow depending on + the remove_hierarchy's value. + If it's true, the method send email and notification to all + the hierarchy. Otherwise it send just to the first institution. + + Params: + institution -- the current institution in the hierarchy whose admin + will receive an email and a notification. + remove_hierarchy -- string that works as a flag informing if the hierarchy. + has been removed or not. + user -- the user who made the request to remove the institution. + """ + email_params = { + "body": """Lamentamos informar que a instituição %s foi removida + pelo usuário %s """ % (institution.name, user.name), + "subject": "Remoção de instituição", + "inst_key": institution.key.urlsafe() + } + email_sender = RemoveInstitutionEmailSender(**email_params) + email_sender.send_email() + + user_has_to_receive_notification = institution.admin != user.key + + if user_has_to_receive_notification: + send_message_notification( + institution.admin.urlsafe(), + user.key.urlsafe(), + 'DELETED_INSTITUTION', + institution.key.urlsafe() + ) + + if remove_hierarchy == "true": + for child_key in institution.children_institutions: + child = child_key.get() + if child.state == "inactive": + notify_institution_removal(child, remove_hierarchy, user) + + class BaseHandler(webapp2.RequestHandler): """Base Handler.""" @@ -193,6 +234,7 @@ def apply_remove_operation(remove_hierarchy, institution, user): institution.handle_hierarchy_removal(remove_hierarchy, user) institution.remove_institution_from_users(remove_hierarchy) remove_permissions(remove_hierarchy, institution) + notify_institution_removal(institution, remove_hierarchy, user) apply_remove_operation(remove_hierarchy, institution, user) diff --git a/frontend/notification/notificationDirective.js b/frontend/notification/notificationDirective.js index 0987d0bf8..7bb0fddba 100644 --- a/frontend/notification/notificationDirective.js +++ b/frontend/notification/notificationDirective.js @@ -272,7 +272,6 @@ } function refreshUserInstitutions (notification) { - notificationCtrl.user.goToDifferentInstitution(notification.entity.key, notification.entity.remove_hierarchy); UserService.load().then(function success(response) { notificationCtrl.user.institutions = response.institutions; notificationCtrl.user.follows = response.follows; diff --git a/frontend/test/specs/userSpec.js b/frontend/test/specs/userSpec.js index 066397087..e4ddcaf6d 100644 --- a/frontend/test/specs/userSpec.js +++ b/frontend/test/specs/userSpec.js @@ -289,46 +289,6 @@ }); }); - describe('goToDifferentInstitution', function () { - it('should change current institution', function () { - var user = new User({ - institutions: [inst, other_inst], - current_institution: inst - }); - - expect(user.current_institution).toBe(inst); - - user.goToDifferentInstitution(inst.key); - - expect(user.current_institution).toBe(other_inst); - }); - - it('should not change the current institution', function() { - var user = new User({ - institutions: [inst], - current_institution: inst - }); - - expect(user.current_institution).toBe(inst); - - user.goToDifferentInstitution(inst.key); - - expect(user.current_institution).toBe(inst); - }); - - it('should go to a non child institution', function() { - var test_inst = {name: 'TEST'}; - var user = new User({ - institutions: [inst, other_inst, test_inst], - current_institution: inst - }); - - user.goToDifferentInstitution(inst.key, "true"); - - expect(user.current_institution).toBe(test_inst); - }); - }); - describe('addPermissions', function () { it('should add the permissions', function () { var user = new User({permissions: {}}); diff --git a/frontend/user/user.js b/frontend/user/user.js index fa6a32499..34ad3c567 100644 --- a/frontend/user/user.js +++ b/frontend/user/user.js @@ -171,19 +171,6 @@ User.prototype.updateInstProfile = function updateInstProfile(institution) { this.institution_profiles[index].institution.photo_url = institution.photo_url; }; -User.prototype.goToDifferentInstitution = function goToDifferentInstitution(previousKey, removeHierarchy) { - var user = this; - _.forEach(this.institutions, function(institution) { - if(institution.key !== previousKey) { - const institutionHasBeenDeleted = removeHierarchy === "true" && institution.parent_institution === previousKey; - if (!(institutionHasBeenDeleted)) { - user.current_institution = institution; - window.localStorage.userInfo = JSON.stringify(this); - } - } - }); -}; - User.prototype.getProfileColor = function getProfileColor() { const instKey = this.current_institution.key; return this.institution_profiles.reduce( From df54b1698542b4c860460fa333715af17e1103e6 Mon Sep 17 00:00:00 2001 From: Raoni Smaneoto Date: Thu, 26 Apr 2018 11:11:39 -0300 Subject: [PATCH 2/2] Fixing the conflits --- frontend/notification/notificationDirective.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/notification/notificationDirective.js b/frontend/notification/notificationDirective.js index 3676599ec..f4f8702c4 100644 --- a/frontend/notification/notificationDirective.js +++ b/frontend/notification/notificationDirective.js @@ -22,7 +22,7 @@ icon: "clear", action: function (properties, notification, event) { if (notification.status !== 'READ') { - return refreshUser(notification, true); + return refreshUser(notification); } } }, @@ -30,7 +30,7 @@ icon: "clear", action: function (properties, notification, event) { if (notification.status !== 'READ') { - return refreshUser(notification, true); + return refreshUser(notification); } } },