Skip to content

Commit

Permalink
Create class models
Browse files Browse the repository at this point in the history
  • Loading branch information
vmagueta committed Jul 16, 2024
1 parent 6bf1ae6 commit 64ab0f2
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
18 changes: 17 additions & 1 deletion assets/database.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@
"name": "Gabe Lewis",
"dept": "C-Level",
"role": "CEO"
},
"[email protected]": {
"name": "Pam Beasly",
"dept": "General",
"role": "Recepcionist"
}
},
"balance": {
"[email protected]": 653,
"[email protected]": 253,
"[email protected]": 200
"[email protected]": 200,
"[email protected]": 500
},
"movement": {
"[email protected]": [
Expand Down Expand Up @@ -137,6 +143,13 @@
"actor": "solermvictor",
"value": 100
}
],
"[email protected]": [
{
"date": "2024-07-15T18:20:06.454769",
"actor": "system",
"value": 500
}
]
},
"users": {
Expand All @@ -148,6 +161,9 @@
},
"[email protected]": {
"password": "SR2jr3iG"
},
"[email protected]": {
"password": "BdO3tMuX"
}
}
}
1 change: 1 addition & 0 deletions assets/people.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Jim Halpert, Sales, Salesman, [email protected]
Dwight Schrute, Sales, Manager, [email protected]
Gabe Lewis, C-Level, CEO, [email protected]
Pam Beasly, General, Recepcionist, [email protected]
1 change: 1 addition & 0 deletions dundie/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
75 changes: 75 additions & 0 deletions dundie/models.py
Original file line number Diff line number Diff line change
@@ -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())
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
click
rich
rich-click
pydantic

0 comments on commit 64ab0f2

Please sign in to comment.