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