2023-05-12 13:11:32 +01:00
|
|
|
"""This module defines the SQL user model for users.
|
|
|
|
|
|
|
|
All users are either Patients or Administrators.
|
|
|
|
"""
|
|
|
|
|
2023-05-12 03:59:05 +01:00
|
|
|
import enum
|
|
|
|
|
|
|
|
from sqlalchemy import Column, ForeignKey, Integer, String, DateTime, Date, Enum, CheckConstraint
|
|
|
|
from sqlalchemy.sql.functions import now
|
|
|
|
from sqlalchemy.orm import relationship
|
|
|
|
|
2023-05-12 13:11:32 +01:00
|
|
|
from backend.database.engine import Base
|
2023-05-12 03:59:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
class User(Base):
|
2023-05-12 13:11:32 +01:00
|
|
|
"""Model for the users table. Contains user info common to all user classes."""
|
|
|
|
|
2023-05-12 03:59:05 +01:00
|
|
|
__tablename__ = "users"
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True, index=True)
|
|
|
|
email = Column(String, unique=True, nullable=False)
|
|
|
|
password = Column(String, nullable=False)
|
|
|
|
created = Column(DateTime(timezone=True), nullable=False, server_default=now())
|
|
|
|
updated = Column(DateTime(timezone=True), nullable=False, server_default=now(), onupdate=now())
|
|
|
|
first_name = Column(String, nullable=False)
|
|
|
|
last_name = Column(String, nullable=False)
|
|
|
|
|
|
|
|
administrator = relationship("Administrator", back_populates="user", uselist=False, cascade="all, delete")
|
|
|
|
patient = relationship("Patient", back_populates="user", uselist=False, cascade="all, delete")
|
|
|
|
|
|
|
|
|
|
|
|
class Administrator(Base):
|
2023-05-12 13:11:32 +01:00
|
|
|
"""Model for the administrators table. Contains user info specific to administrators."""
|
|
|
|
|
2023-05-12 03:59:05 +01:00
|
|
|
__tablename__ = "administrators"
|
|
|
|
|
|
|
|
user_id = Column(Integer, ForeignKey('users.id', ondelete="CASCADE"), primary_key=True,)
|
|
|
|
user = relationship("User", back_populates="administrator", uselist=False, cascade="all, delete")
|
|
|
|
|
|
|
|
|
|
|
|
class Gender(enum.Enum):
|
2023-05-12 13:11:32 +01:00
|
|
|
"""Gender (as assigned at birth) of a patient."""
|
|
|
|
|
2023-05-12 03:59:05 +01:00
|
|
|
male = 'm'
|
|
|
|
female = 'f'
|
|
|
|
|
|
|
|
|
|
|
|
class Patient(Base):
|
2023-05-12 13:11:32 +01:00
|
|
|
"""Model for the patients table. Contains user info specific to patients."""
|
|
|
|
|
2023-05-12 03:59:05 +01:00
|
|
|
__tablename__ = "patients"
|
|
|
|
|
|
|
|
user_id = Column(Integer, ForeignKey('users.id', ondelete="CASCADE"), primary_key=True)
|
|
|
|
user = relationship("User", back_populates="patient", uselist=False, cascade="all, delete")
|
|
|
|
date_of_birth = Column(Date, nullable=False)
|
|
|
|
gender = Column(Enum(Gender), nullable=False)
|