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 19, 2024
1 parent 6bf1ae6 commit c038f05
Show file tree
Hide file tree
Showing 14 changed files with 490 additions and 137 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ipython:


fmt:
@.venv/bin/isort dundie tests integration
@.venv/bin/isort --profile=black -m 3 dundie tests integration
@.venv/bin/black dundie tests integration


Expand Down
58 changes: 57 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 @@ -72,6 +78,16 @@
"date": "2024-07-01T10:29:00.532653",
"actor": "solermvictor",
"value": 100
},
{
"date": "2024-07-19T14:41:20.573189",
"actor": "solermvictor",
"value": 5
},
{
"date": "2024-07-19T14:42:12.498918",
"actor": "solermvictor",
"value": -5
}
],
"[email protected]": [
Expand Down Expand Up @@ -124,6 +140,16 @@
"date": "2024-07-01T10:29:00.532668",
"actor": "solermvictor",
"value": 100
},
{
"date": "2024-07-19T14:41:20.573205",
"actor": "solermvictor",
"value": 5
},
{
"date": "2024-07-19T14:42:12.498933",
"actor": "solermvictor",
"value": -5
}
],
"[email protected]": [
Expand All @@ -136,6 +162,33 @@
"date": "2024-07-01T10:28:22.353956",
"actor": "solermvictor",
"value": 100
},
{
"date": "2024-07-19T14:41:20.573209",
"actor": "solermvictor",
"value": 5
},
{
"date": "2024-07-19T14:42:12.498938",
"actor": "solermvictor",
"value": -5
}
],
"[email protected]": [
{
"date": "2024-07-15T18:20:06.454769",
"actor": "system",
"value": 500
},
{
"date": "2024-07-19T14:41:20.573212",
"actor": "solermvictor",
"value": 5
},
{
"date": "2024-07-19T14:42:12.498940",
"actor": "solermvictor",
"value": -5
}
]
},
Expand All @@ -148,6 +201,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]
18 changes: 9 additions & 9 deletions dundie/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Main commands for dundie in command line interface."""
"""Commandline Interface of Dundie."""

import json
from importlib import metadata
Expand All @@ -12,7 +12,7 @@
click.rich_click.USE_RICH_MARKUP = True
click.rich_click.USE_MARKDOWN = True
click.rich_click.SHOW_ARGUMENTS = True
click.rich_click.GRUOP_ARGUMENTS_OPTIONS = True
click.rich_click.GROUP_ARGUMENTS_OPTIONS = True
click.rich_click.SHOW_METAVARS_COLUMN = False
click.rich_click.APPEND_METAVARS_HELP = True

Expand All @@ -22,10 +22,10 @@
def main():
"""Dunder Mifflin Rewards System.
This CLI application controls Dunder Mifflin Rewards.
This cli application controls Dunder Mifflin rewards.
- admins can load information to the people database and assign points.
- users can view and transfer points.
- admins can load information tot he people database and assign points.
- users can view reports and transfer points.
"""

Expand All @@ -44,7 +44,7 @@ def load(filepath):
table = Table(title="Dunder Mifflin Associates")
headers = ["name", "dept", "role", "created", "e-mail"]
for header in headers:
table.add_column(header, style="italic cyan1")
table.add_column(header, style="magenta")

result = core.load(filepath)
for person in result:
Expand All @@ -59,7 +59,7 @@ def load(filepath):
@click.option("--email", required=False)
@click.option("--output", default=None)
def show(output, **query):
"""Show information about users od dept."""
"""Show information about user or dept."""
result = core.read(**query)
if output:
with open(output, "w") as output_file:
Expand All @@ -70,7 +70,7 @@ def show(output, **query):

table = Table(title="Dunder Mifflin Report")
for key in result[0]:
table.add_column(key.title(), style="italic cyan1")
table.add_column(key.title().replace("_", " "), style="magenta")

for person in result:
table.add_row(*[str(value) for value in person.values()])
Expand All @@ -96,6 +96,6 @@ def add(ctx, value, **query):
@click.option("--email", required=False)
@click.pass_context
def remove(ctx, value, **query):
"""Remove points to the user or dept."""
"""Remove points from the user or dept."""
core.add(-value, **query)
ctx.invoke(show, **query)
50 changes: 26 additions & 24 deletions dundie/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

import os
from csv import reader
from typing import Any, Dict, List

from dundie.database import add_movement, add_person, commit, connect
from dundie.models import Balance, Movement, Person
from dundie.utils.log import get_logger

log = get_logger()
Query = Dict[str, Any]
ResultDict = List[Dict[str, Any]]


def load(filepath):
def load(filepath: str) -> ResultDict:
"""Load data from filepath to the database.
>>> len(load('assets/people.csv'))
Expand All @@ -23,58 +27,56 @@ def load(filepath):

db = connect()
people = []
headers = ["name", "dept", "role", "e-mail"]
headers = ["name", "dept", "role", "email"]
for line in csv_data:
person_data = dict(zip(headers, [item.strip() for item in line]))
pk = person_data.pop("e-mail")
person, created = add_person(db, pk, person_data)

return_data = person.copy()
instance = Person(pk=person_data.pop("email"), **person_data)
person, created = add_person(db, instance)
return_data = person.dict(exclude={"pk"})
return_data["created"] = created
return_data["email"] = pk
return_data["email"] = person.pk
people.append(return_data)

commit(db)
return people


def read(**query):
def read(**query: Query) -> ResultDict:
"""Read data from db and filters using query.
read(email="[email protected]")
"""
query = {k: v for k, v in query.items() if v is not None}
db = connect()
return_data = []
for pk, data in db["people"].items():

dept = query.get("dept")
if dept and dept != data["dept"]:
continue

# WALRUS / Assignment Expression - a partir do python 3.8
if (email := query.get("email")) and email != pk:
continue
if "email" in query:
query["pk"] = query.pop("email")

for person in db[Person].filter(**query):
return_data.append(
{
"email": pk,
"balance": db["balance"][pk],
"last_movement": db["movement"][pk][-1]["date"],
**data,
"email": person.pk,
"balance": db[Balance].get_by_pk(person.pk).value,
"last_movement": db[Movement]
.filter(person__pk=person.pk)[-1]
.date,
**person.dict(exclude={"pk"}),
}
)

return return_data


def add(value, **query):
def add(value: int, **query: Query):
"""Add value to each record on query."""
query = {k: v for k, v in query.items() if v is not None}
people = read(**query)

if not people:
raise RuntimeError("Not Found")

db = connect()
user = os.getenv("USER")
for person in people:
add_movement(db, person["email"], value, user)
instance = db[Person].get_by_pk(person["email"])
add_movement(db, instance, value, user)
commit(db)
Loading

0 comments on commit c038f05

Please sign in to comment.