Changed generateSampleAssetsData.py to include prefices on some cables.

This commit is contained in:
Matthew Smith
2019-12-05 15:28:07 +00:00
parent 8ad0bdf5f3
commit 1f0dc9f1ae
2 changed files with 14 additions and 8 deletions

View File

@@ -1,8 +1,8 @@
import random
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from django.utils import timezone from django.utils import timezone
from assets import models from assets import models
import random
class Command(BaseCommand): class Command(BaseCommand):
@@ -50,7 +50,7 @@ class Command(BaseCommand):
suppliers = models.Supplier.objects.all() suppliers = models.Supplier.objects.all()
for i in range(100): for i in range(100):
asset = models.Asset.objects.create( asset = models.Asset(
asset_id='{}'.format(models.Asset.get_available_asset_id()), asset_id='{}'.format(models.Asset.get_available_asset_id()),
description=random.choice(asset_description), description=random.choice(asset_description),
category=random.choice(categories), category=random.choice(categories),
@@ -63,11 +63,12 @@ class Command(BaseCommand):
if i % 3 == 0: if i % 3 == 0:
asset.purchased_from = random.choice(suppliers) asset.purchased_from = random.choice(suppliers)
asset.clean()
asset.save() asset.save()
def create_cables(self): def create_cables(self):
asset_description = ['The worm', 'Harting without a cap', 'Heavy cable', 'Extension lead', 'IEC cable that we should remember to prep'] asset_description = ['The worm', 'Harting without a cap', 'Heavy cable', 'Extension lead', 'IEC cable that we should remember to prep']
asset_prefixes = ["C","C4P","CBNC", "CDMX", "CDV", "CRCD", "CSOCA", "CXLR"]
csas = [0.75, 1.00, 1.25, 2.5, 4] csas = [0.75, 1.00, 1.25, 2.5, 4]
lengths = [1, 2, 5, 10, 15, 20, 25, 30, 50, 100] lengths = [1, 2, 5, 10, 15, 20, 25, 30, 50, 100]
@@ -79,7 +80,7 @@ class Command(BaseCommand):
connectors = models.Connector.objects.all() connectors = models.Connector.objects.all()
for i in range(100): for i in range(100):
asset = models.Asset.objects.create( asset = models.Asset(
asset_id='{}'.format(models.Asset.get_available_asset_id()), asset_id='{}'.format(models.Asset.get_available_asset_id()),
description=random.choice(asset_description), description=random.choice(asset_description),
category=random.choice(categories), category=random.choice(categories),
@@ -95,12 +96,17 @@ class Command(BaseCommand):
cores=random.choice(circuits) cores=random.choice(circuits)
) )
if i % 5 == 0:
prefix = random.choice(asset_prefixes)
asset.asset_id = prefix + str(models.Asset.get_available_asset_id(wanted_prefix=prefix))
if i % 4 == 0: if i % 4 == 0:
asset.parent = models.Asset.objects.order_by('?').first() asset.parent = models.Asset.objects.order_by('?').first()
if i % 3 == 0: if i % 3 == 0:
asset.purchased_from = random.choice(suppliers) asset.purchased_from = random.choice(suppliers)
asset.clean()
asset.save() asset.save()
def create_connectors(self): def create_connectors(self):

View File

@@ -95,12 +95,12 @@ class Asset(models.Model):
LEFT OUTER JOIN assets_asset b ON LEFT OUTER JOIN assets_asset b ON
(a.asset_id_number + 1 = b.asset_id_number AND (a.asset_id_number + 1 = b.asset_id_number AND
a.asset_id_prefix = b.asset_id_prefix) a.asset_id_prefix = b.asset_id_prefix)
WHERE b.asset_id IS NULL AND a.asset_id_number >= %s AND a.asset_id_prefix = ''; WHERE b.asset_id IS NULL AND a.asset_id_number >= %s AND a.asset_id_prefix = %s;
""" """
with connection.cursor() as cursor: with connection.cursor() as cursor:
cursor.execute(sql, [9000]) cursor.execute(sql, [9000, wanted_prefix])
row = cursor.fetchone() row = cursor.fetchone()
if row[0] is None: if row is None or row[0] is None:
return 9000 return 9000
else: else:
return row[0] return row[0]