mock script, og mock data.

This commit is contained in:
2025-03-29 22:48:30 +01:00
parent b2a6384f31
commit e69d04dcd0
3 changed files with 121 additions and 0 deletions

View File

@@ -20,6 +20,7 @@ subparsers = parser.add_subparsers(
subparsers.add_parser("loop", help="Run the dibbler loop")
subparsers.add_parser("create-db", help="Create the database")
subparsers.add_parser("slabbedasker", help="Find out who is slabbedasker")
subparsers.add_parser("seed-data",help="Fill with mock data")
def main():
@@ -40,6 +41,12 @@ def main():
import dibbler.subcommands.slabbedasker as slabbedasker
slabbedasker.main()
elif args.subcommand == "seed-data":
import dibbler.subcommands.seed_test_data as seed_test_data
seed_test_data.main()
if __name__ == "__main__":

View File

@@ -0,0 +1,38 @@
import json
from dibbler.db import Session
from pathlib import Path
from dibbler.models.Product import Product
from dibbler.models.User import User
JSON_FILE = Path(__file__).parent.parent.parent/"mock_data.json"
def clear_db(session):
session.query(Product).delete()
session.query(User).delete()
session.commit()
def main() :
session = Session()
clear_db(session)
product_items = []
user_items = []
with open(JSON_FILE) as f:
json_obj = json.load(f)
for product in json_obj["products"]:
product_item = Product(bar_code=product["bar_code"], name=product["name"], price=product["price"], stock=product["stock"])
product_items.append(product_item)
for user in json_obj["users"]:
user_item = User(name=user ["name"], card=user [ "card" ], rfid=user [ "rfid" ], credit=user ["credit"])
user_items.append(user_item)
session.add_all(product_items)
session.add_all(user_items)
session.commit()