def filter_users(user_filter: str, project_id: int, page: int) -> UserFilterDTO: """ Finds users that matches first characters, for auto-complete. Users who have participated (mapped or validated) in the project, if given, will be returned ahead of those who have not. """ # Note that the projects_mapped column includes both mapped and validated projects. results = db.session.query(User.username, User.projects_mapped.any(project_id).label("participant")) \ .filter(User.username.ilike(user_filter.lower() + '%')) \ .order_by(desc("participant").nullslast(), User.username).paginate(page, 20, True) if results.total == 0: raise NotFound() dto = UserFilterDTO() for result in results.items: dto.usernames.append(result.username) if project_id is not None: participant = ProjectParticipantUser() participant.username = result.username participant.project_id = project_id participant.is_participant = bool(result.participant) dto.users.append(participant) dto.pagination = Pagination(results) return dto
def get_all_users(query: UserSearchQuery) -> UserSearchDTO: """ Search and filter all users """ # Base query that applies to all searches base = db.session.query(User.id, User.username, User.mapping_level, User.role) # Add filter to query as required if query.mapping_level: base = base.filter(User.mapping_level == MappingLevel[ query.mapping_level.upper()].value) if query.username: base = base.filter( User.username.ilike(query.username.lower() + '%')) if query.role: base = base.filter(User.role == UserRole[query.role.upper()].value) results = base.order_by(User.username).paginate(query.page, 20, True) dto = UserSearchDTO() for result in results.items: listed_user = ListedUser() listed_user.id = result.id listed_user.mapping_level = MappingLevel(result.mapping_level).name listed_user.username = result.username listed_user.role = UserRole(result.role).name dto.users.append(listed_user) dto.pagination = Pagination(results) return dto
def filter_users(user_filter: str, page: int) -> UserFilterDTO: """ Finds users that matches first characters, for auto-complete """ results = db.session.query(User.username).filter(User.username.ilike(user_filter.lower() + '%')) \ .order_by(User.username).paginate(page, 20, True) if results.total == 0: raise NotFound() dto = UserFilterDTO() for result in results.items: dto.usernames.append(result.username) dto.pagination = Pagination(results) return dto