forked from DGNum/gestioCOF
core -- Apply black + isort to all files
This commit is contained in:
parent
104e71dcf6
commit
fdd2b35289
196 changed files with 10727 additions and 8365 deletions
|
@ -1,4 +1,3 @@
|
|||
|
||||
"""
|
||||
Crée des opérations aléatoires réparties sur une période de temps spécifiée
|
||||
"""
|
||||
|
@ -6,29 +5,40 @@ Crée des opérations aléatoires réparties sur une période de temps spécifi
|
|||
import random
|
||||
from datetime import timedelta
|
||||
from decimal import Decimal
|
||||
from django.utils import timezone
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from kfet.models import (Account, Article, OperationGroup, Operation,
|
||||
Checkout, Transfer, TransferGroup)
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.utils import timezone
|
||||
|
||||
from kfet.models import (
|
||||
Account,
|
||||
Article,
|
||||
Checkout,
|
||||
Operation,
|
||||
OperationGroup,
|
||||
Transfer,
|
||||
TransferGroup,
|
||||
)
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = ("Crée des opérations réparties uniformément "
|
||||
"sur une période de temps")
|
||||
help = "Crée des opérations réparties uniformément " "sur une période de temps"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
# Nombre d'opérations à créer
|
||||
parser.add_argument('opes', type=int,
|
||||
help='Number of opegroups to create')
|
||||
parser.add_argument("opes", type=int, help="Number of opegroups to create")
|
||||
|
||||
# Période sur laquelle créer (depuis num_days avant maintenant)
|
||||
parser.add_argument('days', type=int,
|
||||
help='Period in which to create opegroups')
|
||||
parser.add_argument(
|
||||
"days", type=int, help="Period in which to create opegroups"
|
||||
)
|
||||
|
||||
# Optionnel : nombre de transfert à créer (défaut 0)
|
||||
parser.add_argument('--transfers', type=int, default=0,
|
||||
help='Number of transfers to create (default 0)')
|
||||
parser.add_argument(
|
||||
"--transfers",
|
||||
type=int,
|
||||
default=0,
|
||||
help="Number of transfers to create (default 0)",
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
|
||||
|
@ -39,19 +49,19 @@ class Command(BaseCommand):
|
|||
purchases = 0
|
||||
transfers = 0
|
||||
|
||||
num_ops = options['opes']
|
||||
num_transfers = options['transfers']
|
||||
num_ops = options["opes"]
|
||||
num_transfers = options["transfers"]
|
||||
# Convert to seconds
|
||||
time = options['days'] * 24 * 3600
|
||||
time = options["days"] * 24 * 3600
|
||||
|
||||
now = timezone.now()
|
||||
checkout = Checkout.objects.first()
|
||||
articles = Article.objects.all()
|
||||
accounts = Account.objects.exclude(trigramme='LIQ')
|
||||
liq_account = Account.objects.get(trigramme='LIQ')
|
||||
accounts = Account.objects.exclude(trigramme="LIQ")
|
||||
liq_account = Account.objects.get(trigramme="LIQ")
|
||||
try:
|
||||
con_account = Account.objects.get(
|
||||
cofprofile__user__first_name='Assurancetourix'
|
||||
cofprofile__user__first_name="Assurancetourix"
|
||||
)
|
||||
except Account.DoesNotExist:
|
||||
con_account = random.choice(accounts)
|
||||
|
@ -78,12 +88,12 @@ class Command(BaseCommand):
|
|||
if random.random() < 0.2:
|
||||
addcost = True
|
||||
addcost_for = con_account
|
||||
addcost_amount = Decimal('0.5')
|
||||
addcost_amount = Decimal("0.5")
|
||||
else:
|
||||
addcost = False
|
||||
|
||||
# Initialize opegroup amount
|
||||
amount = Decimal('0')
|
||||
amount = Decimal("0")
|
||||
|
||||
# Generating operations
|
||||
ope_list = []
|
||||
|
@ -95,19 +105,18 @@ class Command(BaseCommand):
|
|||
if typevar > 0.9 and account != liq_account:
|
||||
ope = Operation(
|
||||
type=Operation.DEPOSIT,
|
||||
amount=Decimal(random.randint(1, 99)/10)
|
||||
amount=Decimal(random.randint(1, 99) / 10),
|
||||
)
|
||||
# 0.05 probability to have a withdrawal
|
||||
elif typevar > 0.85 and account != liq_account:
|
||||
ope = Operation(
|
||||
type=Operation.WITHDRAW,
|
||||
amount=-Decimal(random.randint(1, 99)/10)
|
||||
amount=-Decimal(random.randint(1, 99) / 10),
|
||||
)
|
||||
# 0.05 probability to have an edition
|
||||
elif typevar > 0.8 and account != liq_account:
|
||||
ope = Operation(
|
||||
type=Operation.EDIT,
|
||||
amount=Decimal(random.randint(1, 99)/10)
|
||||
type=Operation.EDIT, amount=Decimal(random.randint(1, 99) / 10)
|
||||
)
|
||||
else:
|
||||
article = random.choice(articles)
|
||||
|
@ -115,9 +124,9 @@ class Command(BaseCommand):
|
|||
|
||||
ope = Operation(
|
||||
type=Operation.PURCHASE,
|
||||
amount=-article.price*nb,
|
||||
amount=-article.price * nb,
|
||||
article=article,
|
||||
article_nb=nb
|
||||
article_nb=nb,
|
||||
)
|
||||
|
||||
purchases += 1
|
||||
|
@ -130,23 +139,23 @@ class Command(BaseCommand):
|
|||
ope_list.append(ope)
|
||||
amount += ope.amount
|
||||
|
||||
opegroup_list.append(OperationGroup(
|
||||
on_acc=account,
|
||||
checkout=checkout,
|
||||
at=at,
|
||||
is_cof=account.cofprofile.is_cof,
|
||||
amount=amount,
|
||||
))
|
||||
opegroup_list.append(
|
||||
OperationGroup(
|
||||
on_acc=account,
|
||||
checkout=checkout,
|
||||
at=at,
|
||||
is_cof=account.cofprofile.is_cof,
|
||||
amount=amount,
|
||||
)
|
||||
)
|
||||
at_list.append(at)
|
||||
ope_by_grp.append((at, ope_list, ))
|
||||
ope_by_grp.append((at, ope_list))
|
||||
|
||||
OperationGroup.objects.bulk_create(opegroup_list)
|
||||
|
||||
# Fetch created OperationGroup objects pk by at
|
||||
opegroups = (OperationGroup.objects
|
||||
.filter(at__in=at_list)
|
||||
.values('id', 'at'))
|
||||
opegroups_by = {grp['at']: grp['id'] for grp in opegroups}
|
||||
opegroups = OperationGroup.objects.filter(at__in=at_list).values("id", "at")
|
||||
opegroups_by = {grp["at"]: grp["id"] for grp in opegroups}
|
||||
|
||||
all_ope = []
|
||||
for _ in range(num_ops):
|
||||
|
@ -175,30 +184,28 @@ class Command(BaseCommand):
|
|||
else:
|
||||
comment = ""
|
||||
|
||||
transfergroup_list.append(TransferGroup(
|
||||
at=at,
|
||||
comment=comment,
|
||||
valid_by=random.choice(accounts),
|
||||
))
|
||||
transfergroup_list.append(
|
||||
TransferGroup(at=at, comment=comment, valid_by=random.choice(accounts))
|
||||
)
|
||||
at_list.append(at)
|
||||
|
||||
# Randomly generate transfer
|
||||
transfer_list = []
|
||||
for i in range(random.randint(1, 4)):
|
||||
transfer_list.append(Transfer(
|
||||
from_acc=random.choice(accounts),
|
||||
to_acc=random.choice(accounts),
|
||||
amount=Decimal(random.randint(1, 99)/10)
|
||||
))
|
||||
transfer_list.append(
|
||||
Transfer(
|
||||
from_acc=random.choice(accounts),
|
||||
to_acc=random.choice(accounts),
|
||||
amount=Decimal(random.randint(1, 99) / 10),
|
||||
)
|
||||
)
|
||||
|
||||
transfer_by_grp.append((at, transfer_list, ))
|
||||
transfer_by_grp.append((at, transfer_list))
|
||||
|
||||
TransferGroup.objects.bulk_create(transfergroup_list)
|
||||
|
||||
transfergroups = (TransferGroup.objects
|
||||
.filter(at__in=at_list)
|
||||
.values('id', 'at'))
|
||||
transfergroups_by = {grp['at']: grp['id'] for grp in transfergroups}
|
||||
transfergroups = TransferGroup.objects.filter(at__in=at_list).values("id", "at")
|
||||
transfergroups_by = {grp["at"]: grp["id"] for grp in transfergroups}
|
||||
|
||||
all_transfer = []
|
||||
for _ in range(num_transfers):
|
||||
|
@ -211,9 +218,10 @@ class Command(BaseCommand):
|
|||
transfers += len(all_transfer)
|
||||
|
||||
self.stdout.write(
|
||||
"- {:d} opérations créées dont {:d} commandes d'articles"
|
||||
.format(opes_created, purchases))
|
||||
"- {:d} opérations créées dont {:d} commandes d'articles".format(
|
||||
opes_created, purchases
|
||||
)
|
||||
)
|
||||
|
||||
if transfers:
|
||||
self.stdout.write("- {:d} transferts créés"
|
||||
.format(transfers))
|
||||
self.stdout.write("- {:d} transferts créés".format(transfers))
|
||||
|
|
|
@ -6,18 +6,23 @@ import os
|
|||
import random
|
||||
from datetime import timedelta
|
||||
|
||||
from django.utils import timezone
|
||||
from django.contrib.auth.models import User, Group, Permission, ContentType
|
||||
from django.contrib.auth.models import ContentType, Group, Permission, User
|
||||
from django.core.management import call_command
|
||||
from django.utils import timezone
|
||||
|
||||
from gestioncof.management.base import MyBaseCommand
|
||||
from gestioncof.models import CofProfile
|
||||
from kfet.models import (Account, Checkout, CheckoutStatement, Supplier,
|
||||
SupplierArticle, Article)
|
||||
from kfet.models import (
|
||||
Account,
|
||||
Article,
|
||||
Checkout,
|
||||
CheckoutStatement,
|
||||
Supplier,
|
||||
SupplierArticle,
|
||||
)
|
||||
|
||||
# Où sont stockés les fichiers json
|
||||
DATA_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)),
|
||||
'data')
|
||||
DATA_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), "data")
|
||||
|
||||
|
||||
class Command(MyBaseCommand):
|
||||
|
@ -28,7 +33,7 @@ class Command(MyBaseCommand):
|
|||
# Groupes
|
||||
# ---
|
||||
|
||||
Group.objects.filter(name__icontains='K-Fêt').delete()
|
||||
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")
|
||||
|
@ -37,10 +42,11 @@ class Command(MyBaseCommand):
|
|||
group_boy.save()
|
||||
|
||||
permissions_chef = Permission.objects.filter(
|
||||
content_type__in=ContentType.objects.filter(
|
||||
app_label='kfet'))
|
||||
content_type__in=ContentType.objects.filter(app_label="kfet")
|
||||
)
|
||||
permissions_boy = Permission.objects.filter(
|
||||
codename__in=['is_team', 'perform_deposit'])
|
||||
codename__in=["is_team", "perform_deposit"]
|
||||
)
|
||||
|
||||
group_chef.permissions.add(*permissions_chef)
|
||||
group_boy.permissions.add(*permissions_boy)
|
||||
|
@ -51,11 +57,11 @@ class Command(MyBaseCommand):
|
|||
|
||||
self.stdout.write("Création des comptes K-Fêt")
|
||||
|
||||
gaulois = CofProfile.objects.filter(user__last_name='Gaulois')
|
||||
gaulois_trigramme = map('{:03d}'.format, range(50))
|
||||
gaulois = CofProfile.objects.filter(user__last_name="Gaulois")
|
||||
gaulois_trigramme = map("{:03d}".format, range(50))
|
||||
|
||||
romains = CofProfile.objects.filter(user__last_name='Romain')
|
||||
romains_trigramme = map(lambda x: str(100+x), range(99))
|
||||
romains = CofProfile.objects.filter(user__last_name="Romain")
|
||||
romains_trigramme = map(lambda x: str(100 + x), range(99))
|
||||
|
||||
created_accounts = 0
|
||||
team_accounts = 0
|
||||
|
@ -64,18 +70,18 @@ class Command(MyBaseCommand):
|
|||
account, created = Account.objects.get_or_create(
|
||||
trigramme=trigramme,
|
||||
cofprofile=profile,
|
||||
defaults={'balance': random.randint(1, 999)/10}
|
||||
defaults={"balance": random.randint(1, 999) / 10},
|
||||
)
|
||||
created_accounts += int(created)
|
||||
|
||||
if profile.user.first_name == 'Abraracourcix':
|
||||
if profile.user.first_name == "Abraracourcix":
|
||||
profile.user.groups.add(group_chef)
|
||||
|
||||
for (profile, trigramme) in zip(romains, romains_trigramme):
|
||||
account, created = Account.objects.get_or_create(
|
||||
trigramme=trigramme,
|
||||
cofprofile=profile,
|
||||
defaults={'balance': random.randint(1, 999)/10}
|
||||
defaults={"balance": random.randint(1, 999) / 10},
|
||||
)
|
||||
created_accounts += int(created)
|
||||
|
||||
|
@ -83,47 +89,50 @@ class Command(MyBaseCommand):
|
|||
profile.user.groups.add(group_boy)
|
||||
team_accounts += 1
|
||||
|
||||
self.stdout.write("- {:d} comptes créés, {:d} dans l'équipe K-Fêt"
|
||||
.format(created_accounts, team_accounts))
|
||||
self.stdout.write(
|
||||
"- {:d} comptes créés, {:d} dans l'équipe K-Fêt".format(
|
||||
created_accounts, team_accounts
|
||||
)
|
||||
)
|
||||
|
||||
# Compte liquide
|
||||
|
||||
self.stdout.write("Création du compte liquide")
|
||||
liq_user, _ = User.objects.get_or_create(username='liquide')
|
||||
liq_user, _ = User.objects.get_or_create(username="liquide")
|
||||
liq_profile, _ = CofProfile.objects.get_or_create(user=liq_user)
|
||||
liq_account, _ = Account.objects.get_or_create(cofprofile=liq_profile,
|
||||
trigramme='LIQ')
|
||||
liq_account, _ = Account.objects.get_or_create(
|
||||
cofprofile=liq_profile, trigramme="LIQ"
|
||||
)
|
||||
|
||||
# Root account if existing
|
||||
|
||||
root_profile = CofProfile.objects.filter(user__username='root')
|
||||
root_profile = CofProfile.objects.filter(user__username="root")
|
||||
if root_profile.exists():
|
||||
self.stdout.write("Création du compte K-Fêt root")
|
||||
root_profile = root_profile.get()
|
||||
Account.objects.get_or_create(cofprofile=root_profile,
|
||||
trigramme='AAA')
|
||||
Account.objects.get_or_create(cofprofile=root_profile, trigramme="AAA")
|
||||
|
||||
# ---
|
||||
# Caisse
|
||||
# ---
|
||||
|
||||
checkout, created = Checkout.objects.get_or_create(
|
||||
created_by=Account.objects.get(trigramme='000'),
|
||||
name='Chaudron',
|
||||
created_by=Account.objects.get(trigramme="000"),
|
||||
name="Chaudron",
|
||||
defaults={
|
||||
'valid_from': timezone.now(),
|
||||
'valid_to': timezone.now() + timedelta(days=730)
|
||||
"valid_from": timezone.now(),
|
||||
"valid_to": timezone.now() + timedelta(days=730),
|
||||
},
|
||||
)
|
||||
|
||||
if created:
|
||||
CheckoutStatement.objects.create(
|
||||
by=Account.objects.get(trigramme='000'),
|
||||
by=Account.objects.get(trigramme="000"),
|
||||
checkout=checkout,
|
||||
balance_old=0,
|
||||
balance_new=0,
|
||||
amount_taken=0,
|
||||
amount_error=0
|
||||
amount_error=0,
|
||||
)
|
||||
|
||||
# ---
|
||||
|
@ -135,10 +144,7 @@ class Command(MyBaseCommand):
|
|||
articles = random.sample(list(Article.objects.all()), 40)
|
||||
to_create = []
|
||||
for article in articles:
|
||||
to_create.append(SupplierArticle(
|
||||
supplier=supplier,
|
||||
article=article
|
||||
))
|
||||
to_create.append(SupplierArticle(supplier=supplier, article=article))
|
||||
|
||||
SupplierArticle.objects.bulk_create(to_create)
|
||||
|
||||
|
@ -146,10 +152,10 @@ class Command(MyBaseCommand):
|
|||
# Opérations
|
||||
# ---
|
||||
|
||||
call_command('createopes', '100', '7', '--transfers=20')
|
||||
call_command("createopes", "100", "7", "--transfers=20")
|
||||
|
||||
# ---
|
||||
# Wagtail CMS
|
||||
# ---
|
||||
|
||||
call_command('kfet_loadwagtail')
|
||||
call_command("kfet_loadwagtail")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue