From 64ab0f2bf489ded8ece6bcf7baff35116faae2d8 Mon Sep 17 00:00:00 2001 From: vmagueta Date: Mon, 15 Jul 2024 20:03:02 -0300 Subject: [PATCH] Create class models --- assets/database.json | 18 ++++++++++- assets/people.csv | 1 + dundie/database.py | 1 + dundie/models.py | 75 ++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 5 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 dundie/models.py diff --git a/assets/database.json b/assets/database.json index 990694e..97ce6da 100644 --- a/assets/database.json +++ b/assets/database.json @@ -14,12 +14,18 @@ "name": "Gabe Lewis", "dept": "C-Level", "role": "CEO" + }, + "pam@dundermifflin.com": { + "name": "Pam Beasly", + "dept": "General", + "role": "Recepcionist" } }, "balance": { "jim@dundermifflin.com": 653, "schrute@dundermifflin.com": 253, - "glewis@dundermifflin.com": 200 + "glewis@dundermifflin.com": 200, + "pam@dundermifflin.com": 500 }, "movement": { "jim@dundermifflin.com": [ @@ -137,6 +143,13 @@ "actor": "solermvictor", "value": 100 } + ], + "pam@dundermifflin.com": [ + { + "date": "2024-07-15T18:20:06.454769", + "actor": "system", + "value": 500 + } ] }, "users": { @@ -148,6 +161,9 @@ }, "glewis@dundermifflin.com": { "password": "SR2jr3iG" + }, + "pam@dundermifflin.com": { + "password": "BdO3tMuX" } } } \ No newline at end of file diff --git a/assets/people.csv b/assets/people.csv index 1efc0f2..abf2d0b 100644 --- a/assets/people.csv +++ b/assets/people.csv @@ -1,3 +1,4 @@ Jim Halpert, Sales, Salesman, jim@dundermifflin.com Dwight Schrute, Sales, Manager, schrute@dundermifflin.com Gabe Lewis, C-Level, CEO, glewis@dundermifflin.com +Pam Beasly, General, Recepcionist, pam@dundermifflin.com diff --git a/dundie/database.py b/dundie/database.py index dda2a7b..690133f 100644 --- a/dundie/database.py +++ b/dundie/database.py @@ -28,6 +28,7 @@ def commit(db): database_file.write(json.dumps(db, indent=4)) +# SOLID - Single Responsibility def add_person(db, pk, data): """Save person data to database. diff --git a/dundie/models.py b/dundie/models.py new file mode 100644 index 0000000..26c3455 --- /dev/null +++ b/dundie/models.py @@ -0,0 +1,75 @@ +"""Model of database.""" + +from datetime import datetime +from decimal import Decimal + +from pydantic import BaseModel, field_validator +from dundie.database import connect + +from dundie.utils.email import check_valid_email + + +class InvalidEmailError(Exception): + """Class that represents an Error for invalid e-mails.""" + + ... + + +class Person(BaseModel): + """Class that represents a person.""" + + pk: str + name: str + dept: str + role: str + + @field_validator("pk") + def validate_email(cls, v): + """Validate e-mail received as primary key.""" + if not check_valid_email(v): + raise InvalidEmailError(f"Invalid e-mail for {v!r}") + return v + + def __str__(self): + """Change the visualizing in output for name - role.""" + return f"{self.name} - {self.role}" + + +class Balance(BaseModel): + """Class that represents the balance of a person.""" + + person: Person + value: Decimal + + @field_validator("value", mode="before") + def value_logic(cls, v): + """Value Logic.""" + return Decimal(v) * 2 + + class Config: + """Configuration for json encoder.""" + + json_encoders = {Person: lambda p: p.name} + + +class Movement(BaseModel): + """Class that represents a the movements of a person.""" + + person: Person + date: datetime + actor: str + value: Decimal + + +db = connect() + +for pk, data in db["people"].items(): + p = Person(pk=pk, **data) + +# print(p) +# print(p.json()) + + +balance = Balance(person=p, value=500) +print(balance) +print(balance.json()) diff --git a/requirements.txt b/requirements.txt index 45857f0..2e9dd91 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ click rich rich-click +pydantic