def as_dto(self) -> InterestDTO: """ Get the interest from the DB """ dto = InterestDTO() dto.id = self.id dto.name = self.name return dto
def get_interests_stats(user_id): # Get all projects that the user has contributed. stmt = (TaskHistory.query.with_entities( TaskHistory.project_id).distinct().filter( TaskHistory.user_id == user_id)) interests = (Interest.query.with_entities( Interest.id, Interest.name, func.count(distinct( project_interests.c.project_id)).label("count_projects"), ).join( project_interests, and_( Interest.id == project_interests.c.interest_id, project_interests.c.project_id.in_(stmt), ), ).group_by(Interest.id).order_by(desc("count_projects")).all()) interests_dto = [ InterestDTO( dict(id=i.id, name=i.name, count_projects=i.count_projects)) for i in interests ] return interests_dto
def get_all_interests(): interests = (Interest.query.with_entities( Interest.id, Interest.name, func.count(distinct( user_interests.c.user_id)).label("count_users"), func.count(distinct( project_interests.c.project_id)).label("count_projects"), ).outerjoin( user_interests, Interest.id == user_interests.c.interest_id).outerjoin( project_interests, Interest.id == project_interests.c.interest_id).group_by( Interest.id).order_by(Interest.id).all()) dto = InterestsDTO() dto.interests = [ InterestDTO( dict( id=i.id, name=i.name, count_projects=i.count_projects, count_users=i.count_users, )) for i in interests ] return dto
def _get_project_and_base_dto(self): """ Populates a project DTO with properties common to all roles """ base_dto = ProjectDTO() base_dto.project_id = self.id base_dto.project_status = ProjectStatus(self.status).name base_dto.default_locale = self.default_locale base_dto.project_priority = ProjectPriority(self.priority).name base_dto.area_of_interest = self.get_aoi_geometry_as_geojson() base_dto.aoi_bbox = shape(base_dto.area_of_interest).bounds base_dto.mapping_permission = MappingPermission( self.mapping_permission).name base_dto.validation_permission = ValidationPermission( self.validation_permission).name base_dto.enforce_random_task_selection = self.enforce_random_task_selection base_dto.private = self.private base_dto.mapper_level = MappingLevel(self.mapper_level).name base_dto.changeset_comment = self.changeset_comment base_dto.osmcha_filter_id = self.osmcha_filter_id base_dto.due_date = self.due_date base_dto.imagery = self.imagery base_dto.josm_preset = self.josm_preset base_dto.id_presets = self.id_presets base_dto.country_tag = self.country base_dto.organisation_id = self.organisation_id base_dto.license_id = self.license_id base_dto.created = self.created base_dto.last_updated = self.last_updated base_dto.author = User.get_by_id(self.author_id).username base_dto.active_mappers = Project.get_active_mappers(self.id) base_dto.task_creation_mode = TaskCreationMode( self.task_creation_mode).name base_dto.percent_mapped = Project.calculate_tasks_percent( "mapped", self.total_tasks, self.tasks_mapped, self.tasks_validated, self.tasks_bad_imagery, ) base_dto.percent_validated = Project.calculate_tasks_percent( "validated", self.total_tasks, self.tasks_mapped, self.tasks_validated, self.tasks_bad_imagery, ) base_dto.percent_bad_imagery = Project.calculate_tasks_percent( "bad_imagery", self.total_tasks, self.tasks_mapped, self.tasks_validated, self.tasks_bad_imagery, ) base_dto.project_teams = [ ProjectTeamDTO( dict( team_id=t.team.id, team_name=t.team.name, role=TeamRoles(t.role).name, )) for t in self.teams ] if self.custom_editor: base_dto.custom_editor = self.custom_editor.as_dto() if self.private: # If project is private it should have a list of allowed users allowed_usernames = [] for user in self.allowed_users: allowed_usernames.append(user.username) base_dto.allowed_usernames = allowed_usernames if self.mapping_types: mapping_types = [] for mapping_type in self.mapping_types: mapping_types.append(MappingTypes(mapping_type).name) base_dto.mapping_types = mapping_types if self.campaign: base_dto.campaigns = [i.as_dto() for i in self.campaign] if self.mapping_editors: mapping_editors = [] for mapping_editor in self.mapping_editors: mapping_editors.append(Editors(mapping_editor).name) base_dto.mapping_editors = mapping_editors if self.validation_editors: validation_editors = [] for validation_editor in self.validation_editors: validation_editors.append(Editors(validation_editor).name) base_dto.validation_editors = validation_editors if self.priority_areas: geojson_areas = [] for priority_area in self.priority_areas: geojson_areas.append(priority_area.get_as_geojson()) base_dto.priority_areas = geojson_areas base_dto.interests = [ InterestDTO(dict(id=i.id, name=i.name)) for i in self.interests ] return self, base_dto
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