-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
490 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]": [ | ||
|
@@ -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]": [ | ||
|
@@ -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]": [ | ||
|
@@ -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 | ||
} | ||
] | ||
}, | ||
|
@@ -148,6 +201,9 @@ | |
}, | ||
"[email protected]": { | ||
"password": "SR2jr3iG" | ||
}, | ||
"[email protected]": { | ||
"password": "BdO3tMuX" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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')) | ||
|
@@ -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) |
Oops, something went wrong.