Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
Use inheritance for users
Browse files Browse the repository at this point in the history
  • Loading branch information
msathieu committed Feb 25, 2024
1 parent f6dea80 commit a016bd4
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 26 deletions.
6 changes: 0 additions & 6 deletions backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,5 @@
app.config["SQLALCHEMY_DATABASE_URI"] = f"postgresql://{db_user}:{db_password}@{db_host}:{db_port}/{db_database}"
db.init_app(app)


@app.route("/")
def hello_world():
return "Hello, World!"


if __name__ == "__main__":
app.run(debug=True)
12 changes: 6 additions & 6 deletions backend/db/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ def to_domain_model(self) -> UserDataclass:
return UserDataclass(id=self.id, name=self.name, email=self.email)


class Admin(db.Model):
class Admin(User):
id: Mapped[int] = mapped_column(ForeignKey(User.id), primary_key=True)

def to_domain_model(self) -> AdminDataclass:
return AdminDataclass(id=self.id)
return AdminDataclass(id=self.id, name=self.name, email=self.email)


teachers_subjects = Table(
Expand All @@ -50,22 +50,22 @@ def to_domain_model(self) -> AdminDataclass:
)


class Teacher(db.Model):
class Teacher(User):
id: Mapped[int] = mapped_column(ForeignKey(User.id), primary_key=True)
subjects: Mapped[list["Subject"]] = relationship(secondary=teachers_subjects, back_populates="teachers")

def to_domain_model(self) -> TeacherDataclass:
return TeacherDataclass(id=self.id)
return TeacherDataclass(id=self.id, name=self.name, email=self.email)


class Student(db.Model):
class Student(User):
id: Mapped[int] = mapped_column(ForeignKey(User.id), primary_key=True)
subjects: Mapped[list["Subject"]] = relationship(secondary=students_subjects, back_populates="students")
groups: Mapped[list["Group"]] = relationship(secondary=students_groups, back_populates="students")
submissions: Mapped[list["Submission"]] = relationship(back_populates="student")

def to_domain_model(self) -> StudentDataclass:
return StudentDataclass(id=self.id)
return StudentDataclass(id=self.id, name=self.name, email=self.email)


class Subject(db.Model):
Expand Down
6 changes: 3 additions & 3 deletions backend/domain/models/AdminDataclass.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from dataclasses import dataclass

from domain.models.base_model import JsonRepresentable
from domain.models.UserDataclass import UserDataclass


@dataclass()
class AdminDataclass(JsonRepresentable):
id: int
class AdminDataclass(UserDataclass):
pass
6 changes: 3 additions & 3 deletions backend/domain/models/StudentDataclass.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from dataclasses import dataclass

from domain.models.base_model import JsonRepresentable
from domain.models.UserDataclass import UserDataclass


@dataclass()
class StudentDataclass(JsonRepresentable):
id: int
class StudentDataclass(UserDataclass):
pass
6 changes: 3 additions & 3 deletions backend/domain/models/TeacherDataclass.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from dataclasses import dataclass

from domain.models.base_model import JsonRepresentable
from domain.models.UserDataclass import UserDataclass


@dataclass()
class TeacherDataclass(JsonRepresentable):
id: int
class TeacherDataclass(UserDataclass):
pass
7 changes: 2 additions & 5 deletions backend/tests/teachers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
import unittest
from http import HTTPStatus

from routes.index import app
from app import app


class LesgeverTestCase(unittest.TestCase):

def setUp(self):
self.app = app.test_client()
self.app.testing = True
Expand All @@ -17,9 +16,7 @@ def test_create_teacher_bad_request(self):
self.assertIn("error", json.loads(response.data))

def test_create_teacher_success(self):
teacher_data = {
"name": "Bart De Bruyn"
}
teacher_data = {"name": "Bart De Bruyn"}
response = self.app.post("/teachers", data=json.dumps(teacher_data), content_type="application/json")
self.assertEqual(HTTPStatus.CREATED, response.status_code)

Expand Down

0 comments on commit a016bd4

Please sign in to comment.