Add user import

This commit is contained in:
2026-06-28 10:11:38 +02:00
parent 3591e0f33a
commit 3c6857e615
3 changed files with 75 additions and 13 deletions

View File

@@ -193,7 +193,9 @@ async def get_products(
user: UserDep, user: UserDep,
): ):
products = session.scalars( products = session.scalars(
select(Product).order_by(Product.area_id, Product.name) select(Product)
.join(ProductDetails)
.order_by(Product.area_id, ProductDetails.name)
).all() ).all()
templates = get_jinja_renderer() templates = get_jinja_renderer()

View File

@@ -1,9 +1,9 @@
import argparse import argparse
from collections import defaultdict
import csv import csv
import sys
from collections import defaultdict
from datetime import datetime, time from datetime import datetime, time
from decimal import Decimal from decimal import Decimal
import sys
from sqlalchemy import select from sqlalchemy import select
@@ -12,10 +12,10 @@ from allmende_payment_system.models import (
Account, Account,
Order, Order,
OrderItem, OrderItem,
Product,
ProductDetails, ProductDetails,
Transaction, Transaction,
Order, User,
Product,
) )
@@ -26,7 +26,7 @@ def init_db():
print("Database initialized and tables created successfully.") print("Database initialized and tables created successfully.")
def import_csv(filepath: str): def import_transactions(filepath: str):
""" """
Import CSV data from Grist into the database. Import CSV data from Grist into the database.
@@ -146,7 +146,7 @@ def import_csv(filepath: str):
order.transaction._total_amount = Decimal(order.total_amount) order.transaction._total_amount = Decimal(order.total_amount)
elif transaction_type == "einzahlung" or transaction_type == "einkauf": elif transaction_type == "einzahlung" or transaction_type == "einkauf":
transaction = Transaction( transaction = Transaction(
type="deposit", type="deposit" if transaction_type == "einzahlung" else "expense",
timestamp=datetime.combine(row["datum"], time(12, 0, 0)), timestamp=datetime.combine(row["datum"], time(12, 0, 0)),
_total_amount=row["betrag"], _total_amount=row["betrag"],
account_id=account.id, account_id=account.id,
@@ -165,20 +165,78 @@ def import_csv(filepath: str):
db.commit() db.commit()
def import_users(filepath: str):
"""
Import user data from a CSV file into the database.
This can be deleted after the initial import, it's just a one-time helper function to get the existing data into the system.
"""
REQUIRED_COLUMNS = {"username", "firstname", "lastname"}
with open(filepath, newline="", encoding="utf-8") as f:
reader = csv.DictReader(f, delimiter=";")
if not REQUIRED_COLUMNS.issubset(reader.fieldnames):
missing = REQUIRED_COLUMNS - set(reader.fieldnames)
raise ValueError(f"CSV is missing required columns: {missing}")
user_data = []
for i, row in enumerate(reader, start=2): # start=2 to account for header row
try:
user_data.append(
{
"username": row["username"].strip(),
"display_name": row["firstname"].strip()
+ " "
+ row["lastname"].strip(),
}
)
except ValueError as e:
raise ValueError(f"Error in row {i}: {e}")
print(f"Parsed {len(user_data)} rows successfully.")
db = SessionLocal()
for entry in user_data:
user = db.execute(
select(User).where(User.username == entry["username"])
).scalar_one_or_none()
if not user:
user = User(
username=entry["username"],
display_name=entry["display_name"],
)
db.add(user)
db.flush()
db.commit()
def main(): def main():
parser = argparse.ArgumentParser(description="Allmende Payment System CLI") parser = argparse.ArgumentParser(description="Allmende Payment System CLI")
subparsers = parser.add_subparsers(dest="command", help="Available subcommands") subparsers = parser.add_subparsers(dest="command", help="Available subcommands")
subparsers.add_parser("init-db", help="Initialize the database and create tables") subparsers.add_parser("init-db", help="Initialize the database and create tables")
import_csv_parser = subparsers.add_parser( import_transactions_parser = subparsers.add_parser(
"import-csv", help="Import data from a CSV file" "import-transactions", help="Import transaction data from a CSV file"
) )
import_csv_parser.add_argument("filepath", help="Path to the CSV file to import") import_transactions_parser.add_argument(
"filepath", help="Path to the CSV file to import"
)
import_users_parser = subparsers.add_parser(
"import-users", help="Import user data from a CSV file"
)
import_users_parser.add_argument("filepath", help="Path to the CSV file to import")
args = parser.parse_args() args = parser.parse_args()
if args.command == "init-db": if args.command == "init-db":
init_db() init_db()
elif args.command == "import-csv": elif args.command == "import-transactions":
import_csv(args.filepath) import_transactions(args.filepath)
elif args.command == "import-users":
import_users(args.filepath)

View File

@@ -46,7 +46,9 @@ def timer_context(name: str):
return Timer() return Timer()
import cProfile, pstats, io import cProfile
import io
import pstats
from contextlib import contextmanager from contextlib import contextmanager