Improves creation efficiency

This commit is contained in:
Ludovic Stephan 2017-03-20 20:19:04 -03:00
parent 6d36d50e9a
commit dd4d1f3061
2 changed files with 15 additions and 15 deletions

View file

@ -78,14 +78,15 @@ class Command(BaseCommand):
# Initialize opegroup amount # Initialize opegroup amount
amount = Decimal('0') amount = Decimal('0')
opegroup = OperationGroup( opegroup = OperationGroup.objects.create(
on_acc=account, on_acc=account,
checkout=checkout, checkout=checkout,
at=at,
is_cof=account.cofprofile.is_cof is_cof=account.cofprofile.is_cof
) )
opegroup.save()
# Generating operations # Generating operations
ope_list = []
for j in range(random.randint(1, 4)): for j in range(random.randint(1, 4)):
# Operation type # Operation type
typevar = random.random() typevar = random.random()
@ -125,12 +126,12 @@ class Command(BaseCommand):
ope.addcost_amount = addcost_amount * nb ope.addcost_amount = addcost_amount * nb
ope.amount -= ope.addcost_amount ope.amount -= ope.addcost_amount
opes_created += 1 ope_list.append(ope)
ope.save()
amount += ope.amount amount += ope.amount
Operation.objects.bulk_create(ope_list)
opes_created += len(ope_list)
opegroup.amount = amount opegroup.amount = amount
opegroup.at = at
opegroup.save() opegroup.save()
# Transfer generation # Transfer generation
@ -146,25 +147,24 @@ class Command(BaseCommand):
else: else:
comment = "" comment = ""
transfergroup = TransferGroup( transfergroup = TransferGroup.objects.create(
at=at,
comment=comment, comment=comment,
valid_by=random.choice(accounts) valid_by=random.choice(accounts)
) )
transfergroup.save()
# Randomly generate transfer # Randomly generate transfer
transfer_list = []
for i in range(random.randint(1, 4)): for i in range(random.randint(1, 4)):
transfer = Transfer( transfer_list.append(Transfer(
group=transfergroup, group=transfergroup,
from_acc=random.choice(accounts), from_acc=random.choice(accounts),
to_acc=random.choice(accounts), to_acc=random.choice(accounts),
amount=Decimal(random.randint(1, 99)/10) amount=Decimal(random.randint(1, 99)/10)
) ))
transfer.save()
transfers += 1
transfergroup.at = at Transfer.objects.bulk_create(transfer_list)
transfergroup.save() transfers += len(transfer_list)
self.stdout.write( self.stdout.write(
"- {:d} opérations créées dont {:d} commandes d'articles" "- {:d} opérations créées dont {:d} commandes d'articles"

View file

@ -455,7 +455,7 @@ class OrderArticle(models.Model):
quantity_received = models.IntegerField(default = 0) quantity_received = models.IntegerField(default = 0)
class TransferGroup(models.Model): class TransferGroup(models.Model):
at = models.DateTimeField(auto_now_add = True) at = models.DateTimeField(default=timezone.now)
# Optional # Optional
comment = models.CharField( comment = models.CharField(
max_length = 255, max_length = 255,
@ -491,7 +491,7 @@ class OperationGroup(models.Model):
checkout = models.ForeignKey( checkout = models.ForeignKey(
Checkout, on_delete = models.PROTECT, Checkout, on_delete = models.PROTECT,
related_name = "opesgroup") related_name = "opesgroup")
at = models.DateTimeField(auto_now_add = True) at = models.DateTimeField(default=timezone.now)
amount = models.DecimalField( amount = models.DecimalField(
max_digits = 6, decimal_places = 2, max_digits = 6, decimal_places = 2,
default = 0) default = 0)