mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-30 03:42:15 +00:00
Added form validation
* Asset IDs must be unique * Asset IDs that are legacy and unchange have no validation applied * Asset IDs must be alphanumeric * Values must be >=0 * Lengths, CSAs, Circuits and Cores of cables must all be >0
This commit is contained in:
18
RIGS/migrations/0035_auto_20191008_2148.py
Normal file
18
RIGS/migrations/0035_auto_20191008_2148.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 2.0.13 on 2019-10-08 20:48
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('RIGS', '0034_event_risk_assessment_edit_url'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='event',
|
||||||
|
name='risk_assessment_edit_url',
|
||||||
|
field=models.CharField(blank=True, max_length=255, null=True, verbose_name='risk assessment'),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -1,16 +1,12 @@
|
|||||||
from django import forms
|
from django import forms
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
|
import re
|
||||||
|
|
||||||
from assets import models
|
from assets import models
|
||||||
|
|
||||||
|
|
||||||
class AssetForm(forms.ModelForm):
|
class AssetForm(forms.ModelForm):
|
||||||
|
|
||||||
|
|
||||||
def clean_date_sold(self):
|
|
||||||
if self.cleaned_data["date_sold"] and self.cleaned_data["date_acquired"] > self.cleaned_data["date_sold"]:
|
|
||||||
raise ValidationError("Cannot sell an item before it is acquired")
|
|
||||||
return self.cleaned_data["date_sold"]
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Asset
|
model = models.Asset
|
||||||
@@ -19,10 +15,61 @@ class AssetForm(forms.ModelForm):
|
|||||||
'is_cable' : forms.CheckboxInput()
|
'is_cable' : forms.CheckboxInput()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def clean_date_sold(self):
|
||||||
|
if self.cleaned_data["date_sold"] and self.cleaned_data["date_acquired"] > self.cleaned_data["date_sold"]:
|
||||||
|
raise ValidationError("Cannot sell an item before it is acquired")
|
||||||
|
return self.cleaned_data["date_sold"]
|
||||||
|
|
||||||
|
def clean_asset_id(self):
|
||||||
|
# If the asset ID has been changed
|
||||||
|
if self.instance.asset_id and self.instance.asset_id == self.cleaned_data["asset_id"]: #If the item was not changed
|
||||||
|
return self.cleaned_data["asset_id"]
|
||||||
|
else:
|
||||||
|
if re.search("^[a-zA-Z0-9]+$", self.cleaned_data["asset_id"]) == None:
|
||||||
|
raise ValidationError("An Asset ID can only consist of letters and numbers")
|
||||||
|
return self.cleaned_data["asset_id"]
|
||||||
|
|
||||||
|
def clean_purchase_price(self):
|
||||||
|
purchase_price = self.cleaned_data["purchase_price"]
|
||||||
|
if purchase_price and purchase_price < 0:
|
||||||
|
raise ValidationError("A price cannot be negative")
|
||||||
|
return purchase_price
|
||||||
|
|
||||||
|
def clean_salvage_value(self):
|
||||||
|
salvage_value = self.cleaned_data["salvage_value"]
|
||||||
|
if salvage_value and salvage_value < 0:
|
||||||
|
raise ValidationError("A price cannot be negative")
|
||||||
|
return salvage_value
|
||||||
|
|
||||||
|
|
||||||
class CableForm(AssetForm):
|
class CableForm(AssetForm):
|
||||||
class Meta(AssetForm.Meta):
|
class Meta(AssetForm.Meta):
|
||||||
model = models.Cable
|
model = models.Cable
|
||||||
|
|
||||||
|
def clean_length(self):
|
||||||
|
length = self.cleaned_data["length"]
|
||||||
|
if length <= 0:
|
||||||
|
raise ValidationError("The length of a cable must be more than 0")
|
||||||
|
return length
|
||||||
|
|
||||||
|
def clean_csa(self):
|
||||||
|
csa = self.cleaned_data["csa"]
|
||||||
|
if csa <= 0:
|
||||||
|
raise ValidationError("The CSA of a cable must be more than 0")
|
||||||
|
return csa
|
||||||
|
|
||||||
|
def clean_circuits(self):
|
||||||
|
circuits = self.cleaned_data["circuits"]
|
||||||
|
if circuits <= 0:
|
||||||
|
raise ValidationError("There must be at least one circuit")
|
||||||
|
return circuits
|
||||||
|
|
||||||
|
def clean_cores(self):
|
||||||
|
cores = self.cleaned_data["cores"]
|
||||||
|
if cores <= 0:
|
||||||
|
raise ValidationError("There must be at least one core")
|
||||||
|
return cores
|
||||||
|
|
||||||
class SupplierForm(forms.Form):
|
class SupplierForm(forms.Form):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Supplier
|
model = models.Supplier
|
||||||
|
|||||||
18
assets/migrations/0009_auto_20191008_2148.py
Normal file
18
assets/migrations/0009_auto_20191008_2148.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 2.0.13 on 2019-10-08 20:48
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('assets', '0008_auto_20191002_1931'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='asset',
|
||||||
|
name='asset_id',
|
||||||
|
field=models.CharField(max_length=10, unique=True),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -40,7 +40,7 @@ class Supplier(models.Model):
|
|||||||
class Asset(PolymorphicModel):
|
class Asset(PolymorphicModel):
|
||||||
|
|
||||||
parent = models.ForeignKey(to='self', related_name='asset_parent', blank=True, null=True, on_delete=models.SET_NULL)
|
parent = models.ForeignKey(to='self', related_name='asset_parent', blank=True, null=True, on_delete=models.SET_NULL)
|
||||||
asset_id = models.CharField(max_length=10)
|
asset_id = models.CharField(max_length=10, unique=True)
|
||||||
description = models.CharField(max_length=120)
|
description = models.CharField(max_length=120)
|
||||||
category = models.ForeignKey(to=AssetCategory, on_delete=models.CASCADE)
|
category = models.ForeignKey(to=AssetCategory, on_delete=models.CASCADE)
|
||||||
status = models.ForeignKey(to=AssetStatus, on_delete=models.CASCADE)
|
status = models.ForeignKey(to=AssetStatus, on_delete=models.CASCADE)
|
||||||
|
|||||||
@@ -74,7 +74,6 @@ class AssetEdit(LoginRequiredMixin, generic.UpdateView):
|
|||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
context = super().get_context_data(**kwargs)
|
context = super().get_context_data(**kwargs)
|
||||||
context['form'] = forms.AssetForm
|
|
||||||
context['edit'] = True
|
context['edit'] = True
|
||||||
|
|
||||||
return context
|
return context
|
||||||
|
|||||||
Reference in New Issue
Block a user