UPDATE
This commit is contained in:
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
|
||||
from farm_hub.seeds import seed_admin_farm
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Create or update the default farm hub for the admin user."
|
||||
|
||||
def handle(self, *args, **options):
|
||||
try:
|
||||
farm, created = seed_admin_farm()
|
||||
except ValueError as exc:
|
||||
raise CommandError(str(exc)) from exc
|
||||
|
||||
action = "created" if created else "updated"
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(
|
||||
f"Admin farm {action}: farm_uuid={farm.farm_uuid}, name={farm.name}, owner={farm.owner.username}"
|
||||
)
|
||||
)
|
||||
@@ -0,0 +1,29 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from farm_hub.catalog import CATALOG_SEED_DATA
|
||||
from farm_hub.models import FarmType, Product
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Seed farm types and products catalog data."
|
||||
|
||||
def handle(self, *args, **options):
|
||||
farm_type_count = 0
|
||||
product_count = 0
|
||||
|
||||
for farm_type_name, products in CATALOG_SEED_DATA.items():
|
||||
farm_type, created = FarmType.objects.get_or_create(name=farm_type_name)
|
||||
farm_type_count += int(created)
|
||||
for product_data in products:
|
||||
_, product_created = Product.objects.update_or_create(
|
||||
farm_type=farm_type,
|
||||
name=product_data["name"],
|
||||
defaults={key: value for key, value in product_data.items() if key != "name"},
|
||||
)
|
||||
product_count += int(product_created)
|
||||
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(
|
||||
f"Farm catalog seeded successfully. Created farm types: {farm_type_count}, products: {product_count}."
|
||||
)
|
||||
)
|
||||
Reference in New Issue
Block a user