def get_mentor_by_uid(): uid = g.uid if request.method == 'GET': try: user = Users.select(Users.mentor_id).where(Users.uid == uid).get() return {'mentors': Mentor.select().where(Mentor.id == user.mentor_id).dicts().get()} except DoesNotExist: return {'mentors': []} elif request.method == 'PUT': mentor_data = request.json try: user = Users.select(Users.mentor_id).where(Users.uid == uid).get() Mentor.select().where(Mentor.id == user.mentor_id).dicts().get() exists = True except DoesNotExist: exists = False try: if exists: updated = Mentor.update(mentor_data).where(Mentor.id == user.mentor_id).execute() updated_user = Users.update(mentor_id=updated).where(Users.uid == uid).execute() else: updated = Mentor.insert(mentor_data).execute() updated_user = Users.insert(uid=uid, mentor_id=updated, is_manager=False, is_admin=False).execute() except Exception as e: return f'Failed with error: {e}', 409 return 'Success' if updated else 'Non updated'
def mentors(): if request.method == 'GET': return {'mentors': [mentor_dict for mentor_dict in Mentor.select().dicts()]} mentor_data = request.form if request.method == 'POST': try: Mentor.insert(mentor_data).execute() return 'Success' except IntegrityError: return 'Mentor exists (email)', 409
def search(query): result = "[" solutions_by_user = Solution.select().where(Solution.username.contains(query)) mentors = Mentor.select().where( (Mentor.username.contains(query)) & (Mentor.status == "accepted")) for solution in solutions_by_user: result += "{" + f"'solution_url': '{solution.url}'," \ f"'username': '******'" + "}" for mentor in mentors: result += "{" + f"'username': '******'," \ f"'profile_url': '{mentor.profile_link}'" + "}" result += "]" return json.dumps(result)