From 45e6a887e14e17c9a79052c66bb743d11200fb19 Mon Sep 17 00:00:00 2001 From: lbarraga Date: Fri, 1 Mar 2024 23:42:13 +0100 Subject: [PATCH 1/3] Domein modellen omzetten naar pydantic modellen #27 --- backend/db/implementation/SqlAbstractDAO.py | 6 ++++-- backend/db/models/models.py | 4 ++-- backend/domain/models/GroupDataclass.py | 4 ++-- backend/domain/models/ProjectDataclass.py | 6 +++--- backend/domain/models/SubjectDataclass.py | 4 ++-- backend/domain/models/SubmissionDataclass.py | 4 ++-- backend/domain/models/UserDataclass.py | 6 +++--- backend/domain/models/base_model.py | 8 -------- 8 files changed, 18 insertions(+), 24 deletions(-) delete mode 100644 backend/domain/models/base_model.py diff --git a/backend/db/implementation/SqlAbstractDAO.py b/backend/db/implementation/SqlAbstractDAO.py index cc7f2c2a..8a413699 100644 --- a/backend/db/implementation/SqlAbstractDAO.py +++ b/backend/db/implementation/SqlAbstractDAO.py @@ -3,13 +3,15 @@ from sqlalchemy import select from sqlalchemy.orm import Session +from pydantic import BaseModel + from db.errors.database_errors import ItemNotFoundError from db.extensions import engine from db.models.models import AbstractModel -from domain.models.base_model import JsonRepresentable + T = TypeVar("T", bound=AbstractModel) -D = TypeVar("D", bound=JsonRepresentable) +D = TypeVar("D", bound=BaseModel) class SqlAbstractDAO(Generic[T, D]): diff --git a/backend/db/models/models.py b/backend/db/models/models.py index b63d8261..8ce51e0d 100644 --- a/backend/db/models/models.py +++ b/backend/db/models/models.py @@ -3,12 +3,12 @@ from datetime import datetime from typing import Generic, TypeVar +from pydantic import BaseModel from sqlalchemy import Column, ForeignKey, Table from sqlalchemy.orm import Mapped, mapped_column, relationship from db.extensions import Base from domain.models.AdminDataclass import AdminDataclass -from domain.models.base_model import JsonRepresentable from domain.models.GroupDataclass import GroupDataclass from domain.models.ProjectDataclass import ProjectDataclass from domain.models.StudentDataclass import StudentDataclass @@ -17,7 +17,7 @@ from domain.models.TeacherDataclass import TeacherDataclass from domain.models.UserDataclass import UserDataclass -D = TypeVar("D", bound=JsonRepresentable) +D = TypeVar("D", bound=BaseModel) @dataclass() diff --git a/backend/domain/models/GroupDataclass.py b/backend/domain/models/GroupDataclass.py index a67a9ffc..671667e9 100644 --- a/backend/domain/models/GroupDataclass.py +++ b/backend/domain/models/GroupDataclass.py @@ -1,9 +1,9 @@ from dataclasses import dataclass -from domain.models.base_model import JsonRepresentable +from pydantic import BaseModel @dataclass() -class GroupDataclass(JsonRepresentable): +class GroupDataclass(BaseModel): id: int project_id: int diff --git a/backend/domain/models/ProjectDataclass.py b/backend/domain/models/ProjectDataclass.py index 2c45bf36..07826132 100644 --- a/backend/domain/models/ProjectDataclass.py +++ b/backend/domain/models/ProjectDataclass.py @@ -1,11 +1,11 @@ from dataclasses import dataclass from datetime import datetime -from domain.models.base_model import JsonRepresentable +from pydantic import BaseModel, PositiveInt @dataclass() -class ProjectDataclass(JsonRepresentable): +class ProjectDataclass(BaseModel): id: int name: str deadline: datetime @@ -13,5 +13,5 @@ class ProjectDataclass(JsonRepresentable): description: str requirements: str visible: bool - max_students: int + max_students: PositiveInt subject_id: int diff --git a/backend/domain/models/SubjectDataclass.py b/backend/domain/models/SubjectDataclass.py index 92f0a92b..9be302fd 100644 --- a/backend/domain/models/SubjectDataclass.py +++ b/backend/domain/models/SubjectDataclass.py @@ -1,9 +1,9 @@ from dataclasses import dataclass -from domain.models.base_model import JsonRepresentable +from pydantic import BaseModel @dataclass() -class SubjectDataclass(JsonRepresentable): +class SubjectDataclass(BaseModel): id: int name: str diff --git a/backend/domain/models/SubmissionDataclass.py b/backend/domain/models/SubmissionDataclass.py index 5d2641ea..79a85961 100644 --- a/backend/domain/models/SubmissionDataclass.py +++ b/backend/domain/models/SubmissionDataclass.py @@ -2,7 +2,7 @@ from dataclasses import dataclass from datetime import datetime -from domain.models.base_model import JsonRepresentable +from pydantic import BaseModel class SubmissionState(enum.Enum): @@ -12,7 +12,7 @@ class SubmissionState(enum.Enum): @dataclass() -class SubmissionDataclass(JsonRepresentable): +class SubmissionDataclass(BaseModel): id: int date_time: datetime group_id: int diff --git a/backend/domain/models/UserDataclass.py b/backend/domain/models/UserDataclass.py index 1a0696a7..791a864d 100644 --- a/backend/domain/models/UserDataclass.py +++ b/backend/domain/models/UserDataclass.py @@ -1,10 +1,10 @@ from dataclasses import dataclass -from domain.models.base_model import JsonRepresentable +from pydantic import BaseModel, EmailStr @dataclass() -class UserDataclass(JsonRepresentable): +class UserDataclass(BaseModel): id: int name: str - email: str + email: EmailStr diff --git a/backend/domain/models/base_model.py b/backend/domain/models/base_model.py deleted file mode 100644 index 9ab89131..00000000 --- a/backend/domain/models/base_model.py +++ /dev/null @@ -1,8 +0,0 @@ -import dataclasses -from dataclasses import dataclass - - -@dataclass() -class JsonRepresentable: - def to_dict(self) -> dict: - return dataclasses.asdict(self) From 3050db805035c848eba9b87df5f9f32d6358af5e Mon Sep 17 00:00:00 2001 From: lbarraga Date: Fri, 1 Mar 2024 23:42:34 +0100 Subject: [PATCH 2/3] Verwijderen JsonRepresentable #27 --- backend/db/implementation/SqlAbstractDAO.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/backend/db/implementation/SqlAbstractDAO.py b/backend/db/implementation/SqlAbstractDAO.py index 8a413699..540fbc2b 100644 --- a/backend/db/implementation/SqlAbstractDAO.py +++ b/backend/db/implementation/SqlAbstractDAO.py @@ -1,15 +1,13 @@ from typing import Generic, TypeVar +from pydantic import BaseModel from sqlalchemy import select from sqlalchemy.orm import Session -from pydantic import BaseModel - from db.errors.database_errors import ItemNotFoundError from db.extensions import engine from db.models.models import AbstractModel - T = TypeVar("T", bound=AbstractModel) D = TypeVar("D", bound=BaseModel) From f8240372019b24fd58c326997c314ee8224457b0 Mon Sep 17 00:00:00 2001 From: Mathieu Strypsteen Date: Sat, 2 Mar 2024 09:45:15 +0100 Subject: [PATCH 3/3] Remove validators --- backend/domain/validation/SubjectValidator.py | 21 ---------------- backend/domain/validation/TeacherValidator.py | 18 -------------- backend/domain/validation/ValidationResult.py | 24 ------------------- backend/requirements.txt | 2 ++ 4 files changed, 2 insertions(+), 63 deletions(-) delete mode 100644 backend/domain/validation/SubjectValidator.py delete mode 100644 backend/domain/validation/TeacherValidator.py delete mode 100644 backend/domain/validation/ValidationResult.py diff --git a/backend/domain/validation/SubjectValidator.py b/backend/domain/validation/SubjectValidator.py deleted file mode 100644 index 88b2a892..00000000 --- a/backend/domain/validation/SubjectValidator.py +++ /dev/null @@ -1,21 +0,0 @@ -from domain.validation.ValidationResult import ValidationError, ValidationResult, ValidationSuccess - - -class SubjectValidator: - @staticmethod - def validate(json_data: dict) -> ValidationResult: - - name = json_data.get("name") - teacher_id = json_data.get("teacher_id") - - errors: list[str] = [] - if not name: - errors.append("Veld 'name' ontbreekt.") - - if not teacher_id: - errors.append("Veld 'teacher_id' ontbreekt.") - - if len(errors) > 0: - return ValidationError(errors) - - return ValidationSuccess() diff --git a/backend/domain/validation/TeacherValidator.py b/backend/domain/validation/TeacherValidator.py deleted file mode 100644 index 0f57aa15..00000000 --- a/backend/domain/validation/TeacherValidator.py +++ /dev/null @@ -1,18 +0,0 @@ -from domain.validation.ValidationResult import ValidationError, ValidationResult, ValidationSuccess - - -# deze klassen zijn niet meer nodig omdat we gebruik maken van pydantic en FastAPI -class TeacherValidator: - @staticmethod - def validate(json_data: dict) -> ValidationResult: - - name = json_data.get("name") - - errors: list[str] = [] - if not name: - errors.append("Veld 'name' ontbreekt.") - - if len(errors) > 0: - return ValidationError(errors) - - return ValidationSuccess() diff --git a/backend/domain/validation/ValidationResult.py b/backend/domain/validation/ValidationResult.py deleted file mode 100644 index 3ffe7142..00000000 --- a/backend/domain/validation/ValidationResult.py +++ /dev/null @@ -1,24 +0,0 @@ -from abc import ABC, abstractmethod - - -class ValidationResult(ABC): - - errors: list[str] - - @abstractmethod - def __bool__(self) -> bool: - raise NotImplementedError - - -class ValidationSuccess(ValidationResult): - - def __bool__(self) -> bool: - return True - - -class ValidationError(ValidationResult): - def __init__(self, errors: list[str]) -> None: - self.errors = errors - - def __bool__(self) -> bool: - return False diff --git a/backend/requirements.txt b/backend/requirements.txt index 679534f5..dbd63a55 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -1,6 +1,8 @@ annotated-types==0.6.0 anyio==4.3.0 click==8.1.7 +dnspython==2.6.1 +email_validator==2.1.1 fastapi==0.110.0 greenlet==3.0.3 h11==0.14.0