From 1f0dc9f1aead457390ee085ca5e537af3635aa12 Mon Sep 17 00:00:00 2001 From: Matthew Smith Date: Thu, 5 Dec 2019 15:28:07 +0000 Subject: [PATCH] Changed generateSampleAssetsData.py to include prefices on some cables. --- .../commands/generateSampleAssetsData.py | 16 +++++++++++----- assets/models.py | 6 +++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/assets/management/commands/generateSampleAssetsData.py b/assets/management/commands/generateSampleAssetsData.py index c41de7fa..00dcbcda 100644 --- a/assets/management/commands/generateSampleAssetsData.py +++ b/assets/management/commands/generateSampleAssetsData.py @@ -1,8 +1,8 @@ +import random from django.core.management.base import BaseCommand, CommandError from django.utils import timezone from assets import models -import random class Command(BaseCommand): @@ -50,7 +50,7 @@ class Command(BaseCommand): suppliers = models.Supplier.objects.all() for i in range(100): - asset = models.Asset.objects.create( + asset = models.Asset( asset_id='{}'.format(models.Asset.get_available_asset_id()), description=random.choice(asset_description), category=random.choice(categories), @@ -63,11 +63,12 @@ class Command(BaseCommand): if i % 3 == 0: asset.purchased_from = random.choice(suppliers) - + asset.clean() asset.save() 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_prefixes = ["C","C4P","CBNC", "CDMX", "CDV", "CRCD", "CSOCA", "CXLR"] csas = [0.75, 1.00, 1.25, 2.5, 4] lengths = [1, 2, 5, 10, 15, 20, 25, 30, 50, 100] @@ -79,7 +80,7 @@ class Command(BaseCommand): connectors = models.Connector.objects.all() for i in range(100): - asset = models.Asset.objects.create( + asset = models.Asset( asset_id='{}'.format(models.Asset.get_available_asset_id()), description=random.choice(asset_description), category=random.choice(categories), @@ -95,12 +96,17 @@ class Command(BaseCommand): 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: asset.parent = models.Asset.objects.order_by('?').first() if i % 3 == 0: asset.purchased_from = random.choice(suppliers) - + + asset.clean() asset.save() def create_connectors(self): diff --git a/assets/models.py b/assets/models.py index 6e54c5e8..e838488d 100644 --- a/assets/models.py +++ b/assets/models.py @@ -95,12 +95,12 @@ class Asset(models.Model): LEFT OUTER JOIN assets_asset b ON (a.asset_id_number + 1 = b.asset_id_number AND 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: - cursor.execute(sql, [9000]) + cursor.execute(sql, [9000, wanted_prefix]) row = cursor.fetchone() - if row[0] is None: + if row is None or row[0] is None: return 9000 else: return row[0]