Exemple #1
0
    def test_has_connection_between(self):
        #  create hierarchy
        # institution -> child_a -> child_b -> child_c
        child_a = mocks.generate_child_to_parent(self.institution)
        child_b = mocks.generate_child_to_parent(child_a)
        child_c = mocks.generate_child_to_parent(child_b)
        independent_instituion = mocks.create_institution()

        # update institution
        self.institution = self.institution.key.get()

        # verifies the hierarchy
        self.assertEquals(self.institution.parent_institution, None,
                          "institution should not have a parent")
        self.assertEquals(child_a.parent_institution, self.institution.key,
                          "institution should be parent of child_a")
        self.assertEquals(child_b.parent_institution, child_a.key,
                          "child_a should be parent of child_b")
        self.assertEquals(child_c.parent_institution, child_b.key,
                          "child_b should be parent of child_c")
        self.assertEquals(independent_instituion.parent_institution, None,
                          "independent_institution should not have a parent")
        self.assertEquals(independent_instituion.children_institutions, [],
                          "independent_institution should not have children")

        # verifies the connections
        # Case 1: institutions directly connected
        self.assertTrue(
            Institution.has_connection_between(child_a.key,
                                               self.institution.key),
            "The connection between child_a and institution should be true")
        # Case 2: institutions indirectly connected
        self.assertTrue(
            Institution.has_connection_between(child_b.key,
                                               self.institution.key),
            "The connection between child_b and institution should be true")
        self.assertTrue(
            Institution.has_connection_between(child_c.key,
                                               self.institution.key),
            "The connection between child_c and institution should be true")
        # Case 3: institutions disconnected
        self.assertFalse(
            Institution.has_connection_between(independent_instituion.key,
                                               self.institution.key),
            "The connection between independent_instituion and institution should be false"
        )
        self.assertFalse(
            Institution.has_connection_between(self.institution.key,
                                               independent_instituion.key),
            "The connection between institution and independent_instituion should be false"
        )
    def post(self, user, institution_urlsafe):
        """Handler of post requests. This method is called when an
        institution requests to be parent of other institution."""
        user.check_permission('send_link_inst_request',
                              'User is not allowed to send request',
                              institution_urlsafe)

        data = json.loads(self.request.body)
        host = self.request.host
        inst_children_request_type = 'REQUEST_INSTITUTION_CHILDREN'

        type_of_invite = data.get('type_of_invite')

        Utils._assert(type_of_invite != inst_children_request_type,
                      "The type must be REQUEST_INSTITUTION_CHILDREN",
                      EntityException)

        parent_key = ndb.Key(urlsafe=institution_urlsafe)
        requested_inst_key = data.get('institution_requested_key')
        requested_inst_key = ndb.Key(urlsafe=requested_inst_key)

        Utils._assert(
            Institution.has_connection_between(parent_key, requested_inst_key),
            "Circular hierarchy not allowed", EntityException)

        request = InviteFactory.create(data, type_of_invite)
        request.put()

        institution_parent = parent_key.get()
        institution_parent.add_child(requested_inst_key)

        request.send_invite(host, user.current_institution)

        self.response.write(json.dumps(request.make()))
    def post(self, user, institution_urlsafe):
        """Handle POST requests.
        It sends a request to the requested_institution
        to be parent of child_inst, whose key representation
        is institution_urlsafe. It can only be done if the user
        has permission to send the request, if the child_institution
        has no parent and if the two institutions are not linked yet.
        """
        user.check_permission('send_link_inst_request',
                              'User is not allowed to send request',
                              institution_urlsafe)

        data = json.loads(self.request.body)
        host = self.request.host
        inst_parent_request_type = 'REQUEST_INSTITUTION_PARENT'

        type_of_invite = data.get('type_of_invite')

        Utils._assert(type_of_invite != inst_parent_request_type,
                      "The type must be REQUEST_INSTITUTION_PARENT",
                      EntityException)

        child_key = ndb.Key(urlsafe=institution_urlsafe)
        requested_inst_key = data.get('institution_requested_key')
        requested_inst_key = ndb.Key(urlsafe=requested_inst_key)

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

        Utils._assert(
            Institution.has_connection_between(requested_inst_key, child_key),
            "Circular hierarchy not allowed", EntityException)

        request = InviteFactory.create(data, type_of_invite)

        @ndb.transactional(retries=10, xg=True)
        def main_operations(request, requested_inst_key, child_institution,
                            user, host):

            child_institution.parent_institution = requested_inst_key
            child_institution.put()

            if child_institution.key in requested_inst_key.get(
            ).children_institutions:
                remake_link(request, requested_inst_key, child_institution,
                            user)
            else:
                request.put()
                request.send_invite(host, user.current_institution)

            return request

        request = main_operations(request, requested_inst_key,
                                  child_institution, user, host)

        self.response.write(json.dumps(request.make()))