This commit is contained in:
0
backend/core/users/management/__init__.py
Normal file
0
backend/core/users/management/__init__.py
Normal file
0
backend/core/users/management/commands/__init__.py
Normal file
0
backend/core/users/management/commands/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from core.management.commands.seed_all_models import Command # noqa: F401
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
from core.management.commands.seed_fleet_assets import Command # noqa: F401
|
||||
|
||||
107
backend/core/users/management/commands/seed_knezljubo.py
Normal file
107
backend/core/users/management/commands/seed_knezljubo.py
Normal file
@@ -0,0 +1,107 @@
|
||||
"""
|
||||
seed_knezljubo.py
|
||||
|
||||
Kreira demo klijenta "Knezljubo d.o.o." s jednim fleet vozilom (is_company_vehicle=True)
|
||||
i jednim vozilom u vlasništvu klijenta (is_company_vehicle=False).
|
||||
|
||||
Idempotentna: ponavljanjem ne kreira duplikate (koristi get_or_create).
|
||||
|
||||
Uporaba:
|
||||
python manage.py seed_knezljubo
|
||||
docker compose exec -it backend python manage.py seed_knezljubo
|
||||
"""
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.db import transaction
|
||||
|
||||
from modules.crm.models import Client
|
||||
from modules.fleet.models import Vehicle
|
||||
|
||||
|
||||
KNEZLJUBO_TAX_ID = "12345678901"
|
||||
FLEET_VIN = "VIN_FLEET_KNEZLJUBO001"
|
||||
CLIENT_VIN = "VIN_CLIENT_KNEZLJUBO002"
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Kreira klijenta Knezljubo s fleet vozilom i vozilom u vlasništvu klijenta."
|
||||
|
||||
def handle(self, *args, **options):
|
||||
with transaction.atomic():
|
||||
client = self._seed_client()
|
||||
fleet_vehicle = self._seed_fleet_vehicle(client)
|
||||
client_vehicle = self._seed_client_vehicle(client)
|
||||
|
||||
self.stdout.write(self.style.SUCCESS(
|
||||
f"\nZavršeno!\n"
|
||||
f" Klijent : {client} (id={client.id})\n"
|
||||
f" Fleet vozilo : {fleet_vehicle} [is_company_vehicle=True]\n"
|
||||
f" Klijentovo : {client_vehicle} [is_company_vehicle=False]\n"
|
||||
))
|
||||
|
||||
# ── Klijent ───────────────────────────────────────────────────────────────
|
||||
|
||||
def _seed_client(self) -> Client:
|
||||
client, created = Client.objects.get_or_create(
|
||||
tax_id=KNEZLJUBO_TAX_ID,
|
||||
defaults={
|
||||
"name": "Knezljubo d.o.o.",
|
||||
"client_type": "legal",
|
||||
"email": "info@knezljubo.hr",
|
||||
"phone": "+385912345678",
|
||||
"address": "Ilica 100",
|
||||
"city": "Zagreb",
|
||||
"postal_code": "10000",
|
||||
},
|
||||
)
|
||||
action = "Kreiran" if created else "Već postoji"
|
||||
self.stdout.write(self.style.SUCCESS(f"Client: {action} — {client}"))
|
||||
return client
|
||||
|
||||
# ── Fleet vozilo (in vlasništvu naše tvrtke, ali vezano za klijenta) ──────
|
||||
|
||||
def _seed_fleet_vehicle(self, client: Client) -> Vehicle:
|
||||
"""
|
||||
Vozilo je dio naše fleet-a (is_company_vehicle=True) ALI je operativno
|
||||
vezano za klijenta Knezljubo — npr. vozilo koje servisiramo po ugovoru.
|
||||
"""
|
||||
vehicle, created = Vehicle.objects.get_or_create(
|
||||
vin=FLEET_VIN,
|
||||
defaults={
|
||||
"registration_number": "ZG-0001-KZ",
|
||||
"make": "Mercedes",
|
||||
"model": "Sprinter",
|
||||
"year": 2021,
|
||||
"current_mileage": 48500,
|
||||
"service_interval_km": 15000,
|
||||
"is_company_vehicle": True, # dio naše flote
|
||||
"client": client, # operativno vezan za klijenta
|
||||
},
|
||||
)
|
||||
action = "Kreiran" if created else "Već postoji"
|
||||
self.stdout.write(self.style.SUCCESS(f"Fleet vozilo: {action} — {vehicle}"))
|
||||
return vehicle
|
||||
|
||||
# ── Vozilo u vlasništvu klijenta ──────────────────────────────────────────
|
||||
|
||||
def _seed_client_vehicle(self, client: Client) -> Vehicle:
|
||||
"""
|
||||
Vozilo koje KLIJENT posjeduje — naša tvrtka ga servisira.
|
||||
is_company_vehicle=False → filter u admin i frontend pokazuje ovo kao klijentovo.
|
||||
"""
|
||||
vehicle, created = Vehicle.objects.get_or_create(
|
||||
vin=CLIENT_VIN,
|
||||
defaults={
|
||||
"registration_number": "ZG-0002-KZ",
|
||||
"make": "VW",
|
||||
"model": "Crafter",
|
||||
"year": 2019,
|
||||
"current_mileage": 112000,
|
||||
"service_interval_km": 10000,
|
||||
"is_company_vehicle": False, # klijentovo vozilo
|
||||
"client": client,
|
||||
},
|
||||
)
|
||||
action = "Kreiran" if created else "Već postoji"
|
||||
self.stdout.write(self.style.SUCCESS(f"Klijentovo vozilo: {action} — {vehicle}"))
|
||||
return vehicle
|
||||
52
backend/core/users/management/commands/seed_users.py
Normal file
52
backend/core/users/management/commands/seed_users.py
Normal file
@@ -0,0 +1,52 @@
|
||||
# backend/core/users/management/commands/seed_users.py
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.db import transaction
|
||||
from modules.crm.models import Client
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Kreira testne korisnike za razvojno okruženje'
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
users_to_create = [
|
||||
{
|
||||
"email": "serviser@primjer.hr",
|
||||
"first_name": "Ivan",
|
||||
"last_name": "Serviser",
|
||||
"is_serviser": True,
|
||||
"is_team_member": True,
|
||||
"password": "password123"
|
||||
},
|
||||
{
|
||||
"email": "kupac@primjer.hr",
|
||||
"first_name": "Marko",
|
||||
"last_name": "Kupac",
|
||||
"is_kupac": True,
|
||||
"password": "password123"
|
||||
}
|
||||
]
|
||||
|
||||
with transaction.atomic():
|
||||
for user_data in users_to_create:
|
||||
email = user_data.pop('email')
|
||||
password = user_data.pop('password')
|
||||
|
||||
if user_data.get('is_kupac'):
|
||||
client, _ = Client.objects.get_or_create(name="Testni Klijent DOO")
|
||||
user_data['client_profile'] = client
|
||||
|
||||
# Provjeri postoji li korisnik
|
||||
if not User.objects.filter(email=email).exists():
|
||||
# KORISTI MANAGER UMJESTO MODELA
|
||||
user = User.objects.create_user(
|
||||
email=email,
|
||||
password=password,
|
||||
**user_data
|
||||
)
|
||||
self.stdout.write(self.style.SUCCESS(f'Kreiran korisnik: {email}'))
|
||||
else:
|
||||
self.stdout.write(self.style.WARNING(f'Korisnik već postoji: {email}'))
|
||||
|
||||
self.stdout.write(self.style.SUCCESS('Seeding završen!'))
|
||||
Reference in New Issue
Block a user