-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepartment.py
More file actions
55 lines (46 loc) · 2.82 KB
/
Department.py
File metadata and controls
55 lines (46 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from orm_base import Base
from sqlalchemy import Column, Integer, UniqueConstraint, Identity
from sqlalchemy import String
from sqlalchemy.orm import Mapped, mapped_column, relationship
from typing import List # Use this for the list of courses offered by the department
class Department(Base):
"""An organization within a particular college within a university. Each
department offers one or more major fields of study to its students, and
within each major, some number of courses. Each course is offered on
a regular basis as a scheduled section of a given course.
Note, this is just a shell of the Department class. There are additional
columns needed, but this is enough to demonstrate one-to-many relationships."""
__tablename__ = "departments" # Give SQLAlchemy th name of the table.
abbreviation: Mapped[str] = mapped_column('abbreviation', String,
nullable=False, primary_key=True)
courses: Mapped[List["Course"]] = relationship(back_populates="department")
majors: Mapped[List["Major"]] = relationship(back_populates="department")
name: Mapped[str] = mapped_column('name', String(50), nullable=False)
chairName: Mapped[str] = mapped_column('chair_name', String(80), nullable=False)
building: Mapped[str] = mapped_column('building', String(10), nullable=False)
office: Mapped[int] = mapped_column('office', Integer, nullable=False)
description: Mapped[str] = mapped_column('description', String(80), nullable=False)
# __table_args__ can best be viewed as directives that we ask SQLAlchemy to
# send to the database. In this case, that we want two separate uniqueness
# constraints (candidate keys).
__table_args__ = (
UniqueConstraint("name", name="departments_uk_01"), UniqueConstraint("chair_name", name="departments_uk_02"),
UniqueConstraint("building", "office", name="departments_uk_03"),
UniqueConstraint("description", name="departments_uk_04"))
def __init__(self, abbreviation: str, name: str, chair_name: str, building: str, office: int, description: str):
self.abbreviation = abbreviation
self.name = name
self.chairName = chair_name
self.building = building
self.office = office
self.description = description
def add_course(self, course):
if course not in self.courses:
self.courses.add(course) # I believe this will update the course as well.
def remove_course(self, course):
if course in self.courses:
self.courses.remove(course)
def get_courses(self):
return self.courses
def __str__(self):
return f"Department abbreviation: {self.abbreviation} name: {self.name} number course offered: {len(self.courses)}"