def delete(self, interest_id): """ Delete a specified interest --- tags: - interests produces: - application/json parameters: - in: header name: Authorization description: Base64 encoded session token required: true type: string default: Token sessionTokenHere== - name: interest_id in: path description: Unique interest ID required: true type: integer default: 1 responses: 200: description: Interest deleted 401: description: Unauthorized - Invalid credentials 403: description: Forbidden 404: description: Interest not found 500: description: Internal Server Error """ try: orgs_dto = OrganisationService.get_organisations_managed_by_user_as_dto( tm.authenticated_user_id ) if len(orgs_dto.organisations) < 1: raise ValueError("User not a Org Manager") except ValueError as e: error_msg = f"InterestsAllAPI DELETE: {str(e)}" return {"Error": error_msg}, 403 try: InterestService.delete(interest_id) return {"Success": "Interest deleted"}, 200 except NotFound: return {"Error": "Interest Not Found"}, 404 except Exception as e: error_msg = f"License DELETE - unhandled error: {str(e)}" current_app.logger.critical(error_msg) return {"Error": error_msg}, 500
def get(self): """ Get all interests --- tags: - interests produces: - application/json parameters: - in: header name: Authorization description: Base64 encoded session token required: true type: string default: Token sessionTokenHere== responses: 200: description: List of interests 500: description: Internal Server Error """ try: interests = InterestService.get_all_interests() return interests.to_primitive(), 200 except Exception as e: error_msg = f"Interest GET - unhandled error: {str(e)}" current_app.logger.critical(error_msg) return {"Error": error_msg}, 500
def relationship_project(interests, project_id, ids): interest_ids = [interests[c]["id"] for c in ids] project_interests = InterestService.create_or_update_project_interests( project_id, interest_ids) project_interests_ids = [ i["id"] for i in project_interests.to_primitive()["interests"] ] return interest_ids, project_interests_ids
def relationship_user(interests, user_id, ids): interest_ids = [interests[c]["id"] for c in ids] user_interests = InterestService.create_or_update_user_interests( user_id, interest_ids) user_interests_ids = [ i["id"] for i in user_interests.to_primitive()["interests"] ] return interest_ids, user_interests_ids
def update_project_categories(filename): with open(filename, "r", encoding="ISO-8859-1", newline="") as csvfile: reader = csv.DictReader(csvfile) for row in reader: project_id = int(row.get("projectId")) primary_category = row.get("primaryCat") interest_ids = [] # Map only primary_category interest to projects if primary_category: try: interest = InterestService.get_by_name(primary_category) except NotFound: interest = InterestService.create(primary_category) interest_ids.append(interest.id) try: InterestService.create_or_update_project_interests( project_id, interest_ids) except Exception as e: print(f"Problem updating {project_id}: {type(e)}")
def get(self, interest_id): """ Get an existing interest --- tags: - interests produces: - application/json parameters: - in: header name: Authorization description: Base64 encoded session token required: true type: string default: Token sessionTokenHere== - name: interest_id in: path description: Interest ID required: true type: integer default: 1 responses: 200: description: Interest 400: description: Invalid Request 401: description: Unauthorized - Invalid credentials 403: description: Forbidden 500: description: Internal Server Error """ try: orgs_dto = OrganisationService.get_organisations_managed_by_user_as_dto( tm.authenticated_user_id ) if len(orgs_dto.organisations) < 1: raise ValueError("User not a Org Manager") except ValueError as e: error_msg = f"InterestsRestAPI GET: {str(e)}" return {"Error": error_msg}, 403 try: interest = InterestService.get(interest_id) return interest.to_primitive(), 200 except Exception as e: error_msg = f"Interest GET - unhandled error: {str(e)}" current_app.logger.critical(error_msg) return {"Error": error_msg}, 500
def post(self): """ Creates a relationship between user and interests --- tags: - interests produces: - application/json parameters: - in: header name: Authorization description: Base64 encoded session token required: true type: string default: Token sessionTokenHere== - in: body name: body required: true description: JSON object for creating/updating user and interests relationships schema: properties: interests: type: array items: type: integer responses: 200: description: New user interest relationship created 400: description: Invalid Request 401: description: Unauthorized - Invalid credentials 500: description: Internal Server Error """ try: data = request.get_json() user_interests = InterestService.create_or_update_user_interests( tm.authenticated_user_id, data["interests"]) return user_interests.to_primitive(), 200 except ValueError as e: return {"Error": str(e)}, 400 except NotFound: return {"Error": "Interest not Found"}, 404 except Exception as e: error_msg = f"User relationship POST - unhandled error: {str(e)}" current_app.logger.critical(error_msg) return {"Error": error_msg}, 500
def get(self): """ Get all interests --- tags: - interests produces: - application/json responses: 200: description: List of interests 500: description: Internal Server Error """ try: interests = InterestService.get_all_interests() return interests.to_primitive(), 200 except Exception as e: error_msg = f"Interest GET - unhandled error: {str(e)}" current_app.logger.critical(error_msg) return {"Error": error_msg}, 500
def get(self, user_id): """ Get rate of contributions from a user given their interests --- tags: - interests produces: - application/json parameters: - in: header name: Authorization description: Base64 encoded session token required: true type: string default: Token sessionTokenHere== - name: user_id in: path description: User ID required: true type: integer responses: 200: description: Interest found 401: description: Unauthorized - Invalid credentials 500: description: Internal Server Error """ try: rate = InterestService.compute_contributions_rate(user_id) return rate.to_primitive(), 200 except NotFound: return {"Error": "User not Found"}, 404 except Exception as e: error_msg = f"Interest GET - unhandled error: {str(e)}" current_app.logger.critical(error_msg) return {"Error": error_msg}, 500
def post(self, project_id): """ Creates a relationship between project and interests --- tags: - interests produces: - application/json parameters: - in: header name: Authorization description: Base64 encoded session token required: true type: string default: Token sessionTokenHere== - name: project_id in: path description: Unique project ID required: true type: integer default: 1 - in: body name: body required: true description: JSON object for creating/updating project and interests relationships schema: properties: interests: type: array items: type: integer responses: 200: description: New project interest relationship created 400: description: Invalid Request 401: description: Unauthorized - Invalid credentials 403: description: Forbidden 500: description: Internal Server Error """ try: ProjectAdminService.is_user_action_permitted_on_project( token_auth.current_user(), project_id) except ValueError as e: error_msg = f"ProjectsActionsSetInterestsAPI POST: {str(e)}" return {"Error": error_msg}, 403 try: data = request.get_json() project_interests = InterestService.create_or_update_project_interests( project_id, data["interests"]) return project_interests.to_primitive(), 200 except NotFound: return {"Error": "Project not Found"}, 404 except Exception as e: error_msg = ( f"ProjectsActionsSetInterestsAPI POST - unhandled error: {str(e)}" ) current_app.logger.critical(error_msg) return {"Error": error_msg}, 500
def test_interests(self): if self.skip_tests: return interests = ["interest_number_{0}".format(i) for i in range(1, 9)] [InterestService.create(interest_name=i) for i in interests] interests = InterestService.get_all_interests().to_primitive( )["interests"] self.assertEqual(len(interests), 8) # Create new interest. new_interest = InterestService.create( interest_name="test_new_interest") interests = InterestService.get_all_interests().to_primitive( )["interests"] self.assertEqual(len(interests), 9) # Update interest new_interest.name = "new_interest_name" updated_interest = InterestService.update(new_interest.id, new_interest) self.assertEqual(new_interest.name, updated_interest.name) # Associate users with interest. iids, uids = TestInterestService.relationship_user( interests, self.test_user.id, [1, 4, 5]) self.assertEqual(uids, iids) # Try again. iids, uids = TestInterestService.relationship_user( interests, self.test_user.id, [1, 2]) self.assertEqual(uids, iids) # Validate unexistent interest. with self.assertRaises(NotFound): InterestService.create_or_update_user_interests( self.test_user.id, [0]) # Associate projects with interest. iids, pids = TestInterestService.relationship_project( interests, self.test_project.id, [1, 2]) self.assertEqual(pids, iids) # Validate unexistent interest. with self.assertRaises(NotFound): InterestService.create_or_update_project_interests( self.test_project.id, [0]) # Delete one by one. for interest in interests: InterestService.delete(interest["id"]) interests = InterestService.get_all_interests().to_primitive( )["interests"] self.assertEqual(len(interests), 0)
def patch(self, interest_id): """ Update an existing interest --- tags: - interests produces: - application/json parameters: - in: header name: Authorization description: Base64 encoded session token required: true type: string default: Token sessionTokenHere== - name: interest_id in: path description: Interest ID required: true type: integer default: 1 - in: body name: body required: true description: JSON object for creating a new interest schema: properties: name: type: string default: Public Domain responses: 200: description: Interest updated 400: description: Invalid Request 401: description: Unauthorized - Invalid credentials 403: description: Forbidden 500: description: Internal Server Error """ try: orgs_dto = OrganisationService.get_organisations_managed_by_user_as_dto( token_auth.current_user()) if len(orgs_dto.organisations) < 1: raise ValueError("User not a Org Manager") except ValueError as e: error_msg = f"InterestsAllAPI PATCH: {str(e)}" return {"Error": error_msg}, 403 try: interest_dto = InterestDTO(request.get_json()) interest_dto.validate() except DataError as e: current_app.logger.error(f"Error validating request: {str(e)}") return str(e), 400 try: update_interest = InterestService.update(interest_id, interest_dto) return update_interest.to_primitive(), 200 except Exception as e: error_msg = f"Interest PUT - unhandled error: {str(e)}" current_app.logger.critical(error_msg) return {"Error": error_msg}, 500
def post(self): """ Creates a new interest --- tags: - interests produces: - application/json parameters: - in: header name: Authorization description: Base64 encoded session token required: true type: string default: Token sessionTokenHere== - in: body name: body required: true description: JSON object for creating a new interest schema: properties: name: type: string default: Public Domain responses: 200: description: New interest created 400: description: Invalid Request 401: description: Unauthorized - Invalid credentials 403: description: Forbidden 500: description: Internal Server Error """ try: orgs_dto = OrganisationService.get_organisations_managed_by_user_as_dto( tm.authenticated_user_id ) if len(orgs_dto.organisations) < 1: raise ValueError("User not a Org Manager") except ValueError as e: error_msg = f"InterestsAllAPI POST: {str(e)}" return {"Error": error_msg}, 403 try: interest_dto = InterestDTO(request.get_json()) interest_dto.validate() except DataError as e: current_app.logger.error(f"Error validating request: {str(e)}") return str(e), 400 try: new_interest = InterestService.create(interest_dto.name) return new_interest.to_primitive(), 200 except IntegrityError: return ( {"error": "Value '{0}' already exists".format(interest_dto.name)}, 400, ) except Exception as e: error_msg = f"Interest POST - unhandled error: {str(e)}" current_app.logger.critical(error_msg) return {"Error": error_msg}, 500