mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-20 15:02:31 +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:
@@ -1,16 +1,12 @@
|
||||
from django import forms
|
||||
from django.core.exceptions import ValidationError
|
||||
import re
|
||||
|
||||
from assets import models
|
||||
|
||||
|
||||
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:
|
||||
model = models.Asset
|
||||
@@ -19,10 +15,61 @@ class AssetForm(forms.ModelForm):
|
||||
'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 Meta(AssetForm.Meta):
|
||||
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 Meta:
|
||||
model = models.Supplier
|
||||
|
||||
Reference in New Issue
Block a user