2021-10-31 20:57:28 +01:00
|
|
|
from django import forms
|
2021-11-07 20:36:12 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
2021-11-01 22:17:20 +01:00
|
|
|
from parts import models as parts_models
|
2021-11-12 20:14:02 +01:00
|
|
|
from shimatta_modules.EngineeringNumberConverter import EngineeringNumberConverter
|
2021-10-31 20:57:28 +01:00
|
|
|
|
|
|
|
class MyTestForm(forms.Form):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class AddSubStorageForm(forms.Form):
|
|
|
|
storage_name = forms.CharField(label="storage_name", initial='')
|
|
|
|
responsible = forms.CharField(label='responsible_user')
|
2021-11-07 20:36:12 +01:00
|
|
|
|
|
|
|
class DeleteStockForm(forms.Form):
|
2021-11-01 22:17:20 +01:00
|
|
|
stock_uuid = forms.UUIDField()
|
2021-11-07 20:36:12 +01:00
|
|
|
|
|
|
|
class EditWatermarkForm(forms.Form):
|
|
|
|
stock_uuid = forms.UUIDField()
|
|
|
|
watermark_active = forms.BooleanField(required=False) #If it is false, the webbrowser won't send it at all. Therefore we have to set it to required=False
|
2021-11-01 22:17:20 +01:00
|
|
|
watermark = forms.IntegerField(min_value=0)
|
|
|
|
|
2021-11-07 20:36:12 +01:00
|
|
|
def clean(self):
|
|
|
|
cleaned_data = super().clean()
|
|
|
|
id = cleaned_data.get("stock_uuid")
|
|
|
|
|
|
|
|
if not id:
|
|
|
|
raise ValidationError("No stock UUID given")
|
2021-11-01 22:17:20 +01:00
|
|
|
|
2021-11-07 20:36:12 +01:00
|
|
|
stock = None
|
|
|
|
try:
|
|
|
|
stock = parts_models.Stock.objects.get(id=id)
|
|
|
|
except:
|
|
|
|
raise ValidationError("Stock with uuid %s does not exist" % (id))
|
|
|
|
cleaned_data['stock'] = stock
|
|
|
|
|
|
|
|
return cleaned_data
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
stock = self.cleaned_data['stock']
|
|
|
|
active = self.cleaned_data['watermark_active']
|
|
|
|
watermark = self.cleaned_data['watermark']
|
|
|
|
|
|
|
|
if not active:
|
|
|
|
watermark = -1
|
|
|
|
|
|
|
|
stock.watermark = watermark
|
|
|
|
stock.save()
|
|
|
|
|
|
|
|
class EditStockAmountForm(forms.Form):
|
|
|
|
stock_uuid = forms.UUIDField()
|
|
|
|
amount = forms.IntegerField(min_value=0)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
cleaned_data = super().clean()
|
|
|
|
id = cleaned_data.get("stock_uuid")
|
|
|
|
|
|
|
|
if not id:
|
|
|
|
raise ValidationError("No stock UUID given")
|
|
|
|
|
|
|
|
stock = None
|
|
|
|
try:
|
|
|
|
stock = parts_models.Stock.objects.get(id=id)
|
|
|
|
except:
|
|
|
|
raise ValidationError("Stock with uuid %s does not exist" % (id))
|
|
|
|
cleaned_data['stock'] = stock
|
|
|
|
|
|
|
|
return cleaned_data
|
|
|
|
|
|
|
|
def save(self, increase: bool):
|
|
|
|
stock = self.cleaned_data['stock']
|
|
|
|
amount = self.cleaned_data['amount']
|
|
|
|
|
|
|
|
if not increase:
|
|
|
|
amount = -amount
|
|
|
|
|
2021-11-08 23:11:05 +01:00
|
|
|
return stock.atomic_increment(amount)
|
|
|
|
|
|
|
|
class AddStockForm(forms.Form):
|
|
|
|
watermark_active = forms.BooleanField(required=False)
|
|
|
|
watermark = forms.IntegerField(min_value=0, required=True, initial=0)
|
|
|
|
amount = forms.IntegerField(min_value=0, required=True, initial=1)
|
|
|
|
component_uuid = forms.UUIDField(required=True)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
cleaned_data = super().clean()
|
|
|
|
|
|
|
|
id = cleaned_data.get('component_uuid')
|
|
|
|
if not id:
|
|
|
|
raise ValidationError('No valid component selected!')
|
|
|
|
component = None
|
|
|
|
try:
|
|
|
|
component = parts_models.Component.objects.get(id=id)
|
|
|
|
except:
|
|
|
|
raise ValidationError("Invalid component selected!")
|
|
|
|
cleaned_data['component'] = component
|
|
|
|
|
|
|
|
return cleaned_data
|
|
|
|
|
|
|
|
def save(self, storage):
|
|
|
|
component = self.cleaned_data.get('component')
|
|
|
|
amount = self.cleaned_data.get('amount')
|
|
|
|
watermark = -1
|
|
|
|
|
2021-11-08 23:16:02 +01:00
|
|
|
if self.cleaned_data.get('watermark_active'):
|
2021-11-08 23:11:05 +01:00
|
|
|
watermark = self.cleaned_data.get('watermark')
|
|
|
|
|
|
|
|
new_stock = parts_models.Stock.objects.create(storage=storage, component=component, watermark=watermark, amount=amount)
|
|
|
|
new_stock.save()
|
2021-11-10 19:38:39 +01:00
|
|
|
|
|
|
|
class EditComponentForm(forms.Form):
|
|
|
|
name = forms.CharField(required=True)
|
|
|
|
datasheet_link = forms.CharField(max_length=300, required=False)
|
|
|
|
description = forms.CharField(required=False, widget=forms.Textarea)
|
|
|
|
|
|
|
|
# Look up these fields later. Will be autocompleted in UI
|
2021-11-11 20:51:02 +01:00
|
|
|
manufacturer = forms.CharField(required=False, initial='')
|
|
|
|
component_type = forms.CharField(required=False, label='Component Type', initial='')
|
|
|
|
pref_distri = forms.CharField(required=False, label='Preferred Distributor', initial='')
|
|
|
|
package = forms.CharField(required=False, initial='')
|
2021-11-10 19:38:39 +01:00
|
|
|
|
|
|
|
image = forms.ImageField(required=False)
|
|
|
|
|
|
|
|
def __init__(self, *args, instance=None, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
self.instance = instance
|
|
|
|
if instance:
|
|
|
|
self.fields['name'].initial = instance.name
|
|
|
|
self.fields['datasheet_link'].initial = instance.datasheet_link
|
|
|
|
if instance.component_type:
|
|
|
|
self.fields['component_type'].initial = instance.component_type.class_name
|
|
|
|
self.fields['description'].initial = instance.description
|
|
|
|
if instance.manufacturer:
|
|
|
|
self.fields['manufacturer'].initial = instance.manufacturer.name
|
|
|
|
if instance.package:
|
|
|
|
self.fields['package'].initial = instance.package.name
|
|
|
|
if instance.pref_distri:
|
|
|
|
self.fields['pref_distri'].initial = instance.pref_distri.name
|
|
|
|
self.fields['image'].initial = instance.image
|
|
|
|
|
|
|
|
def clean_component_type(self):
|
|
|
|
data = self.cleaned_data['component_type'].strip()
|
|
|
|
|
|
|
|
if len(data) == 0:
|
|
|
|
self.cleaned_data['component_type_object'] = None
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.cleaned_data['component_type_object'] = parts_models.ComponentType.objects.get(class_name=data)
|
|
|
|
except:
|
|
|
|
raise ValidationError('Invalid Component Type')
|
|
|
|
|
|
|
|
def clean_manufacturer(self):
|
|
|
|
data = self.cleaned_data['manufacturer'].strip()
|
|
|
|
|
|
|
|
if len(data) == 0:
|
|
|
|
self.cleaned_data['manufacturer_object'] = None
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
self.cleaned_data['manufacturer_object'] = parts_models.Manufacturer.objects.get(name=data)
|
|
|
|
except:
|
|
|
|
raise ValidationError('Invalid Manufacturer')
|
|
|
|
|
|
|
|
def clean_pref_distri(self):
|
|
|
|
data = self.cleaned_data['pref_distri'].strip()
|
|
|
|
|
|
|
|
if len(data) == 0:
|
|
|
|
self.cleaned_data['pref_distri_object'] = None
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
self.cleaned_data['pref_distri_object'] = parts_models.Distributor.objects.get(name=data)
|
|
|
|
except:
|
|
|
|
raise ValidationError('Invalid Distributor')
|
|
|
|
|
|
|
|
def clean_package(self):
|
|
|
|
data = self.cleaned_data['package'].strip()
|
|
|
|
|
|
|
|
if len(data) == 0:
|
|
|
|
self.cleaned_data['package_object'] = None
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
self.cleaned_data['package_object'] = parts_models.Package.objects.get(name=data)
|
|
|
|
except:
|
|
|
|
raise ValidationError('Invalid Package')
|
|
|
|
|
|
|
|
|
2021-11-13 21:05:52 +01:00
|
|
|
def _save_new(self):
|
|
|
|
self.instance = parts_models.Component.objects.create(
|
|
|
|
name=self.cleaned_data['name'],
|
|
|
|
datasheet_link=self.cleaned_data['datasheet_link'],
|
|
|
|
description=self.cleaned_data['description'],
|
|
|
|
package=self.cleaned_data['package_object'],
|
|
|
|
component_type=self.cleaned_data['component_type_object'],
|
|
|
|
pref_distri=self.cleaned_data['pref_distri_object'],
|
|
|
|
manufacturer=self.cleaned_data['manufacturer_object']
|
|
|
|
)
|
|
|
|
|
|
|
|
if bool(self.cleaned_data['image']):
|
|
|
|
self.instance.image = self.cleaned_data['image']
|
|
|
|
self.instance.save()
|
|
|
|
|
2021-11-10 19:38:39 +01:00
|
|
|
def save(self):
|
|
|
|
if self.instance is None:
|
2021-11-13 21:05:52 +01:00
|
|
|
self._save_new()
|
|
|
|
return
|
2021-11-10 19:38:39 +01:00
|
|
|
|
|
|
|
self.instance.name = self.cleaned_data['name']
|
|
|
|
self.instance.datasheet_link = self.cleaned_data['datasheet_link']
|
|
|
|
self.instance.description = self.cleaned_data['description']
|
|
|
|
|
|
|
|
if bool(self.cleaned_data['image']) is False:
|
|
|
|
self.instance.image = None
|
|
|
|
else:
|
|
|
|
self.instance.image = self.cleaned_data['image']
|
|
|
|
|
|
|
|
self.instance.manufacturer = self.cleaned_data['manufacturer_object']
|
|
|
|
self.instance.pref_distri = self.cleaned_data['pref_distri_object']
|
|
|
|
self.instance.package = self.cleaned_data['package_object']
|
|
|
|
self.instance.component_type = self.cleaned_data['component_type_object']
|
|
|
|
self.instance.save()
|
2021-11-12 20:14:02 +01:00
|
|
|
|
|
|
|
class EditComponentParameterForm(forms.Form):
|
2021-11-13 21:05:52 +01:00
|
|
|
parameter_type = forms.CharField(initial='') # This must come first. Do not change the order of these elements!
|
|
|
|
value = forms.CharField(initial='')
|
2021-11-12 20:14:02 +01:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
|
|
init_values = kwargs.get('initial', None)
|
|
|
|
if init_values is not None:
|
|
|
|
if isinstance(init_values['parameter_type'], parts_models.ComponentParameterType):
|
|
|
|
type_instance = init_values['parameter_type']
|
|
|
|
self.parameter_type_object = type_instance
|
|
|
|
kwargs['initial']['parameter_type'] = init_values['parameter_type'].parameter_name
|
|
|
|
if isinstance(init_values['value'], int) or isinstance(init_values['value'], float):
|
2021-11-13 21:05:52 +01:00
|
|
|
if type_instance.parameter_type == 'E':
|
2021-11-12 20:14:02 +01:00
|
|
|
(num, prefix) = EngineeringNumberConverter.number_to_engineering(init_values['value'], False)
|
2021-11-13 21:05:52 +01:00
|
|
|
kwargs['initial']['value'] = f'{num}{prefix}'
|
|
|
|
elif type_instance.parameter_type == 'I':
|
2021-11-12 20:14:02 +01:00
|
|
|
(num, prefix) = EngineeringNumberConverter.number_to_engineering(init_values['value'], True)
|
2021-11-13 21:05:52 +01:00
|
|
|
kwargs['initial']['value'] = f'{num}{prefix}'
|
2021-11-12 20:14:02 +01:00
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def clean_parameter_type(self):
|
|
|
|
data = self.cleaned_data['parameter_type']
|
|
|
|
|
|
|
|
param_instance = None
|
|
|
|
try:
|
|
|
|
param_instance = parts_models.ComponentParameterType.objects.get(parameter_name=data)
|
|
|
|
except:
|
|
|
|
raise ValidationError(f'Component Parameter Type {data} is not defined')
|
|
|
|
|
|
|
|
self.cleaned_data['parameter_type_object'] = param_instance
|
|
|
|
return data
|
|
|
|
|
|
|
|
def clean_value(self):
|
|
|
|
parameter_type = self.cleaned_data.get('parameter_type_object', None)
|
|
|
|
value_data = self.cleaned_data['value']
|
|
|
|
|
|
|
|
if parameter_type is None:
|
|
|
|
raise ValidationError('Cannot convert value for unknown parameter type')
|
|
|
|
|
|
|
|
processed_value = None
|
|
|
|
|
2021-11-13 21:05:52 +01:00
|
|
|
if parameter_type.parameter_type == 'E' or parameter_type.parameter_type == 'I':
|
2021-11-12 20:14:02 +01:00
|
|
|
try:
|
|
|
|
processed_value = EngineeringNumberConverter.engineering_to_number(value_data)
|
|
|
|
except:
|
|
|
|
raise ValidationError(f'Cannot not convert Value "{value_data}" to a number')
|
2021-11-13 21:05:52 +01:00
|
|
|
elif parameter_type.parameter_type == 'F':
|
2021-11-12 20:14:02 +01:00
|
|
|
processed_value = value_data
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
processed_value = float(value_data)
|
|
|
|
except:
|
|
|
|
raise ValidationError(f'"{value_data}" is not a valid number')
|
|
|
|
|
|
|
|
self.cleaned_data['processed_value'] = processed_value
|
|
|
|
|
2021-11-13 21:05:52 +01:00
|
|
|
return value_data
|
|
|
|
|
|
|
|
|
|
|
|
class DistributorNumberForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = parts_models.DistributorNum
|
|
|
|
fields = ['distributor', 'distributor_part_number']
|
|
|
|
|
|
|
|
class DistributorNumberFormSet(forms.BaseModelFormSet):
|
|
|
|
def save(self, component, commit=True):
|
|
|
|
instances = super().save(commit=False)
|
|
|
|
for instance in instances:
|
|
|
|
instance.component = component
|
|
|
|
if commit:
|
|
|
|
instance.save()
|
|
|
|
return instances
|