gestioCOF/kfet/management/commands/loadkfetdevdata.py

171 lines
5.5 KiB
Python
Raw Normal View History

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
2017-02-08 18:38:27 +01:00
from decimal import Decimal
2017-02-08 02:22:31 +01:00
from django.utils import timezone
from django.contrib.auth.models import User, Group, Permission, ContentType
from cof.management.base import MyBaseCommand
from gestion.models import Profile
2017-02-11 21:20:51 +01:00
from kfet.models import Account, Article, OperationGroup, Operation, Checkout
2017-02-08 02:22:31 +01:00
# Où sont stockés les fichiers json
DATA_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)),
'data')
class Command(MyBaseCommand):
help = "Crée des utilisateurs, des articles et des opérations aléatoires"
def handle(self, *args, **options):
# ---
# Groupes
# ---
Group.objects.filter(name__icontains='K-Fêt').delete()
group_chef = Group(name="K-Fêt César")
group_boy = Group(name="K-Fêt Légionnaire")
group_chef.save()
group_boy.save()
permissions_chef = Permission.objects.filter(
content_type__in=ContentType.objects.filter(
app_label='kfet'))
permissions_boy = Permission.objects.filter(
codename__in=['is_team', 'perform_deposit'])
group_chef.permissions.add(*permissions_chef)
group_boy.permissions.add(*permissions_boy)
# ---
# Comptes
# ---
2017-02-08 18:54:49 +01:00
self.stdout.write("Création des comptes K-Fêt")
gaulois = Profile.objects.filter(user__last_name='Gaulois')
2017-02-08 02:22:31 +01:00
gaulois_trigramme = map('{:03d}'.format, range(50))
romains = Profile.objects.filter(user__last_name='Romain')
2017-02-08 18:11:38 +01:00
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,
profile=profile,
2017-02-08 18:11:38 +01: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
if profile.user.first_name == 'Abraracourcix':
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,
profile=profile,
2017-02-08 18:11:38 +01: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
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
liq_user, _ = User.objects.get_or_create(username='liquide')
liq_profile, _ = Profile.objects.get_or_create(user=liq_user)
liq_account, _ = Account.objects.get_or_create(profile=liq_profile,
2017-02-08 18:32:05 +01:00
trigramme='LIQ')
2017-02-08 02:22:31 +01:00
2017-02-08 18:26:56 +01:00
# ---
# Caisse
# ---
2017-02-08 18:32:05 +01:00
checkout, _ = Checkout.objects.get_or_create(
2017-02-08 18:29:40 +01:00
created_by=Account.objects.get(trigramme='000'),
2017-02-08 18:26:56 +01:00
name='Chaudron',
valid_from=timezone.now(),
2017-02-08 18:31:10 +01:00
valid_to=timezone.now() + timedelta(days=365)
2017-02-08 18:26:56 +01:00
)
2017-02-08 02:22:31 +01:00
# ---
# Opérations
# ---
2017-02-08 18:54:49 +01:00
self.stdout.write("Génération d'opérations")
2017-02-08 02:22:31 +01:00
articles = Article.objects.all()
2017-02-08 18:11:38 +01:00
accounts = Account.objects.exclude(trigramme='LIQ')
2017-02-08 02:22:31 +01:00
num_op = 100
2017-02-08 18:11:38 +01:00
# Operations are put uniformly over the span of a week
past_date = 3600*24*7
2017-02-08 02:22:31 +01:00
for i in range(num_op):
2017-02-08 18:11:38 +01:00
if random.random() > 0.25:
account = random.choice(accounts)
else:
account = liq_account
2017-02-08 18:38:27 +01:00
amount = Decimal('0')
2017-02-08 02:22:31 +01:00
at = timezone.now() - timedelta(
seconds=random.randint(0, past_date))
opegroup = OperationGroup(
on_acc=account,
checkout=checkout,
at=at,
is_cof=False
)
if hasattr(account.profile, "cof"):
opegroup.is_cof = account.profile.cof.is_cof
opegroup.save()
2017-02-08 02:22:31 +01:00
opegroup.save()
for j in range(random.randint(1, 4)):
2017-02-08 18:11:38 +01:00
typevar = random.random()
if typevar > 0.9 and account != liq_account:
ope = Operation(
group=opegroup,
type=Operation.DEPOSIT,
2017-02-08 18:38:27 +01:00
amount=Decimal(random.randint(1, 99)/10,)
2017-02-08 18:11:38 +01:00
)
elif typevar > 0.8 and account != liq_account:
ope = Operation(
group=opegroup,
type=Operation.WITHDRAW,
2017-02-08 18:38:59 +01:00
amount=-Decimal(random.randint(1, 99)/10,)
2017-02-08 18:11:38 +01:00
)
else:
article = random.choice(articles)
nb = random.randint(1, 5)
ope = Operation(
group=opegroup,
type=Operation.PURCHASE,
amount=-article.price*nb,
article=article,
article_nb=nb
)
2017-02-08 02:22:31 +01:00
ope.save()
amount += ope.amount
opegroup.amount = amount
opegroup.save()