Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 All @@ -41,7 +41,7 @@
if db.inspect(db.engine).get_table_names():
print("Tables already exist.")
# clear the codes table
db.session.query(Codes).delete()

Check failure on line 44 in db_init.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

db_init.py:44:30: F821 Undefined name `Codes`
db.session.commit()
sys.exit()
db.create_all()
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
91 changes: 89 additions & 2 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,22 @@ def editOpportunity(opportunity_id):
# )
# db.session.add(new_lead)


# 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() # Commit all changes


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


return {"data": "Opportunity Updated"}, 200


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

return {"data": "Opportunity Deleted"}

#////////////////////////////////////////////////
# Store opportunities saved by a user
# ***Specificaly storing a individual users saved opportunities***

# Save User Opportunity
@main_blueprint.post("/saveUserOpportunity/<int:opportunity_id>")
@jwt_required()
def saveUserOpportunity(opportunity_id):
data = request.get_json()
if not data:
abort(400, "Missing JSON 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 already 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, "Missing JSON data")

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
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")
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
Loading