2024-07-05 13:32:07 +00:00
|
|
|
from django import forms
|
2024-11-12 17:04:59 +00:00
|
|
|
from utils.device import create_property, create_doc, create_index
|
2024-10-15 14:58:51 +00:00
|
|
|
from utils.save_snapshots import move_json, save_in_disk
|
2024-08-05 13:07:29 +00:00
|
|
|
|
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-09-26 02:10:41 +00:00
|
|
|
custom_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", '')
|
2024-09-26 02:10:41 +00:00
|
|
|
if d.get("custom_id"):
|
|
|
|
row['CUSTOM_ID']= d["custom_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-15 14:58:51 +00:00
|
|
|
|
2024-10-15 15:56:34 +00:00
|
|
|
path_name = save_in_disk(doc, self.user.institution.name, place="placeholder")
|
2024-10-04 15:32:53 +00:00
|
|
|
create_index(doc, self.user)
|
2024-11-14 00:05:54 +00:00
|
|
|
create_property(doc, user, commit=commit)
|
2024-10-15 15:56:34 +00:00
|
|
|
move_json(path_name, self.user.institution.name, place="placeholder")
|
2024-10-15 14:58:51 +00:00
|
|
|
|
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
|
|
|
|