Automatic user adding when importing transactions
This commit is contained in:
@@ -81,6 +81,15 @@ def import_transactions(filepath: str):
|
|||||||
lambda: defaultdict(lambda: Order(user_id=1))
|
lambda: defaultdict(lambda: Order(user_id=1))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# find users associated with the account
|
||||||
|
|
||||||
|
all_users = db.execute(select(User)).scalars().all()
|
||||||
|
|
||||||
|
users_by_firstname = defaultdict(list)
|
||||||
|
for user in all_users:
|
||||||
|
first_name = user.display_name.split()[0]
|
||||||
|
users_by_firstname[first_name].append(user)
|
||||||
|
|
||||||
for i, row in enumerate(rows, start=2):
|
for i, row in enumerate(rows, start=2):
|
||||||
transaction_type = row["typ"].lower()
|
transaction_type = row["typ"].lower()
|
||||||
if transaction_type == "einkauf foodcoop":
|
if transaction_type == "einkauf foodcoop":
|
||||||
@@ -110,6 +119,23 @@ def import_transactions(filepath: str):
|
|||||||
db.add(account)
|
db.add(account)
|
||||||
db.flush()
|
db.flush()
|
||||||
|
|
||||||
|
for person_name in account_name.split(","):
|
||||||
|
person_name = person_name.strip()
|
||||||
|
|
||||||
|
if person_name in users_by_firstname:
|
||||||
|
users = users_by_firstname[person_name]
|
||||||
|
if len(users) > 1:
|
||||||
|
print(
|
||||||
|
f"Warning: Multiple users found for name '{person_name}' in row {i}. Not adding any user."
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
user = users[0]
|
||||||
|
|
||||||
|
if account not in user.accounts:
|
||||||
|
print(f"Adding user {user.username} to account {account.name}")
|
||||||
|
user.accounts.append(account)
|
||||||
|
db.flush()
|
||||||
|
|
||||||
if transaction_type == "essen" or transaction_type == "essen kind":
|
if transaction_type == "essen" or transaction_type == "essen kind":
|
||||||
if row["stueck"] == 0:
|
if row["stueck"] == 0:
|
||||||
# if no quantity is given, assume it's 1 for non-zero amounts
|
# if no quantity is given, assume it's 1 for non-zero amounts
|
||||||
|
|||||||
Reference in New Issue
Block a user