timetracker/common/import_data.py

31 lines
748 B
Python
Raw Normal View History

2023-01-19 20:26:21 +00:00
import csv
from typing import TypeAlias
2023-01-19 20:50:11 +00:00
from games.models import Game
2023-01-19 20:26:21 +00:00
DataList: TypeAlias = list[dict[str, str]] | None
def read_csv(filename: str) -> DataList:
with open(filename, "r") as csvfile:
writer = csv.DictReader(csvfile)
return writer
def import_data(data: DataList):
matching_names = {}
for line in data:
name = line["name"]
if name not in matching_names:
# try exact match first
try:
game_id = Game.objects.get(name__iexact=name)
except:
pass
matching_names[name] = game_id
print(f"Exact matched {len(matching_names)} games.")
def import_from_file(filename: str):
import_data(read_csv(filename))