Skip to content

Commit

Permalink
Create department routes (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelCenzano authored Oct 16, 2024
2 parents 474d2e1 + 00bc21d commit 5055ce5
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 63 deletions.
61 changes: 29 additions & 32 deletions db_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,37 @@
db.session.commit()

rpi_departments_rows = (
("Computer Science", "DS", "School of Science"),
("Biology", "life", "School of Science"),
("Materials Engineering", "also pretty cool", "School of Engineering"),
("Environmental Engineering", "water", "School of Engineering"),
("Math", "quick maths", "School of Science"),
("Computer Science", "DS is rough", "School of Science", "CSCI"),
("Biology", "life science", "School of Science", "BIOL"),
(
"Aerospace Engineering",
"space, the final frontier",
"Materials Engineering",
"also pretty cool",
"School of Engineering",
"MTLE",
),
(
"Aeronautical Engineering",
"flying, need for speed",
"Environmental Engineering",
"water stuff",
"School of Engineering",
"ENVE",
),
("Math", "quick maths", "School of Science", "MATH"),
(
"Material Science",
"Creating the best materials",
"Mechanical, Aerospace, and Nuclear Engineering",
"space, the final frontier",
"School of Engineering",
"MANE",
),
)

for row_tuple in rpi_departments_rows:
row = RPIDepartments(
name=row_tuple[0], description=row_tuple[1], school_id=row_tuple[2]
name=row_tuple[0],
description=row_tuple[1],
school_id=row_tuple[2],
id=row_tuple[3],
image="https://cdn-icons-png.flaticon.com/512/5310/5310672.png",
website="https://www.rpi.edu",
)
db.session.add(row)
db.session.commit()
Expand All @@ -91,24 +97,24 @@
db.session.commit()

lab_manager_rows = (
("led", "Duy", "Le", "Computer Science", "database database database"),
("led", "Duy", "Le", "CSCI", "database database database"),
(
"turner",
"Wes",
"Turner",
"Computer Science",
"CSCI",
"open source stuff is cool",
),
(
"kuzmin",
"Konstantine",
"Kuzmin",
"Computer Science",
"CSCI",
"java, psoft, etc.",
),
("goldd", "David", "Goldschmidt", "Computer Science", "VIM master"),
("rami", "Rami", "Rami", "Material Science", "cubes are cool"),
("holm", "Mark", "Holmes", "Math", "all about that math"),
("goldd", "David", "Goldschmidt", "CSCI", "VIM master"),
("rami", "Rami", "Rami", "MTLE", "cubes are cool"),
("holm", "Mark", "Holmes", "MATH", "all about that math"),
)

raf_test_user = (
Expand All @@ -117,7 +123,7 @@
"Cenzano",
"Raf",
2025,
"Computer Science",
"CSCI",
"labconnect is the best RCOS project",
"https://rafael.sirv.com/Images/rafael.jpeg?thumbnail=350&format=webp&q=90",
"https://rafaelcenzano.com",
Expand Down Expand Up @@ -358,19 +364,10 @@
)

for r in user_majors:
row = UserMajors(user_id=r[0], major_code=r[1])
db.session.add(row)
db.session.commit()

user_departments = (
("cenzar", "Computer Science"),
("cenzar", "Math"),
("test", "Computer Science"),
)

for r in user_departments:
row = UserDepartments(user_id=r[0], department_id=r[1])
db.session.add(row)
major = UserMajors(user_id=r[0], major_code=r[1])
department = UserDepartments(user_id=r[0], department_id=r[1])
db.session.add(major)
db.session.add(department)
db.session.commit()

user_courses = (
Expand Down
Binary file modified docs/database_docs/Labconnect_DB.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 42 additions & 28 deletions labconnect/main/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ def index():
@main_blueprint.get("/departments")
def departmentCards():
data = db.session.execute(
db.select(RPIDepartments.name, RPIDepartments.school_id)
db.select(RPIDepartments.name, RPIDepartments.school_id, RPIDepartments.id)
).all()
results = [
{
"title": department.name,
"department_id": department.id,
"school": department.school_id,
"image": "https://cdn-icons-png.flaticon.com/512/5310/5310672.png",
}
Expand All @@ -54,35 +55,49 @@ def departmentCards():
@main_blueprint.get("/departments/<string:department>")
def departmentDetails(department: str):

if not department:
abort(400)

department_data = db.first_or_404(
db.select(RPIDepartments).where(RPIDepartments.name == department)
)

result = department_data.to_dict()
department_data = db.session.execute(
db.select(
RPIDepartments.id,
RPIDepartments.name,
RPIDepartments.description,
RPIDepartments.image,
RPIDepartments.website,
).where(RPIDepartments.id == department)
).first()

prof_data = department_data.lab_managers
if department_data is None:
abort(404)

professors = []
where_conditions = []
staff_data = db.session.execute(
db.select(
User.id,
User.first_name,
User.preferred_name,
User.last_name,
User.profile_picture,
)
.join(LabManager, User.lab_manager_id == LabManager.id)
.join(RPIDepartments, LabManager.department_id == RPIDepartments.id)
.where(RPIDepartments.id == department)
).all()

for prof in prof_data:
professors.append(
result = {
"id": department_data[0],
"name": department_data[1],
"description": department_data[2],
"image": department_data[3],
"website": department_data[4],
"staff": [
{
"name": prof.getName(),
"rcs_id": prof.getEmail(),
"image": "https://www.svgrepo.com/show/206842/professor.svg",
"name": (
staff[2] + " " + staff[3] if staff[2] else staff[1] + " " + staff[3]
),
"id": staff[0],
"image": staff[4],
}
)
where_conditions.append(LabManager.id == prof.id)

result["professors"] = professors

result["image"] = (
"https://t4.ftcdn.net/jpg/02/77/10/87/360_F_277108701_1JAbS8jg7Gw42dU6nz7sF72bWiCm3VMv.jpg"
)
for staff in staff_data
],
}

return result

Expand Down Expand Up @@ -178,20 +193,19 @@ def profile():
@main_blueprint.get("/staff/<string:id>")
def getProfessorProfile(id: str):

# TODO: add ways to share phone number and email
# TODO: ensure this fails for non labmanager users
data = db.session.execute(
db.select(
User.preferred_name,
User.first_name,
User.last_name,
User.profile_picture,
LabManager.department_id,
RPIDepartments.name,
User.description,
User.website,
)
.where(User.id == id)
.join(LabManager, User.lab_manager_id == LabManager.id)
.join(RPIDepartments, LabManager.department_id == RPIDepartments.id)
).first()

if not data:
Expand Down
9 changes: 6 additions & 3 deletions labconnect/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class LabManager(db.Model, CustomSerializerMixin):
serialize_rules = ()

id = db.Column(db.Integer, primary_key=True, autoincrement=True)
department_id = db.Column(db.String(64), db.ForeignKey("rpi_departments.name"))
department_id = db.Column(db.String(4), db.ForeignKey("rpi_departments.id"))

user = db.relationship("User", back_populates="lab_manager")
department = db.relationship("RPIDepartments", back_populates="lab_managers")
Expand Down Expand Up @@ -113,8 +113,11 @@ class RPIDepartments(db.Model, CustomSerializerMixin):
serialize_only = ("name", "description", "school_id")
serialize_rules = ()

name = db.Column(db.String(64), primary_key=True)
id = db.Column(db.String(4), primary_key=True)
name = db.Column(db.String(64), nullable=False, unique=False)
image = db.Column(db.String(512), nullable=True, unique=False)
description = db.Column(db.String(2000), nullable=True, unique=False)
website = db.Column(db.String(512), nullable=True, unique=False)
school_id = db.Column(db.String(64), db.ForeignKey("rpi_schools.name"))

school = db.relationship("RPISchools", back_populates="departments")
Expand Down Expand Up @@ -254,7 +257,7 @@ class UserDepartments(db.Model, CustomSerializerMixin):

user_id = db.Column(db.String(9), db.ForeignKey("user.id"), primary_key=True)
department_id = db.Column(
db.String(64), db.ForeignKey("rpi_departments.name"), primary_key=True
db.String(4), db.ForeignKey("rpi_departments.id"), primary_key=True
)

user = db.relationship("User", back_populates="departments")
Expand Down
39 changes: 39 additions & 0 deletions migrations/versions/c72f39e93d72_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""empty message
Revision ID: c72f39e93d72
Revises: 4dd3611b273e
Create Date: 2024-10-15 17:29:11.568986
"""

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "c72f39e93d72"
down_revision = "4dd3611b273e"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("rpi_departments", schema=None) as batch_op:
batch_op.add_column(sa.Column("website", sa.String(length=2000), nullable=True))

with op.batch_alter_table("user", schema=None) as batch_op:
batch_op.create_unique_constraint(None, ["id"])

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("user", schema=None) as batch_op:
batch_op.drop_constraint(None, type_="unique")

with op.batch_alter_table("rpi_departments", schema=None) as batch_op:
batch_op.drop_column("website")

# ### end Alembic commands ###
33 changes: 33 additions & 0 deletions migrations/versions/f7518ca21f44_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""empty message
Revision ID: f7518ca21f44
Revises: c72f39e93d72
Create Date: 2024-10-16 01:35:20.577378
"""

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "f7518ca21f44"
down_revision = "c72f39e93d72"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("user", schema=None) as batch_op:
batch_op.create_unique_constraint(None, ["id"])

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("user", schema=None) as batch_op:
batch_op.drop_constraint(None, type_="unique")

# ### end Alembic commands ###

0 comments on commit 5055ce5

Please sign in to comment.