2024-07-05 13:32:07 +00:00
|
|
|
from django import forms
|
2024-08-05 13:07:29 +00:00
|
|
|
from utils.device import create_annotation, create_doc, create_index
|
|
|
|
|
2024-07-05 13:32:07 +00:00
|
|
|
|
2024-07-23 13:37:40 +00:00
|
|
|
DEVICE_TYPES = [
|
|
|
|
("Desktop", "Desktop"),
|
|
|
|
("Laptop", "Laptop"),
|
|
|
|
("Server", "Server"),
|
|
|
|
("GraphicCard", "GraphicCard"),
|
|
|
|
("HardDrive", "HardDrive"),
|
|
|
|
("SolidStateDrive", "SolidStateDrive"),
|
|
|
|
("Motherboard", "Motherboard"),
|
|
|
|
("NetworkAdapter", "NetworkAdapter"),
|
|
|
|
("Processor", "Processor"),
|
|
|
|
("RamModule", "RamModule"),
|
|
|
|
("SoundCard", "SoundCard"),
|
|
|
|
("Display", "Display"),
|
|
|
|
("Battery", "Battery"),
|
|
|
|
("Camera", "Camera"),
|
|
|
|
]
|
2024-07-19 15:40:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DeviceForm(forms.Form):
|
2024-07-23 13:37:40 +00:00
|
|
|
type = forms.ChoiceField(choices = DEVICE_TYPES, required=False)
|
2024-07-30 11:37:08 +00:00
|
|
|
amount = forms.IntegerField(required=False, initial=1)
|
2024-08-05 13:07:29 +00:00
|
|
|
customer_id = forms.CharField(required=False)
|
2024-07-23 13:37:40 +00:00
|
|
|
name = forms.CharField(required=False)
|
|
|
|
value = forms.CharField(required=False)
|
|
|
|
|
|
|
|
|
|
|
|
class BaseDeviceFormSet(forms.BaseFormSet):
|
2024-07-30 11:37:08 +00:00
|
|
|
def clean(self):
|
|
|
|
for x in self.cleaned_data:
|
|
|
|
if x.get("amount"):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2024-07-23 13:37:40 +00:00
|
|
|
def save(self, user, commit=True):
|
|
|
|
self.user = user
|
2024-08-05 13:07:29 +00:00
|
|
|
row = {}
|
2024-07-23 13:37:40 +00:00
|
|
|
for f in self.forms:
|
|
|
|
d = f.cleaned_data
|
|
|
|
if not d:
|
|
|
|
continue
|
2024-08-05 13:07:29 +00:00
|
|
|
|
2024-07-23 13:37:40 +00:00
|
|
|
if d.get("type"):
|
2024-08-05 13:07:29 +00:00
|
|
|
row["type"] = d["type"]
|
2024-07-23 13:37:40 +00:00
|
|
|
if d.get("amount"):
|
2024-08-05 13:07:29 +00:00
|
|
|
row["amount"] = d["amount"]
|
2024-07-23 13:37:40 +00:00
|
|
|
if d.get("name"):
|
2024-08-05 13:07:29 +00:00
|
|
|
row[d["name"]] = d.get("value", '')
|
|
|
|
if d.get("customer_id"):
|
|
|
|
row['CUSTOMER_ID']= d["customer_id"]
|
2024-07-23 13:37:40 +00:00
|
|
|
|
2024-08-05 13:07:29 +00:00
|
|
|
doc = create_doc(row)
|
2024-07-23 13:37:40 +00:00
|
|
|
if not commit:
|
|
|
|
return doc
|
|
|
|
|
2024-10-04 15:32:53 +00:00
|
|
|
create_index(doc, self.user)
|
2024-08-05 13:07:29 +00:00
|
|
|
create_annotation(doc, user, commit=commit)
|
2024-07-23 13:37:40 +00:00
|
|
|
return doc
|
|
|
|
|
2024-07-19 15:40:01 +00:00
|
|
|
|
2024-07-23 13:37:40 +00:00
|
|
|
DeviceFormSet = forms.formset_factory(form=DeviceForm, formset=BaseDeviceFormSet, extra=1)
|
2024-07-19 15:40:01 +00:00
|
|
|
|