From 1a1fe4b98ca368e60d331ab0b882f77adb3c5fbe Mon Sep 17 00:00:00 2001 From: Jordi Albert <63541019+jarg1023@users.noreply.github.com> Date: Mon, 5 Sep 2022 12:32:57 +0200 Subject: [PATCH 1/2] #29 | Link IP Address to IT Object in a more centralized way. Now it is controlled from the IT Object. Also added new field to IP Address to display IT Object title to make more clear to which device is related --- msp/msp/doctype/ip_address/ip_address.json | 12 ++++++++++-- msp/msp/doctype/it_object/it_object.py | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/msp/msp/doctype/ip_address/ip_address.json b/msp/msp/doctype/ip_address/ip_address.json index 4b1f6d1..a8692fd 100644 --- a/msp/msp/doctype/ip_address/ip_address.json +++ b/msp/msp/doctype/ip_address/ip_address.json @@ -10,6 +10,7 @@ "title", "ip_address", "protocol", + "it_object_name", "it_object", "ip_network", "it_landscape" @@ -37,7 +38,8 @@ "fieldname": "it_object", "fieldtype": "Link", "label": "IT Object", - "options": "IT Object" + "options": "IT Object", + "read_only": 1 }, { "fieldname": "it_landscape", @@ -52,10 +54,16 @@ "label": "IP Network", "options": "IP Network", "reqd": 1 + }, + { + "fieldname": "it_object_name", + "fieldtype": "Data", + "label": "IT Object Name", + "read_only": 1 } ], "links": [], - "modified": "2022-06-17 11:21:47.850521", + "modified": "2022-09-05 12:03:39.294521", "modified_by": "Administrator", "module": "MSP", "name": "IP Address", diff --git a/msp/msp/doctype/it_object/it_object.py b/msp/msp/doctype/it_object/it_object.py index 7312138..ba6ca1d 100644 --- a/msp/msp/doctype/it_object/it_object.py +++ b/msp/msp/doctype/it_object/it_object.py @@ -8,6 +8,23 @@ import requests from frappe.model.document import Document class ITObject(Document): + def save(self, *args, **kwargs): + super().save(*args, **kwargs) + if not self.main_ip: + return + + ip_address_name_with_it_object = frappe.db.get_value("IP Address", {'it_object': self.name}, ['name']) + if ip_address_name_with_it_object: + current_ip_address_with_it_object_doctype = frappe.get_doc("IP Address", ip_address_name_with_it_object) + current_ip_address_with_it_object_doctype.it_object = None + current_ip_address_with_it_object_doctype.it_object_name = None + current_ip_address_with_it_object_doctype.save() + + ip_address_doctype = frappe.get_doc("IP Address", self.main_ip) + ip_address_doctype.it_object = self.name + ip_address_doctype.it_object_name = self.title + ip_address_doctype.save() + frappe.db.commit() def get_host_status_from_hosts_data(self, hosts_data, msp_settings_doc): From 7a5746fd27981e18a0714eb4b11993a779ac3b71 Mon Sep 17 00:00:00 2001 From: Jordi Albert <63541019+jarg1023@users.noreply.github.com> Date: Tue, 6 Sep 2022 09:32:30 +0200 Subject: [PATCH 2/2] #29 | Create method to set IT Object data in IP Address Doctypes in already existing doctypes --- msp/msp/doctype/it_object/it_object.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/msp/msp/doctype/it_object/it_object.py b/msp/msp/doctype/it_object/it_object.py index ba6ca1d..48594f3 100644 --- a/msp/msp/doctype/it_object/it_object.py +++ b/msp/msp/doctype/it_object/it_object.py @@ -10,6 +10,9 @@ from frappe.model.document import Document class ITObject(Document): def save(self, *args, **kwargs): super().save(*args, **kwargs) + self.set_it_object_data_in_ip_address_doctype() + + def set_it_object_data_in_ip_address_doctype(self): if not self.main_ip: return @@ -92,3 +95,9 @@ class ITObject(Document): 'status': 500, 'response': f'Data could not be fetched from {msp_settings_doc.oitc_url}. Error -> {str(exception)}' } + +def set_it_object_data_in_ip_address_doctype_for_existing_it_objects(): + it_objects = frappe.db.get_all("IT Object", fields=['name']) + for it_object in it_objects: + it_object_doctype = frappe.get_doc("IT Object", it_object['name']) + it_object_doctype.set_it_object_data_in_ip_address_doctype()