2017-02-08 02:22:31 +01:00
|
|
|
"""
|
|
|
|
Crée des utilisateurs, des articles et des opérations aléatoires
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import random
|
|
|
|
from datetime import timedelta
|
|
|
|
|
2020-08-03 20:35:56 +02:00
|
|
|
from django.contrib.auth.models import User
|
2017-03-19 20:03:39 +01:00
|
|
|
from django.core.management import call_command
|
2018-10-06 12:35:49 +02:00
|
|
|
from django.utils import timezone
|
2017-02-08 02:22:31 +01:00
|
|
|
|
|
|
|
from gestioncof.management.base import MyBaseCommand
|
|
|
|
from gestioncof.models import CofProfile
|
2020-09-07 14:49:41 +02:00
|
|
|
from kfet.auth.models import KFetGroup, KFetPermission
|
2018-10-06 12:35:49 +02:00
|
|
|
from kfet.models import (
|
|
|
|
Account,
|
|
|
|
Article,
|
|
|
|
Checkout,
|
|
|
|
CheckoutStatement,
|
|
|
|
Supplier,
|
|
|
|
SupplierArticle,
|
|
|
|
)
|
2017-02-08 02:22:31 +01:00
|
|
|
|
|
|
|
# Où sont stockés les fichiers json
|
2018-10-06 12:35:49 +02:00
|
|
|
DATA_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), "data")
|
2017-02-08 02:22:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Command(MyBaseCommand):
|
|
|
|
help = "Crée des utilisateurs, des articles et des opérations aléatoires"
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
# ---
|
|
|
|
# Groupes
|
|
|
|
# ---
|
|
|
|
|
2020-08-03 20:35:56 +02:00
|
|
|
group_chef, _ = KFetGroup.objects.get_or_create(name="K-Fêt César")
|
|
|
|
group_boy, _ = KFetGroup.objects.get_or_create(name="K-Fêt Légionnaire")
|
2017-02-08 02:22:31 +01:00
|
|
|
|
2020-09-07 14:49:41 +02:00
|
|
|
# Give relevant permissions to both groups
|
|
|
|
chef_perms = KFetPermission.objects.all()
|
|
|
|
group_chef.permissions.add(*chef_perms)
|
|
|
|
|
|
|
|
boy_perms = KFetPermission.objects.filter(
|
|
|
|
codename__in=["is_team", "perform_deposit", "add_account", "add_transfer"]
|
|
|
|
)
|
|
|
|
group_boy.permissions.add(*boy_perms)
|
2017-02-08 02:22:31 +01:00
|
|
|
|
|
|
|
# ---
|
|
|
|
# Comptes
|
|
|
|
# ---
|
|
|
|
|
2017-02-08 18:54:49 +01:00
|
|
|
self.stdout.write("Création des comptes K-Fêt")
|
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
gaulois = CofProfile.objects.filter(user__last_name="Gaulois")
|
|
|
|
gaulois_trigramme = map("{:03d}".format, range(50))
|
2017-02-08 02:22:31 +01:00
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
romains = CofProfile.objects.filter(user__last_name="Romain")
|
|
|
|
romains_trigramme = map(lambda x: str(100 + x), range(99))
|
2017-02-08 02:22:31 +01:00
|
|
|
|
2017-02-08 18:54:49 +01:00
|
|
|
created_accounts = 0
|
|
|
|
team_accounts = 0
|
|
|
|
|
2017-02-08 02:22:31 +01:00
|
|
|
for (profile, trigramme) in zip(gaulois, gaulois_trigramme):
|
2017-02-08 18:54:49 +01:00
|
|
|
account, created = Account.objects.get_or_create(
|
2017-02-08 02:22:31 +01:00
|
|
|
trigramme=trigramme,
|
2017-02-08 18:11:38 +01:00
|
|
|
cofprofile=profile,
|
2018-10-06 12:35:49 +02:00
|
|
|
defaults={"balance": random.randint(1, 999) / 10},
|
2017-02-08 02:22:31 +01:00
|
|
|
)
|
2017-02-08 18:54:49 +01:00
|
|
|
created_accounts += int(created)
|
2017-02-08 02:22:31 +01:00
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
if profile.user.first_name == "Abraracourcix":
|
2017-02-08 02:22:31 +01:00
|
|
|
profile.user.groups.add(group_chef)
|
|
|
|
|
|
|
|
for (profile, trigramme) in zip(romains, romains_trigramme):
|
2017-02-08 18:54:49 +01:00
|
|
|
account, created = Account.objects.get_or_create(
|
2017-02-08 02:22:31 +01:00
|
|
|
trigramme=trigramme,
|
2017-02-08 18:11:38 +01:00
|
|
|
cofprofile=profile,
|
2018-10-06 12:35:49 +02:00
|
|
|
defaults={"balance": random.randint(1, 999) / 10},
|
2017-02-08 02:22:31 +01:00
|
|
|
)
|
2017-02-08 18:54:49 +01:00
|
|
|
created_accounts += int(created)
|
2017-02-08 02:22:31 +01:00
|
|
|
|
2017-02-08 18:54:49 +01:00
|
|
|
if random.random() > 0.75 and created:
|
2017-02-08 02:22:31 +01:00
|
|
|
profile.user.groups.add(group_boy)
|
2017-02-08 18:54:49 +01:00
|
|
|
team_accounts += 1
|
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
self.stdout.write(
|
|
|
|
"- {:d} comptes créés, {:d} dans l'équipe K-Fêt".format(
|
|
|
|
created_accounts, team_accounts
|
|
|
|
)
|
|
|
|
)
|
2017-02-08 02:22:31 +01:00
|
|
|
|
|
|
|
# Compte liquide
|
|
|
|
|
2017-03-18 23:02:08 +01:00
|
|
|
self.stdout.write("Création du compte liquide")
|
2018-10-06 12:35:49 +02:00
|
|
|
liq_user, _ = User.objects.get_or_create(username="liquide")
|
2017-02-08 02:22:31 +01:00
|
|
|
liq_profile, _ = CofProfile.objects.get_or_create(user=liq_user)
|
2018-10-06 12:35:49 +02:00
|
|
|
liq_account, _ = Account.objects.get_or_create(
|
|
|
|
cofprofile=liq_profile, trigramme="LIQ"
|
|
|
|
)
|
2017-02-08 02:22:31 +01:00
|
|
|
|
2017-03-18 22:02:33 +01:00
|
|
|
# Root account if existing
|
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
root_profile = CofProfile.objects.filter(user__username="root")
|
2017-03-18 22:02:33 +01:00
|
|
|
if root_profile.exists():
|
2017-03-18 23:02:08 +01:00
|
|
|
self.stdout.write("Création du compte K-Fêt root")
|
2017-03-18 22:02:33 +01:00
|
|
|
root_profile = root_profile.get()
|
2018-10-06 12:35:49 +02:00
|
|
|
Account.objects.get_or_create(cofprofile=root_profile, trigramme="AAA")
|
2017-03-18 22:02:33 +01:00
|
|
|
|
2017-02-08 18:26:56 +01:00
|
|
|
# ---
|
|
|
|
# Caisse
|
|
|
|
# ---
|
|
|
|
|
2017-03-18 22:02:23 +01:00
|
|
|
checkout, created = Checkout.objects.get_or_create(
|
2018-10-06 12:35:49 +02:00
|
|
|
created_by=Account.objects.get(trigramme="000"),
|
|
|
|
name="Chaudron",
|
2017-03-18 22:02:23 +01:00
|
|
|
defaults={
|
2018-10-06 12:35:49 +02:00
|
|
|
"valid_from": timezone.now(),
|
|
|
|
"valid_to": timezone.now() + timedelta(days=730),
|
2017-03-18 22:02:23 +01:00
|
|
|
},
|
2017-02-08 18:26:56 +01:00
|
|
|
)
|
|
|
|
|
2017-03-18 22:02:23 +01:00
|
|
|
if created:
|
|
|
|
CheckoutStatement.objects.create(
|
2018-10-06 12:35:49 +02:00
|
|
|
by=Account.objects.get(trigramme="000"),
|
2017-03-18 22:02:23 +01:00
|
|
|
checkout=checkout,
|
|
|
|
balance_old=0,
|
|
|
|
balance_new=0,
|
|
|
|
amount_taken=0,
|
2018-10-06 12:35:49 +02:00
|
|
|
amount_error=0,
|
2017-03-18 22:02:23 +01:00
|
|
|
)
|
|
|
|
|
2017-03-30 04:16:40 +02:00
|
|
|
# ---
|
|
|
|
# Fournisseur
|
|
|
|
# ---
|
|
|
|
|
|
|
|
supplier, created = Supplier.objects.get_or_create(name="Panoramix")
|
|
|
|
if created:
|
2017-03-30 04:22:18 +02:00
|
|
|
articles = random.sample(list(Article.objects.all()), 40)
|
2017-03-30 04:16:40 +02:00
|
|
|
to_create = []
|
|
|
|
for article in articles:
|
2018-10-06 12:35:49 +02:00
|
|
|
to_create.append(SupplierArticle(supplier=supplier, article=article))
|
2017-03-30 04:16:40 +02:00
|
|
|
|
|
|
|
SupplierArticle.objects.bulk_create(to_create)
|
|
|
|
|
2017-02-08 02:22:31 +01:00
|
|
|
# ---
|
|
|
|
# Opérations
|
|
|
|
# ---
|
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
call_command("createopes", "100", "7", "--transfers=20")
|
2017-05-30 20:44:30 +02:00
|
|
|
|
|
|
|
# ---
|
|
|
|
# Wagtail CMS
|
|
|
|
# ---
|
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
call_command("kfet_loadwagtail")
|