mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-17 13:32:15 +00:00
Ensured hidden asset fields are completed on every database write
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import random
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.utils import timezone
|
||||
|
||||
from assets import models
|
||||
|
||||
|
||||
@@ -96,8 +95,9 @@ class Command(BaseCommand):
|
||||
cores=random.choice(circuits)
|
||||
)
|
||||
|
||||
prefix = random.choice(asset_prefixes)
|
||||
asset.asset_id = prefix + str(models.Asset.get_available_asset_id(wanted_prefix=prefix))
|
||||
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()
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
# Generated by Django 2.0.13 on 2019-12-05 16:53
|
||||
# Edited by Matthew Smith on same date.
|
||||
# Generated by Django 2.0.13 on 2019-12-05 19:37
|
||||
|
||||
import re
|
||||
from django.db import migrations, models
|
||||
from django.db import migrations, models
|
||||
import django.db.migrations.operations.special
|
||||
|
||||
import django.db.migrations
|
||||
|
||||
def forwards(apps, schema_editor):
|
||||
AssetModel = apps.get_model('assets', 'Asset')
|
||||
@@ -25,8 +22,6 @@ def forwards(apps, schema_editor):
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
replaces = [('assets', '0008_auto_20191205_1354'), ('assets', '0009_auto_20191205_1652')]
|
||||
|
||||
dependencies = [
|
||||
('assets', '0007_auto_20190108_0202_squashed_0014_auto_20191017_2052'),
|
||||
]
|
||||
@@ -35,14 +30,12 @@ class Migration(migrations.Migration):
|
||||
migrations.AddField(
|
||||
model_name='asset',
|
||||
name='asset_id_number',
|
||||
field=models.IntegerField(default=0),
|
||||
preserve_default=False,
|
||||
field=models.IntegerField(default=1),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='asset',
|
||||
name='asset_id_prefix',
|
||||
field=models.CharField(default=0, max_length=5),
|
||||
preserve_default=False,
|
||||
field=models.CharField(default='', max_length=5),
|
||||
),
|
||||
migrations.RunPython(
|
||||
code=forwards,
|
||||
@@ -3,6 +3,9 @@ from django.core.exceptions import ValidationError
|
||||
from django.db import models, connection
|
||||
from django.urls import reverse
|
||||
|
||||
from django.db.models.signals import pre_save
|
||||
from django.dispatch.dispatcher import receiver
|
||||
|
||||
|
||||
class AssetCategory(models.Model):
|
||||
class Meta:
|
||||
@@ -85,8 +88,8 @@ class Asset(models.Model):
|
||||
|
||||
# Hidden asset_id components
|
||||
# For example, if asset_id was "C1001" then asset_id_prefix would be "C" and number "1001"
|
||||
asset_id_prefix = models.CharField(max_length=5)
|
||||
asset_id_number = models.IntegerField()
|
||||
asset_id_prefix = models.CharField(max_length=5, default="")
|
||||
asset_id_number = models.IntegerField(default=1)
|
||||
|
||||
def get_available_asset_id(wanted_prefix=""):
|
||||
sql = """
|
||||
@@ -120,11 +123,9 @@ class Asset(models.Model):
|
||||
errdict["date_sold"] = ["Cannot sell an item before it is acquired"]
|
||||
|
||||
self.asset_id = self.asset_id.upper()
|
||||
asset_search = re.search("^([A-Z0-9]*?[A-Z]?)([0-9]+)$", self.asset_id)
|
||||
asset_search = re.search("^([a-zA-Z0-9]*?[a-zA-Z]?)([0-9]+)$", self.asset_id)
|
||||
if asset_search is None:
|
||||
errdict["asset_id"] = ["An Asset ID can only consist of letters and numbers"]
|
||||
self.asset_id_prefix = asset_search.group(1)
|
||||
self.asset_id_number = int(asset_search.group(2))
|
||||
errdict["asset_id"] = ["An Asset ID can only consist of letters and numbers, with a final number"]
|
||||
|
||||
if self.purchase_price and self.purchase_price < 0:
|
||||
errdict["purchase_price"] = ["A price cannot be negative"]
|
||||
@@ -148,3 +149,14 @@ class Asset(models.Model):
|
||||
|
||||
if errdict != {}: # If there was an error when validation
|
||||
raise ValidationError(errdict)
|
||||
|
||||
|
||||
@receiver(pre_save, sender=Asset)
|
||||
def pre_save_asset(sender, instance, **kwargs):
|
||||
"""Automatically fills in hidden members on database access"""
|
||||
asset_search = re.search("^([a-zA-Z0-9]*?[a-zA-Z]?)([0-9]+)$", instance.asset_id)
|
||||
if asset_search is None:
|
||||
instance.asset_id += "1"
|
||||
asset_search = re.search("^([a-zA-Z0-9]*?[a-zA-Z]?)([0-9]+)$", instance.asset_id)
|
||||
instance.asset_id_prefix = asset_search.group(1)
|
||||
instance.asset_id_number = int(asset_search.group(2))
|
||||
|
||||
Reference in New Issue
Block a user