diff --git a/labconnect/main/opportunity_routes.py b/labconnect/main/opportunity_routes.py index 9d69a212..8cd59f31 100644 --- a/labconnect/main/opportunity_routes.py +++ b/labconnect/main/opportunity_routes.py @@ -1,5 +1,3 @@ -import datetime - from flask import abort, request from flask_jwt_extended import get_jwt_identity, jwt_required @@ -19,28 +17,49 @@ from . import main_blueprint -@main_blueprint.route("/searchOpportunity/", methods=["GET"]) +@main_blueprint.get("/searchOpportunity/") def searchOpportunity(query: str): # Perform a search stmt = ( db.select(Opportunities) .where( + # Made query input ( - Opportunities.search_vector.match(query) + Opportunities.search_vector.match(input) ) # Full-text search using pre-generated tsvector | ( - db.func.similarity(Opportunities.name, query) >= 0.1 + db.func.similarity(Opportunities.name, input) >= 0.1 ) # Fuzzy search on the 'name' field | ( - db.func.similarity(Opportunities.description, query) >= 0.1 + db.func.similarity(Opportunities.description, input) >= 0.1 ) # Fuzzy search on the 'description' field ) .order_by( db.func.similarity( - Opportunities.name, query + Opportunities.name, input ).desc() # Order by similarity for fuzzy search results ) ) + # Perform a search + # stmt = ( + # db.select(Opportunities) + # .where( + # ( + # Opportunities.search_vector.match(input) + # ) # Full-text search using pre-generated tsvector + # | ( + # db.func.similarity(Opportunities.name, input) >= 0.1 + # ) # Fuzzy search on the 'name' field + # | ( + # db.func.similarity(Opportunities.description, input) >= 0.1 + # ) # Fuzzy search on the 'description' field + # ) + # .order_by( + # db.func.similarity( + # Opportunities.name, input + # ).desc() # Order by similarity for fuzzy search results + # ) + # ) data = db.session.execute(stmt).scalars().all() @@ -54,30 +73,24 @@ def searchOpportunity(query: str): # @main_blueprint.get("/opportunity") # def getOpportunity2(): - # if not request.data: # abort(400) - # json_request_data = request.get_json() - # if not json_request_data: # abort(400) - # id = json_request_data.get("id", None) - # if not id: # abort(400) - # data = db.first_or_404(db.select(Opportunities).where(Opportunities.id == id)) - # result = data.to_dict() - # return result def convert_to_enum(location_string): try: - return LocationEnum[location_string] # Use upper() for case-insensitivity + return LocationEnum[ + location_string.upper() + ] # Use upper() for case-insensitivity except KeyError: return None # Or raise an exception if you prefer @@ -86,7 +99,6 @@ def packageOpportunity(opportunityInfo, professorInfo): data = opportunityInfo.to_dict() data["professor"] = professorInfo.name data["department"] = professorInfo.department_id - return data @@ -99,6 +111,14 @@ def packageIndividualOpportunity(opportunityInfo): "author": "", "department": "", } + data = { + "id": opportunityInfo.id, + "name": opportunityInfo.name, + "description": opportunityInfo.description, + "recommended_experience": opportunityInfo.recommended_experience, + "author": "", + "department": "", + } opportunity_credits = "" if opportunityInfo.one_credit: @@ -145,18 +165,21 @@ def packageIndividualOpportunity(opportunityInfo): ) queryInfo = query.all() - print(queryInfo) + # print(queryInfo) if len(queryInfo) == 0: return data data["department"] = queryInfo[0][1].department_id - for i, item in enumerate(queryInfo): - data["author"] += item[1].getName() - # data["author"] += "look at def packageIndividualOpportunity(opportunityInfo):" - if i != len(queryInfo) - 1: - data["author"] += ", " + # for i, item in enumerate(queryInfo): + # data["author"] += item[1].getName() + # data["author"] += "look at def packageIndividualOpportunity(opportunityInfo):" + # if i != len(queryInfo) - 1: + # data["author"] += ", " + + author_names = [item[1].getName() for item in queryInfo] + data["author"] = ", ".join(author_names) if len(queryInfo) > 1: data["authorProfile"] = ( @@ -182,13 +205,7 @@ def packageOpportunityCard(opportunity): data = query.all() - professorInfo = "" - - for i, item in enumerate(data): - professor_name = f"{item[2]} {item[3]}" - professorInfo += professor_name - if i != len(data) - 1: - professorInfo += ", " + professorInfo = ", ".join(item[1].getName() for item in data) card = { "id": opportunity.id, @@ -202,7 +219,7 @@ def packageOpportunityCard(opportunity): return card -# @main_blueprint.route("/getOpportunity/", methods=["GET"]) +# @main_blueprint.get("/getOpportunity/") # def getOpportunity(opp_id: int): # # query database for opportunity # query = db.session.execute( @@ -216,7 +233,6 @@ def packageOpportunityCard(opportunity): # abort(404) # data = data[0] - # oppData = packageIndividualOpportunity(data[0]) # # return data in the below format if opportunity is found @@ -238,11 +254,10 @@ def packageOpportunityCard(opportunity): # return result -# -@main_blueprint.route("/opportunity/filter", methods=["POST"]) -def filterOpportunities(): - # Handle POST requests for filtering opportunities - json_request_data = request.get_json() +##@main_blueprint.route("/opportunity/filter", methods=["POST"]) +##def filterOpportunities(): +# Handle POST requests for filtering opportunities +##json_request_data = request.get_json() # if not json_request_data: @@ -394,66 +409,64 @@ def filterOpportunities(): # return {"msg": "Opportunity updated successfully"}, 200 -# @main_blueprint.route("/getOpportunityMeta/", methods=["GET"]) +# @main_blueprint.get("/getOpportunityMeta/") # def getOpportunityMeta(id: int): -# if request.method == "GET": -# query = db.session.execute( -# db.select( -# Opportunities, RecommendsMajors, RecommendsCourses, RecommendsClassYears -# ) -# .where(Opportunities.id == id) -# .join(RecommendsMajors, RecommendsMajors.opportunity_id == Opportunities.id) -# .join( -# RecommendsCourses, RecommendsCourses.opportunity_id == Opportunities.id -# ) -# .join( -# RecommendsClassYears, -# RecommendsClassYears.opportunity_id == Opportunities.id, -# ) +# query = db.session.execute( +# db.select( +# Opportunities, RecommendsMajors, RecommendsCourses, RecommendsClassYears # ) -# data = query.all() -# print(data) +# .where(Opportunities.id == id) +# .join(RecommendsMajors, RecommendsMajors.opportunity_id == Opportunities.id) +# .join(RecommendsCourses, RecommendsCourses.opportunity_id == Opportunities.id) +# .join( +# RecommendsClassYears, +# RecommendsClassYears.opportunity_id == Opportunities.id, +# ) +# ) +# data = query.all() +# print(data) -# if not data or len(data) == 0: -# abort(404) -# dictionary = data[0][0].to_dict() -# dictionary["semester"] = dictionary["semester"].upper() -# dictionary["courses"] = set() -# dictionary["majors"] = set() -# dictionary["years"] = set() +# if not data or len(data) == 0: +# abort(404) -# for row in data: -# dictionary["courses"].add(row[2].course_code) -# dictionary["majors"].add(row[1].major_code) -# dictionary["years"].add(row[3].class_year) +# dictionary = data[0][0].to_dict() +# dictionary["semester"] = dictionary["semester"].upper() +# dictionary["courses"] = set() +# dictionary["majors"] = set() +# dictionary["years"] = set() -# dictionary["courses"] = list(dictionary["courses"]) -# dictionary["majors"] = list(dictionary["majors"]) -# dictionary["years"] = list(dictionary["years"]) +# for row in data: +# dictionary["courses"].add(row[2].course_code) +# dictionary["majors"].add(row[1].major_code) +# dictionary["years"].add(row[3].class_year) -# for i in range(len(dictionary["years"])): -# dictionary["years"][i] = str(dictionary["years"][i]) +# dictionary["courses"] = list(dictionary["courses"]) +# dictionary["majors"] = list(dictionary["majors"]) +# dictionary["years"] = list(dictionary["years"]) -# dictionary["credits"] = [] -# if dictionary["one_credit"]: -# dictionary["credits"].append("1") +# for i in range(len(dictionary["years"])): +# dictionary["years"][i] = str(dictionary["years"][i]) -# if dictionary["two_credits"]: -# dictionary["credits"].append("2") +# dictionary["credits"] = [] +# if dictionary["one_credit"]: +# dictionary["credits"].append("1") -# if dictionary["three_credits"]: -# dictionary["credits"].append("3") +# if dictionary["two_credits"]: +# dictionary["credits"].append("2") -# if dictionary["four_credits"]: -# dictionary["credits"].append("4") +# if dictionary["three_credits"]: +# dictionary["credits"].append("3") -# dictionary.pop("one_credit") -# dictionary.pop("two_credits") -# dictionary.pop("three_credits") -# dictionary.pop("four_credits") +# if dictionary["four_credits"]: +# dictionary["credits"].append("4") -# return {"data": dictionary} +# dictionary.pop("one_credit") +# dictionary.pop("two_credits") +# dictionary.pop("three_credits") +# dictionary.pop("four_credits") + +# return {"data": dictionary} # abort(500) @@ -467,24 +480,22 @@ def getOpportunityCards(): ) data = query.fetchall() - # return data in the below format if opportunity is found cards = {"data": [packageOpportunityCard(opportunity[0]) for opportunity in data]} return cards -# @main_blueprint.route("/getOpportunities", methods=["GET"]) +# @main_blueprint.get("/getOpportunities") # def getOpportunities(): -# if request.method == "GET": -# # query database for opportunity -# query = db.session.execute( -# db.select(Opportunities, Leads, LabManager) -# .join(Leads, Leads.opportunity_id == Opportunities.id) -# .join(LabManager, Leads.lab_manager_id == LabManager.id) -# ) -# data = query.all() -# print(data[0]) +# # # query database for opportunity +# query = db.session.execute( +# db.select(Opportunities, Leads, LabManager) +# .join(Leads, Leads.opportunity_id == Opportunities.id) +# .join(LabManager, Leads.lab_manager_id == LabManager.id) +# ) +# data = query.all() +# print(data[0]) # # return data in the below format if opportunity is found # return { @@ -496,16 +507,14 @@ def getOpportunityCards(): # abort(500) - -# @main_blueprint.route("/getOpportunityByProfessor/", methods=["GET"]) +# @main_blueprint.get("/getOpportunityByProfessor/") # def getOpportunityByProfessor(rcs_id: str): -# if request.method == "GET": -# # query database for opportunity -# query = db.session.execute( -# db.select(Opportunities, Leads) -# .where(Leads.lab_manager_id == rcs_id) -# .join(Opportunities, Leads.opportunity_id == Opportunities.id) -# ) +# # # query database for opportunity +# query = db.session.execute( +# db.select(Opportunities, Leads) +# .where(Leads.lab_manager_id == rcs_id) +# .join(Opportunities, Leads.opportunity_id == Opportunities.id) +# ) # data = query.all() # print(data) @@ -555,48 +564,45 @@ def getLabManagerOpportunityCards(rcs_id: str): return cards -# @main_blueprint.route("/getProfileOpportunities/", methods=["GET"]) +# @main_blueprint.get("/getProfileOpportunities/") # def getProfileOpportunities(rcs_id: str): -# if request.method == "GET": -# # query database for opportunity - -# query = db.session.execute( -# db.select(Opportunities, Leads) -# .where(Leads.lab_manager_id == rcs_id) -# .join(Opportunities, Leads.opportunity_id == Opportunities.id) -# ) +# # # query database for opportunity -# data = query.all() +# query = db.session.execute( +# db.select(Opportunities, Leads) +# .where(Leads.lab_manager_id == rcs_id) +# .join(Opportunities, Leads.opportunity_id == Opportunities.id) +# ) -# cards = {"data": []} +# data = query.all() -# for row in data: -# opportunity = row[0] +# cards = {"data": []} -# oppData = { -# "id": opportunity.id, -# "title": opportunity.name, -# "body": "Due " + str(opportunity.application_due), -# "attributes": [], -# "activeStatus": opportunity.active, -# } +# for row in data: +# opportunity = row[0] -# if opportunity.pay is not None and opportunity.pay > 0: -# oppData["attributes"].append("Paid") -# if ( -# opportunity.one_credit -# or opportunity.two_credits -# or opportunity.three_credits -# or opportunity.four_credits -# ): -# oppData["attributes"].append("Credits") +# oppData = { +# "id": opportunity.id, +# "title": opportunity.name, +# "body": "Due " + str(opportunity.application_due), +# "attributes": [], +# "activeStatus": opportunity.active, +# } -# cards["data"].append(oppData) +# if opportunity.pay is not None and opportunity.pay > 0: +# oppData["attributes"].append("Paid") +# if ( +# opportunity.one_credit +# or opportunity.two_credits +# or opportunity.three_credits +# or opportunity.four_credits +# ): +# oppData["attributes"].append("Credits") -# # return data in the below format if opportunity is found -# return cards +# cards["data"].append(oppData) -# abort(500) +# # return data in the below format if opportunity is found +# return cards # functions to create/edit/delete opportunities @@ -701,7 +707,6 @@ def editOpportunity_get(opportunity_id): return {"error": "Opportunity not found"}, 404 opportunity = opportunity[0] - print(opportunity) # Query related courses courses_data = db.session.execute( diff --git a/labconnect/main/routes.py b/labconnect/main/routes.py index 52456de2..5cfb9afc 100644 --- a/labconnect/main/routes.py +++ b/labconnect/main/routes.py @@ -1,28 +1,30 @@ -from typing import Any +# from typing import Any from flask import abort, request from flask_jwt_extended import get_jwt_identity, jwt_required from labconnect import db from labconnect.models import ( - ClassYears, - Courses, + # ClassYears, + # Courses, LabManager, Leads, - Majors, + # Majors, Opportunities, - Participates, - RecommendsClassYears, - RecommendsCourses, - RecommendsMajors, + # Participates, + # RecommendsClassYears, + # RecommendsCourses, + # RecommendsMajors, RPIDepartments, - RPISchools, + # RPISchools, User, - UserCourses, - UserDepartments, - UserMajors, + # UserCourses, + # UserDepartments, + # UserMajors, ) +# = not currently using + from . import main_blueprint @@ -45,7 +47,6 @@ def departmentCards(): } for department in data ] - return results