Exemplo n.º 1
0
    def put(self, user, request_key):
        """Handler PUT Requests. Change status of parent_request from 'sent' to 'accepted'."""
        request = ndb.Key(urlsafe=request_key).get()
        user.check_permission(
            'answer_link_inst_request',
            'User is not allowed to accept link between institutions',
            request.institution_requested_key.urlsafe())
        request.change_status('accepted')

        parent_institution = request.institution_requested_key.get()
        parent_institution.add_child(request.institution_key)

        institution_children = request.institution_key.get()

        request.send_response_notification(user.current_institution, user.key,
                                           'ACCEPT')
        request.send_response_email('ACCEPT')

        notification = Notification(
            entity_key=institution_children.key.urlsafe(),
            receiver_key=user.key.urlsafe(),
            notification_type='ADD_ADM_PERMISSIONS',
            message=create_system_message(institution_children.key))

        notification_id = NotificationsQueueManager.create_notification_task(
            notification)
        enqueue_task(
            'add-admin-permissions', {
                'institution_key': institution_children.key.urlsafe(),
                'notifications_ids': [notification_id]
            })

        self.response.write(json.dumps(request.make()))
Exemplo n.º 2
0
        def process_invites(emails, invite, current_institution_key):
            """
            This method creates and sends an invitation 
            to be a member of the institution to all incoming 
            emails per parameter.

            Params:
            emails -- Emails of the users to be invited.
            invite -- Data of the invitation to be created.
            current_institution_key -- Institution in which the administrator was when he sent the invitation.
            """
            current_invite = {}
            invites_keys = []
            for email in emails:
                invite['invitee'] = email
                current_invite = createInvite(invite)
                invites_keys.append(current_invite.key.urlsafe())
                invites.append({
                    'email': email,
                    'key': current_invite.key.urlsafe()
                })

            current_invite = current_invite or createInvite(invite)
            notification_id = current_invite.create_sent_invites_notification(
                current_institution_key)

            enqueue_task(
                'send-invite', {
                    'invites_keys': json.dumps(invites_keys),
                    'host': host,
                    'current_institution': current_institution_key.urlsafe(),
                    'notifications_ids': [notification_id]
                })
Exemplo n.º 3
0
    def post(self, user, post_key, comment_id=None, reply_id=None):
        """Handle POST Requests."""
        """This method is only meant to give like in post, comment or reply and send notification."""
        post = ndb.Key(urlsafe=post_key).get()
        Utils._assert(post.state == 'deleted', "This post has been deleted",
                      EntityException)
        if comment_id:
            comment = post.like_comment(user, comment_id, reply_id)

            notification_type = 'LIKE_COMMENT'
            user_is_the_author = comment['author_key'] == user.key.urlsafe()
            if not user_is_the_author:
                receiver_key = comment['author_key']
                notification_message = post.create_notification_message(
                    user_key=user.key,
                    current_institution_key=user.current_institution,
                    sender_institution_key=post.institution)
                send_message_notification(receiver_key=receiver_key,
                                          notification_type=notification_type,
                                          entity_key=post_key,
                                          message=notification_message)
        else:
            post = post.like(user.key)

            entity_type = 'LIKE_POST'
            params = {
                'receiver_key': post.author.urlsafe(),
                'sender_key': user.key.urlsafe(),
                'entity_key': post.key.urlsafe(),
                'entity_type': entity_type,
                'current_institution': user.current_institution.urlsafe(),
                'sender_institution_key': post.institution.urlsafe()
            }

            enqueue_task('post-notification', params)
Exemplo n.º 4
0
def setup_enqueue_process(invite, institution, user):
    """This method creates the notification and setups its
    sending process properly by calling add-permissions handler.

    Params:
    invite -- The invite that has been accepted by the user to create
    the new institution.
    institution -- The institution that has been created, when the invite was sent, 
    as a stub and now is active.
    user -- The user who received the invite and accepted it creating the institution.
    """
    notification_id = invite.create_accept_response_notification(
        'ACCEPT_INVITE_INSTITUTION',
        institution.key,
        invite.admin_key.urlsafe(),
        user
    )

    if invite.__class__.__name__ == 'InviteInstitutionParent':
        system_notification_id = invite.create_accept_response_notification(
            'ADD_ADM_PERMISSIONS',
            institution_key=institution.key,
            receiver_key_urlsafe=user.key.urlsafe()
        )
        enqueue_task('add-admin-permissions', {
            'institution_key': institution.key.urlsafe(),
            'notifications_ids': [notification_id, system_notification_id]
        })
    else:
        enqueue_task('add-admin-permissions', {
            'institution_key': institution.key.urlsafe(),
            'notifications_ids': [notification_id]
        })
    def put(self, user, request_key):
        """Handler PUT Requests. Change status of children_request from 'sent' to 'accepted'."""
        request = ndb.Key(urlsafe=request_key).get()
        user.check_permission(
            'answer_link_inst_request',
            'User is not allowed to accept link between institutions',
            request.institution_requested_key.urlsafe())

        Utils._assert(
            request.institution_requested_key.get().parent_institution != None,
            "The institution's already have a parent", NotAuthorizedException)

        request.change_status('accepted')

        institution_children = request.institution_requested_key.get()
        institution_children.set_parent(request.institution_key)

        request.send_response_notification(user.current_institution, user.key,
                                           'ACCEPT')
        request.send_response_email('ACCEPT')

        enqueue_task('add-admin-permissions',
                     {'institution_key': institution_children.key.urlsafe()})

        self.response.write(json.dumps(request.make()))
Exemplo n.º 6
0
    def post(self, user, post_key):
        """Handle Post Comments requests."""
        body = json.loads(self.request.body)
        comment_data = body['commentData']
        post = ndb.Key(urlsafe=post_key).get()
        institution = post.institution.get()

        Utils._assert(not institution.is_active(),
                      "This institution is not active", EntityException)
        Utils._assert(post.state == 'deleted', "This post has been deleted",
                      EntityException)

        comment = Comment.create(comment_data, user)
        post.add_comment(comment)
        entity_type = 'COMMENT'

        params = {
            'receiver_key': post.author.urlsafe(),
            'sender_key': user.key.urlsafe(),
            'entity_key': post.key.urlsafe(),
            'entity_type': entity_type,
            'current_institution': user.current_institution.urlsafe(),
            'sender_institution_key': post.institution.urlsafe()
        }
        enqueue_task('post-notification', params)

        self.response.write(json.dumps(Utils.toJson(comment)))
Exemplo n.º 7
0
    def delete(self, user, institution_children_urlsafe,
               institution_parent_urlsafe):
        """
        Handle delete parent link between institutions.

        This handler remove the parent link between two institutions.
        This handler is called by children institution to remove link with the parent.
        """
        institution_children = ndb.Key(
            urlsafe=institution_children_urlsafe).get()
        institution_parent = ndb.Key(urlsafe=institution_parent_urlsafe).get()

        Utils._assert(not type(institution_children) is Institution,
                      "Key is not an institution", EntityException)
        Utils._assert(not type(institution_parent) is Institution,
                      "Key is not an institution", EntityException)

        user.check_permission(
            'remove_link',
            "User is not allowed to remove link between institutions",
            institution_children_urlsafe)

        # Remove Parent
        institution_children.set_parent(None)
        admin = institution_parent.admin

        notification_id = create_notification(
            user=user,
            receiver_institution=institution_parent,
            sender_institution=institution_children,
            create_message=institution_children.create_notification_message)

        enqueue_task(
            'remove-admin-permissions', {
                'institution_key': institution_children.key.urlsafe(),
                'parent_key': institution_parent.key.urlsafe(),
                'notification_id': notification_id
            })

        email_sender = RequestLinkEmailSender(
            **{
                'institution_parent_name': institution_parent.name,
                'institution_parent_email':
                institution_parent.institutional_email,
                'institution_requested_key': institution_parent.key.urlsafe(),
                'institution_child_name': institution_children.name,
                'institution_child_email':
                institution_children.institutional_email,
                'subject': get_subject('REMOVED_LINK_EMAIL'),
                'receiver': admin.get().email[0],
                'html': 'removed_institutional_link.html'
            })
        email_sender.send_email()
Exemplo n.º 8
0
 def save_changes(user, actual_admin, invite, institution):
     user.add_institution_admin(institution.key)
     actual_admin.remove_institution_admin(institution.key)
     invite.change_status('accepted')
     
     enqueue_task(
         'transfer-admin-permissions', 
         {
             'institution_key': institution.key.urlsafe(), 
             'user_key': user.key.urlsafe()
         }
     )
     invite.send_response_notification(current_institution=user.current_institution, action='ACCEPT')
Exemplo n.º 9
0
        def save_changes(user, actual_admin, invite, institution):
            user.add_institution_admin(institution.key)
            actual_admin.remove_institution_admin(institution.key)
            invite.change_status('accepted')

            system_notification_id = invite.create_system_notification()
            notification_id = invite.create_accept_response_notification(
                user.current_institution)

            enqueue_task(
                'transfer-admin-permissions', {
                    'institution_key': institution.key.urlsafe(),
                    'user_key': user.key.urlsafe(),
                    'notifications_ids':
                    [system_notification_id, notification_id]
                })
Exemplo n.º 10
0
    def delete(self, user, institution_key):
        """
        Handle DELETE institution.

        This handler is responsible for deleting an institution.
        If the 'remove_hierarchy' parameter is true, it removes the child hierarchy.
        When removing an institution, the permissions of administrator,
        in relation to this institution, are removed from
        the administrators of the parents institutions.
        """
        remove_hierarchy = self.request.get('removeHierarchy')
        institution = ndb.Key(urlsafe=institution_key).get()

        user.check_permission(
            'remove_inst',
            'User is not allowed to remove institution',
            institution_key)

        Utils._assert(not type(institution) is Institution,
                      "Key is not an institution", EntityException)

        institution.remove_institution(remove_hierarchy, user)

        params = {
            'institution_key': institution_key,
            'remove_hierarchy': remove_hierarchy,
            'user_key': user.key.urlsafe(),
            'current_institution': user.current_institution.urlsafe()
        }

        enqueue_task('remove-inst', params)

        notification_entity = {
            'key': institution_key,
            'institution_name': institution.name,
            'remove_hierarchy': remove_hierarchy
        }

        notification_params = {
            "sender_key": user.key.urlsafe(),
            "entity_type": "DELETED_INSTITUTION",
            "institution_key": institution_key,
            "current_institution": user.current_institution.urlsafe(),
            "entity": json.dumps(notification_entity)
        }
        enqueue_task('notify-followers', notification_params)
Exemplo n.º 11
0
    def put(self, user, institution_key, inviteKey):
        """
        Handle PUT Requests.
        
        This method end up the institution's configurations 
        from its previously created stub. Besides, it marks the invite received as accepted and 
        adds the permissions to the parent admins if the institution created has a parent institution.
        """
        body = json.loads(self.request.body)
        data = body['data']

        institution = ndb.Key(urlsafe=institution_key).get()

        invite = ndb.Key(urlsafe=inviteKey).get()

        Utils._assert(invite.status == 'accepted',
                      "This invitation has already been accepted",
                      NotAuthorizedException)

        invite.status = 'accepted'
        invite.put()

        institution.createInstitutionWithStub(user, institution)

        user.name = data.get('sender_name')
        data_profile = {
            'office': 'Administrador',
            'institution_key': institution.key.urlsafe(),
            'institution_name': institution.name,
            'institution_photo_url': institution.photo_url
        }
        user.create_and_add_profile(data_profile)

        user.add_permissions(permissions.DEFAULT_ADMIN_PERMISSIONS,
                             institution.key.urlsafe())
        user.put()

        invite.send_response_notification(institution.key, user.key, 'ACCEPT')

        enqueue_task('add-admin-permissions',
                     {'institution_key': institution_key})

        institution_json = Utils.toJson(institution)
        self.response.write(json.dumps(institution_json))
    def put(self, user, request_key):
        """Handler PUT Requests. Change status of parent_request from 'sent' to 'accepted'."""
        request = ndb.Key(urlsafe=request_key).get()
        user.check_permission('answer_link_inst_request',
                              'User is not allowed to accept link between institutions',
                              request.institution_requested_key.urlsafe())
        request.change_status('accepted')

        parent_institution = request.institution_requested_key.get()
        parent_institution.add_child(request.institution_key)

        institution_children = request.institution_key.get()

        request.send_response_notification(user.current_institution, user.key, 'ACCEPT')
        request.send_response_email('ACCEPT')

        enqueue_task('add-admin-permissions', {'institution_key': institution_children.key.urlsafe()})

        self.response.write(json.dumps(request.make()))
def remake_link(request, requested_inst_key, child_institution, user):
    notification_message = create_message(
        sender_key=user.key,
        current_institution_key=child_institution.key,
        receiver_institution_key=requested_inst_key,
        sender_institution_key=child_institution.key)

    notification = Notification(
        entity_key=child_institution.key.urlsafe(),
        receiver_key=requested_inst_key.get().admin.urlsafe(),
        notification_type='RE_ADD_ADM_PERMISSIONS',
        message=notification_message)

    notification_id = NotificationsQueueManager.create_notification_task(
        notification)
    enqueue_task(
        'add-admin-permissions', {
            'institution_key': child_institution.key.urlsafe(),
            'notifications_ids': [notification_id]
        })

    request.change_status('accepted')
    def post(self, user):
        """Handler POST invites.
        
        This method creates invites for:
        New institution to be added in the hierarchy.
        """
        body = json.loads(self.request.body)
        data = body['data']
        host = self.request.host
        invite = data['invite_body']
        type_of_invite = invite.get('type_of_invite')

        # This pattern checks whether the invitation type is INSTITUTION_CHILDREN or INSTITUTION_PARENT
        invite_pattern = re.compile('^INSTITUTION_(CHILDREN|PARENT)$')
        Utils._assert(not invite_pattern.match(type_of_invite),
                      "invitation type not allowed", NotAuthorizedException)

        institution = ndb.Key(urlsafe=invite['institution_key']).get()
        can_invite_inst = user.has_permission("send_link_inst_invite",
                                              institution.key.urlsafe())

        Utils._assert(not can_invite_inst,
                      "User is not allowed to send hierarchy invites",
                      NotAuthorizedException)

        invite = createInvite(invite)
        enqueue_task(
            'send-invite', {
                'invites_keys': json.dumps([invite.key.urlsafe()]),
                'host': host,
                'current_institution': user.current_institution.urlsafe()
            })

        self.response.write(
            json.dumps({
                'msg': 'The invite are being processed.',
                'invite': invite.make()
            }))
Exemplo n.º 15
0
    def post(self, user):
        """Handle POST Requests."""
        body = json.loads(self.request.body)
        post_data = body['post']
        institution_key = post_data['institution']

        institution = ndb.Key(urlsafe=institution_key).get()

        Utils._assert(not institution.is_active(),
                      "This institution is not active", NotAuthorizedException)

        permission = get_permission(post_data)

        user.key.get().check_permission(
            permission, "You don't have permission to publish post.",
            institution_key)

        @ndb.transactional(xg=True, retries=10)
        def create_post(post_data, user, institution):
            created_post = PostFactory.create(post_data, user.key,
                                              institution.key)
            user.add_post(created_post)

            institution.add_post(created_post)

            return created_post

        post = create_post(post_data, user, institution)

        entity_type = PostFactory.get_type(post_data)

        params = {
            'sender_key': user.key.urlsafe(),
            'entity_key': post.key.urlsafe(),
            'entity_type': entity_type,
            'institution_key': post.institution.urlsafe(),
            'current_institution': user.current_institution.urlsafe()
        }

        enqueue_task('notify-followers', params)

        if (post.shared_post):
            shared_post = post.shared_post.get()
            entity_type = 'SHARED_POST'
            params = {
                'receiver_key': shared_post.author.urlsafe(),
                'sender_key': user.key.urlsafe(),
                'entity_key': shared_post.key.urlsafe(),
                'entity_type': entity_type,
                'current_institution': user.current_institution.urlsafe(),
                'sender_institution_key': shared_post.institution.urlsafe()
            }

            enqueue_task('post-notification', params)
        elif post.shared_event:
            shared_event = post.shared_event.get()
            if shared_event.author_key != user.key:
                notification_message = post.create_notification_message(
                    user_key=user.key,
                    current_institution_key=user.current_institution,
                    sender_institution_key=shared_event.institution_key)
                send_message_notification(
                    receiver_key=shared_event.author_key.urlsafe(),
                    notification_type='SHARED_EVENT',
                    entity_key=post.key.urlsafe(),
                    message=notification_message)

        self.response.write(json.dumps(post.make(self.request.host)))
Exemplo n.º 16
0
    def post(self, user):
        """Handle POST invites.
        
        This method creates invites for: 
        new institution administrators 
        and new members of the institution.
        """
        body = json.loads(self.request.body)
        data = body['data']
        host = self.request.host
        invite = data['invite_body']
        type_of_invite = invite.get('type_of_invite')

        # This pattern checks whether the invitation type is USER or USER_ADM
        invite_pattern = re.compile('^USER(_ADM$|$)')
        Utils._assert(not invite_pattern.match(type_of_invite),
                      "invitation type not allowed", NotAuthorizedException)

        institution = ndb.Key(urlsafe=invite['institution_key']).get()
        can_invite_members = user.has_permission("invite_members",
                                                 institution.key.urlsafe())

        Utils._assert(not can_invite_members,
                      "User is not allowed to send invites",
                      NotAuthorizedException)

        invites = []

        @ndb.transactional(xg=True, retries=10)
        def process_invites(emails, invite, current_institution_key):
            """
            This method creates and sends an invitation 
            to be a member of the institution to all incoming 
            emails per parameter.

            Params:
            emails -- Emails of the users to be invited.
            invite -- Data of the invitation to be created.
            current_institution_key -- Institution in which the administrator was when he sent the invitation.
            """
            current_invite = {}
            invites_keys = []
            for email in emails:
                invite['invitee'] = email
                current_invite = createInvite(invite)
                invites_keys.append(current_invite.key.urlsafe())
                invites.append({
                    'email': email,
                    'key': current_invite.key.urlsafe()
                })

            current_invite = current_invite or createInvite(invite)
            notification_id = current_invite.create_sent_invites_notification(
                current_institution_key)

            enqueue_task(
                'send-invite', {
                    'invites_keys': json.dumps(invites_keys),
                    'host': host,
                    'current_institution': current_institution_key.urlsafe(),
                    'notifications_ids': [notification_id]
                })

        # If the invitation was USER type, more than one invitation can be sent at the same time.
        if type_of_invite == 'USER':
            process_invites(data['emails'], invite, user.current_institution)
        else:
            invite = createInvite(invite)
            invites.append({
                'email': invite.invitee,
                'key': invite.key.urlsafe()
            })
            enqueue_task(
                'send-invite', {
                    'invites_keys': json.dumps([invite.key.urlsafe()]),
                    'host': host,
                    'current_institution': user.current_institution.urlsafe()
                })

        self.response.write(
            json.dumps({
                'msg': 'The invites are being processed.',
                'invites': invites
            }))
Exemplo n.º 17
0
    def delete(self, user, institution_key, institution_link):
        """
        Handle delete link between institutions.

        This handler remove the link between two institutions. 
        If the parameter isParent is true, it means that the removal 
        request has been made from a child institution, otherwise 
        the request has been made by a parent institution.
        """

        user.check_permission(
            'remove_link',
            "User is not allowed to remove link between institutions",
            institution_key)

        is_parent = self.request.get('isParent')
        # If isParent is true, this attribute
        # holds the reference of the child intitution.
        institution = ndb.Key(urlsafe=institution_key).get()
        # If isParent is true, this attribute
        # holds the reference of the parent intitution.
        institution_link = ndb.Key(urlsafe=institution_link).get()

        Utils._assert(not type(institution) is Institution,
                      "Key is not an institution", EntityException)
        Utils._assert(not type(institution_link) is Institution,
                      "Key is not an institution", EntityException)
        Utils._assert(institution.state == 'inactive',
                      "The institution has been deleted",
                      NotAuthorizedException)
        Utils._assert(institution_link.state == 'inactive',
                      "The institution has been deleted",
                      NotAuthorizedException)

        institution.remove_link(institution_link, is_parent)
        admin = institution_link.admin

        if is_parent == "true":
            enqueue_task(
                'remove-admin-permissions', {
                    'institution_key': institution.key.urlsafe(),
                    'parent_key': institution_link.key.urlsafe()
                })

            email_sender = RequestLinkEmailSender(
                **{
                    'institution_parent_name': institution_link.name,
                    'institution_parent_email':
                    institution_link.institutional_email,
                    'institution_requested_key':
                    institution_link.key.urlsafe(),
                    'institution_child_name': institution.name,
                    'institution_child_email': institution.institutional_email,
                    'subject': get_subject('REMOVED_LINK_EMAIL'),
                    'receiver': admin.get().email[0],
                    'html': 'removed_institutional_link.html'
                })
            email_sender.send_email()
        else:
            enqueue_task(
                'remove-admin-permissions', {
                    'institution_key': institution_link.key.urlsafe(),
                    'parent_key': institution.key.urlsafe()
                })

            email_sender = RequestLinkEmailSender(
                **{
                    'institution_parent_name': institution.name,
                    'institution_parent_email':
                    institution.institutional_email,
                    'institution_requested_key':
                    institution_link.key.urlsafe(),
                    'institution_child_name': institution_link.name,
                    'institution_child_email':
                    institution_link.institutional_email,
                    'subject': get_subject('REMOVED_LINK_EMAIL'),
                    'receiver': admin.get().email[0],
                    'html': 'removed_institutional_link.html'
                })
            email_sender.send_email()

        notification_type = 'REMOVE_INSTITUTION_LINK'

        notification_message = institution.create_notification_message(
            user_key=user.key,
            current_institution_key=user.current_institution,
            receiver_institution_key=institution_link.key,
            sender_institution_key=institution.key)
        send_message_notification(receiver_key=admin.urlsafe(),
                                  notification_type=notification_type,
                                  entity_key=institution_link.key.urlsafe(),
                                  message=notification_message)