def get(self, code): """ Accept an invitation. If an invitation's state is 'pending', this endpoint will set the invitation's state to 'accepted' and the authenticated user will be added as a partner to the associated organization. If an invitation's state is 'accepted' or 'cancelled', the invitation cannot be accepted again or accepted at all. In order to accept an invitation, the user must be an authenticated user. Params: code: The code of the invitation to accept """ invitation = InvitationModel.query.filter_by(code=code).first() if invitation is None: raise errors.EntityNotFoundError('invitation', code) user = UserModel.query.filter_by(google_id=g.user['google_id']).first() if user is None: raise errors.EntityNotFoundError('user', g.user['google_id']) PartnerModel(PartnerType.MEMBER, user.firstname, user.lastname, user.email, user, invitation.organization, invitation.id) invitation.status = InvitationStatus.ACCEPTED db.session.commit() data = invitation.serialize return {'success': True, 'data': data}, 200
def post(self, organization_id): """ Invite a user to an organization. This endpoint will send an invitation to a given email address. The newly-created invitation will be in the 'pending' state until the user accepts the invitation. At this point the invitation will transition to the 'accepted' state and the user will be added as a new partner to the organization. In order to invite a user to an organization, the authenticated user must be an admin of the organization. Params: organization_id: The id of the organization for which to invite the user email: The email address the invitation will be sent to """ organization = OrganizationModel.query.get(organization_id) if organization is None: raise errors.EntityNotFoundError('organization', organization_id) parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument('email', required=True) args = parser.parse_args() invitation = InvitationModel(args['email'], organization.id) organization.invitations.append(invitation) db.session.add(invitation) db.session.commit() return {'success': True, 'data': invitation.serialize}, 200
def post(self): """ Create an organization. This endpoint creates a new organization and adds the authenticated user as an admin to the organization. Params: name: The name of the organization """ user = UserModel.query.filter_by(google_id=g.user['google_id']).first() if user is None or user.is_deleted is True: raise errors.EntityNotFoundError('user', g.user['google_id']) parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument('name', required=True) args = parser.parse_args() organization = OrganizationModel(args['name']) PartnerModel(PartnerType.ADMIN, user.firstname, user.lastname, user.email, user, organization) db.session.commit() return {'success': True, 'data': organization.serialize}, 200
def delete(self, user_id): user = UserModel.query.get(user_id) if user == None: raise errors.EntityNotFoundError('User', user_id) db.session.delete(user) db.session.commit() return {'users': 'delete'}
def put(self, partner_id): """ Edit a partner. In order to edit a partner, the authenticated user must be an admin of the organization that the partner is associated with. Params: partner_id: The id of the partner to edit """ partner = PartnerModel.query.get(partner_id) if partner is None: raise errors.EntityNotFoundError('partner', partner_id) parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument('firstname', required=True) parser.add_argument('lastname', required=True) parser.add_argument('email', required=True) args = parser.parse_args() partner.firstname = args['firstname'] partner.lastname = args['lastname'] partner.email = args['email'] db.session.commit() return {'success': True, 'data': partner.serialize}, 200
def delete(self, organization_id): """ Delete an organization. This endpoint sets the organization's state to 'deleted', so that it cannot be accessed by its members or admins in any way. In order to delete an organization, the authenticated user must be an admin of the organization. Params: organization_id: The id of the organization to delete """ organization = OrganizationModel.query.get(organization_id) if organization is None: raise errors.EntityNotFoundError('organization', organization_id) organization.is_deleted = True for partner in organization.partners: partner.is_deleted = True db.session.commit() return {'success': True, 'data': organization.serialize}, 200
def put(self, organization_id): """ Edit an organization. In order to edit an organization, the authenticated user must be an admin of the organization. Params: organization_id: The id of the organization to edit name: The name of the organization """ organization = OrganizationModel.query.get(organization_id) if organization is None: raise errors.EntityNotFoundError('organization', organization_id) parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument('name', required=True) args = parser.parse_args() organization.name = args['name'] db.session.commit() return {'success': True, 'data': organization.serialize}, 200
def put(self): """ Edit the authenticated user. Params: firstname: The firstname of the authenticated user lastname: The lastname of the authenticated user email: The email address of the authenticated user """ user = UserModel.query.filter_by(google_id=g.user['google_id']).first() if user is None or user.is_deleted is True: raise errors.EntityNotFoundError('user', g.user['google_id']) parser = reqparse.RequestParser(bundle_errors=True) parser.add_argument('firstname', required=True) parser.add_argument('lastname', required=True) parser.add_argument('email', required=True) args = parser.parse_args() user.firstname = args['firstname'] user.lastname = args['lastname'] user.email = args['email'] db.session.commit() return {'success': True, 'data': user.serialize}, 200
def delete(self, invitation_id): """ Delete an invitation. If an invitation's state is 'pending', this endpoint will set the invitation's state to 'cancelled'. If an invitation's state is 'accepted' or 'cancelled', the invitation cannot be deleted at all or deleted again. In order to delete an invitation, the authenticated user must be an admin of the organization that the invitation is associated with. Params: invitation_id: The id of the invitation to delete """ invitation = InvitationModel.query.get(invitation_id) if invitation is None: raise errors.EntityNotFoundError('invitation', invitation_id) if invitation.status == InvitationStatus.ACCEPTED: raise errors.EntityNotModifiedError('The invitation has already ' 'been accepted and cannot be ' 'deleted.') invitation.status = InvitationStatus.CANCELLED db.session.commit() data = invitation.serialize return {'success': True, 'data': data}, 200
def put(self, user_id): args = parser.parse_args() user = UserModel.query.get(user_id) if user == None: raise errors.EntityNotFoundError('User', user_id) user.firstname = args['firstname'] user.lastname = args['lastname'] user.email = args['email'] db.session.commit() return {'users': 'update'}
def get(self): """ Retrieve the authenticated user. """ user = UserModel.query.filter_by(google_id=g.user['google_id']).first() if user is None or user.is_deleted is True: raise errors.EntityNotFoundError('user', g.user['google_id']) return {'success': True, 'data': user.serialize}, 200
def get(self, organization_id): """ Retrieve an organization. In order to retrieve an organization, the authenticated user must be a member or an admin of the organization. Params: organization_id: The id of the organization to retrieve """ organization = OrganizationModel.query.get(organization_id) if organization is None: raise errors.EntityNotFoundError('organization', organization_id) return {'success': True, 'data': organization.serialize}, 200
def get(self): """ List organizations for the authenticated user. This endpoint only lists organizations that the authenticated user is allowed to operate on as a member or an admin. """ user = UserModel.query.filter_by(google_id=g.user['google_id']).first() if user is None or user.is_deleted is True: raise errors.EntityNotFoundError('user', g.user['google_id']) organizations = db.session.query(OrganizationModel).filter( OrganizationModel.partners.any(is_deleted=False, user=user)) data = [item.serialize for item in organizations] return {'success': True, 'data': data}, 200
def get(self, invitation_id): """ Retrieve an invitation. In order to retrieve an invitation, the authenticated user must be a member or an admin of the organization that the invitation is associated with. Params: invitation_id: The id of the invitation to retrieve """ invitation = InvitationModel.query.get(invitation_id) if invitation is None: raise errors.EntityNotFoundError('invitation', invitation_id) return {'success': True, 'data': invitation.serialize}, 200
def get(self, partner_id): """ Retrieve a partner. In order to retrieve a partner, the authenticated user must be a member or an admin of the organization that the partner is associated with. Params: partner_id: The id of the partner to retrieve """ partner = PartnerModel.query.get(partner_id) if partner is None: raise errors.EntityNotFoundError('partner', partner_id) return {'success': True, 'data': partner.serialize}, 200
def delete(self, partner_id): """ Delete a partner. In order to delete a partner, the authenticated user must be an admin of the organization that the partner is associated with. Params: partner_id: The id of the partner to delete """ partner = PartnerModel.query.get(partner_id) if partner is None: raise errors.EntityNotFoundError('partner', partner_id) partner.is_deleted = True db.session.commit() return {'success': True, 'data': partner.serialize}, 200
def get(self, organization_id): """ List members of an organization. This endpoint lists all partners with access through membership or with admin access to the organization, whether their state is 'active' or not. In order to list the members of an organization, the authenticated user must be a member or an admin of the organization. Params: organization_id: The id of the organization for which to list the members """ organization = OrganizationModel.query.get(organization_id) if organization is None: raise errors.EntityNotFoundError('organization', organization_id) data = [i.serialize for i in organization.partners] return {'success': True, 'data': data}, 200
def delete(self): """ Delete the authenticated user. This endpoint sets the authenticated user's account to 'closed' and the user's partnerships with organizations to 'inactive'. By signin-up again with the same google account, the user's account is reopened. To rejoin an organization, a new invitation is needed. """ user = UserModel.query.filter_by(google_id=g.user['google_id']).first() if user is None or user.is_deleted is True: raise errors.EntityNotFoundError('user', g.user['google_id']) user.is_deleted = True for partner in user.partners: partner.is_deleted = True db.session.commit() return {'success': True, 'data': user.serialize}, 200
def get(self, organization_id): """ List invitations to an organization. This endpoint lists all 'pending', 'accepted' and 'cancelled' invitations to an organization. In order to list invitations to an organization, the authenticated user must be a member or an admin of the organization. Params: organization_id: The id of the organization for which to list the invitations """ organization = OrganizationModel.query.get(organization_id) if organization is None: raise errors.EntityNotFoundError('organization', organization_id) invitations = InvitationModel.query.filter_by( organization_id=organization.id) data = [i.serialize for i in invitations] return {'success': True, 'data': data}, 200
def get(self, user_id): user = UserModel.query.get(user_id) if user == None: raise errors.EntityNotFoundError('User', user_id) return {'users': [user.serialize]}