def post(self, user, institution_key):
        """Handler of post requests."""
        body = json.loads(self.request.body)
        data = body['data']
        host = self.request.host
        inst_request_type = 'REQUEST_INSTITUTION'

        type_of_invite = data.get('type_of_invite')

        Utils._assert(type_of_invite != inst_request_type,
                      "The type must be REQUEST_INSTITUTION", EntityException)

        user.name = data['admin']['name']
        user.put()

        inst_stub = createInstitution(user, data)
        data['sender_key'] = user.key.urlsafe()
        data['institution_key'] = inst_stub.key.urlsafe()
        data['admin_key'] = user.key.urlsafe()

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

        request.send_invite(host, user.current_institution)

        self.response.write(json.dumps(request.make()))
    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()))
def createInvite(data):
    """Create an invite."""
    invite = InviteFactory.create(data, data['type_of_invite'])
    invite.put()

    if (invite.stub_institution_key):
        invite.stub_institution_key.get().addInvite(invite)

    return invite
Exemple #5
0
def create_invite(admin, institution_key, type_of_invite, invitee_key=None):
    """Create an invite."""
    data = {
        'invitee': str(admin.email),
        'admin_key': admin.key.urlsafe(),
        'institution_key': institution_key.urlsafe(),
        'suggestion_institution_name': 'inst_test'
    }

    if invitee_key:
        data['invitee_key'] = invitee_key

    invite = InviteFactory.create(data, type_of_invite)
    invite_hash = getHash(invite)
    invite.sender_name = invite_hash
    invite.put()
    return invite
    def post(self, user):
        """Handle POST Requests.
        
        Creates a stub_institution, an institution with
        a pending state. It is made from the invite's data
        and can be accepted later.
        It is allowed only if the institution that sent the invite
        is not inactive and if the user has permission to send this
        kind of invite.
        """
        body = json.loads(self.request.body)
        data = body['data']
        host = self.request.host
        type_of_invite = data.get('type_of_invite')

        Utils._assert(type_of_invite != 'INSTITUTION',
                      "invitation type not allowed", NotAuthorizedException)

        invite = InviteFactory.create(data, type_of_invite)
        institution = invite.institution_key.get()
    
        user.check_permission(
            'send_invite_inst',
            'User is not allowed to post invite', 
            institution.key.urlsafe()
        )

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

        invite.put()
        invite.stub_institution_key.get().addInvite(invite)

        invite.send_invite(host, user.current_institution)

        make_invite = invite.make()

        self.response.write(json.dumps(make_invite))
Exemple #7
0
    def post(self, user, institution_key):
        """Handler of post requests."""
        body = json.loads(self.request.body)
        data = body['data']
        host = self.request.host
        user_request_type = 'REQUEST_USER'

        type_of_invite = data.get('type_of_invite')
        Utils._assert(type_of_invite != user_request_type,
                      "The type must be REQUEST_USER", EntityException)

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

        user.name = data.get('sender_name')
        user.put()

        if (request.stub_institution_key):
            request.stub_institution_key.get().addInvite(request)

        request.send_invite(host, user.current_institution)
        make_invite = request.make()

        self.response.write(json.dumps(make_invite))
Exemple #8
0
def createInvite(data):
    """Create an invite."""
    invite = InviteFactory.create(data, data['type_of_invite'])
    invite.put()

    return invite