Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 6 additions & 22 deletions db_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
UserCourses,
UserDepartments,
UserMajors,
Codes
UserSavedOpportunities,
)

app = create_app()
Expand Down Expand Up @@ -115,20 +115,8 @@

lab_manager_rows = (
("led", "Duy", "Le", "CSCI", "database database database"),
(
"turner",
"Wes",
"Turner",
"CSCI",
"open source stuff is cool",
),
(
"kuzmin",
"Konstantine",
"Kuzmin",
"CSCI",
"java, psoft, etc.",
),
("turner", "Wes","Turner","CSCI","open source stuff is cool",),
("kuzmin","Konstantine","Kuzmin","CSCI","java, psoft, etc.",),
("goldd", "David", "Goldschmidt", "CSCI", "VIM master"),
("rami", "Rami", "Rami", "MTLE", "cubes are cool"),
("holm", "Mark", "Holmes", "MATH", "all about that math"),
Expand Down Expand Up @@ -411,12 +399,7 @@
db.session.add(row)
db.session.commit()

participates_rows = (
("cenzar", 1),
("cenzar", 2),
("test", 3),
("test", 4),
)
participates_rows = (("cenzar", 1),("cenzar", 2),("test", 3),("test", 4),)

for r in participates_rows:
row = Participates()
Expand All @@ -426,7 +409,7 @@
db.session.add(row)
db.session.commit()

tables = [
tables = [
ClassYears,
Courses,
Leads,
Expand All @@ -442,6 +425,7 @@
UserCourses,
UserDepartments,
UserMajors,
UserSavedOpportunities
]

for table in tables:
Expand Down
148 changes: 145 additions & 3 deletions labconnect/main/opportunity_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Courses,
Participates,
RecommendsMajors,
UserSavedOpportunities,
)

from . import main_blueprint
Expand Down Expand Up @@ -209,7 +210,8 @@ def getOpportunity(opp_id: int):
Opportunities.id == RecommendsClassYears.opportunity_id,
)
.where(Opportunities.id == opp_id)
.group_by(Opportunities.id)
.group_by(Opportunities.id)

)

data = query.all()
Expand Down Expand Up @@ -754,7 +756,8 @@ def editOpportunity_get(opportunity_id):
if credit
]

years = [str(year.class_year) for year in years_data] if years_data else []
years = [str(year.class_year) for year in years_data]
#if years_data else []

# Format opportunity data as JSON
opportunity_data = {
Expand Down Expand Up @@ -884,6 +887,7 @@ def editOpportunity(opportunity_id):

db.session.commit()

# data is causing errors
# Add the updated list of managers
# if "lab_manager_ids" in data:
# for lab_manager_id in data["lab_manager_ids"]:
Expand All @@ -892,8 +896,17 @@ def editOpportunity(opportunity_id):
# )
# db.session.add(new_lead)

# db.session.commit() # Commit all changes

# Atttempt to fix by replacing data with request_data
# Add the updated list of managers
if "lab_manager_ids" in request_data:
for lab_manager_id in request_data["lab_manager_ids"]:
new_lead = Leads(
lab_manager_id=lab_manager_id, opportunity_id=opportunity_id
)
db.session.add(new_lead)

db.session.commit()
return {"data": "Opportunity Updated"}, 200


Expand Down Expand Up @@ -930,3 +943,132 @@ def deleteOpportunity(opportunity_id):
db.session.commit()

return {"data": "Opportunity Deleted"}

# Store opportunities saved by a user
# ***Specificaly storing a individual users saved opportunities***
"""
Info based off of(delete later):
saved_opportunities = db.relationship(
"UserSavedOpportunities", back_populates="user"
)
lab_manager = db.relationship("LabManager", back_populates="user")
opportunities = db.relationship("Participates", back_populates="user")
year = db.relationship("ClassYears", back_populates="users")
departments = db.relationship("UserDepartments", back_populates="user")
majors = db.relationship("UserMajors", back_populates="user")
courses = db.relationship("UserCourses", back_populates="user")
"""
# Save User Saved Opportunity
@main_blueprint.post("/saveUserOpportunity/<int:opportunity_id>")
@jwt_required()
def saveUserOpportunity(opportunity_id):
data = request.get_json()
if not data:
abort(400, "Missing data")

save_opp_opportunity_id = db.session.get(Opportunities, opportunity_id)
if not save_opp_opportunity_id:
return {"error": "Opportunity not found"}, 404

save_opp_user_id = get_jwt_identity()

# Check if the opportunity already exists in saved opportunities
find_opp = db.session.execute(
db.select(UserSavedOpportunities).where(
(UserSavedOpportunities.user_id == save_opp_user_id) &
(UserSavedOpportunities.opportunity_id == save_opp_opportunity_id)
)
).scalar_one_or_none()

if find_opp:
return {"message": "Opportunity previously saved"}, 200

# Save the new opportunity
new_opp = UserSavedOpportunities(
user_id = save_opp_user_id,
opportunity_id = save_opp_opportunity_id
)
db.session.add(new_opp)
db.session.commit()

return {"message": "Opportunity saved successfully"}, 201

# Delete an opportunitiy saved by a user
@main_blueprint.delete("/deleteUserOpportunity/<int:opportunity_id>")
@jwt_required()
def deleteUserOpportunity(opportunity_id):
data = request.get_json()
if not data:
abort(400, "No data found")

save_opp_user_id = get_jwt_identity()
save_opp_opportunity_id = db.session.get(Opportunities, opportunity_id)
if not save_opp_opportunity_id:
return {"error": "Opportunity not found"}, 404

# Find the saved opportunity
get_saved_opp_info = db.session.execute(
db.select(UserSavedOpportunities).where(
(UserSavedOpportunities.user_id == save_opp_user_id) &
(UserSavedOpportunities.opportunity_id == save_opp_opportunity_id)
)
).scalar_one_or_none()

if not get_saved_opp_info:
return {"message": "Opportunity not found"}, 404

# Delete the opportunity
db.session.delete(get_saved_opp_info)
db.session.commit()

return {"message": "Opportunity deleted"}, 200

#Create route to return a list saved opportunities
@main_blueprint.get("/AllSavedUserOpportunities/")
@jwt_required()
def allSavedUserOportunities():
#Get current users ID
user_id = get_jwt_identity()

#Get all saved opportunities for the user
saved_opps = db.session.execute(
db.select(UserSavedOpportunities).where(
UserSavedOpportunities.user_id == user_id
)
).scalars().all()
if not saved_opps:
return {"message": "No saved opportunities found"}, 404

#Put opportunities into a dictionary
saved_opportunities_list = [opp.to_dict() for opp in saved_opps]

return saved_opportunities_list, 200

#Create route to allow for multiple pages to be unsaved given a list of opp_ids delete them
@main_blueprint.delete("/UnsaveMultiplePages/")
@jwt_required()
#Delete id that appear on delete_ids list
def UnsaveMultipleOpps():
#Get a list of opportunity IDs
data = request.get_json()
delete_ids = data.get("delete_ids")
if not delete_ids or not isinstance(delete_ids, list):
return {"message": "Invalid or missing delete_ids"}, 400

#Get opportunities to delete for current user
user_id = get_jwt_identity()
saved_opps = db.session.execute(
db.select(UserSavedOpportunities).where(
UserSavedOpportunities.user_id == user_id,
UserSavedOpportunities.opportunity_id.in_(delete_ids)
)
).scalars().all()
if not saved_opps:
return {"message": "User has no saved opportunities"}, 404

#Delete the opportinities
for opp in saved_opps:
db.session.delete(opp)

db.session.commit()
return {"message": f"Deleted {len(saved_opps)} saved opportunities"}, 200
10 changes: 10 additions & 0 deletions labconnect/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,18 @@ class UserSavedOpportunities(db.Model):
)

user = db.relationship("User", back_populates="saved_opportunities")
#user = db.relationship("User", back_populates="opportunities")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These commented things were removed in main, they should be removed here too

opportunity = db.relationship("Opportunities", back_populates="saved_opportunities")

#__tablename__ = "participates"

#user_id = db.Column(db.String(9), db.ForeignKey("user.id"), primary_key=True)
#opportunity_id = db.Column(
#db.Integer, db.ForeignKey("opportunities.id"), primary_key=True
#)

#user = db.relationship("User", back_populates="opportunities")
#opportunity = db.relationship("Opportunities", back_populates="users")

class Leads(db.Model):
__tablename__ = "leads"
Expand Down