From 27d317f7f34f9e70c38e735ae64a6a4f7093ce6d Mon Sep 17 00:00:00 2001 From: FischLord Date: Wed, 21 Feb 2024 14:48:31 +0100 Subject: [PATCH] finished edit function --- ip-atlas/crud.py | 54 +++++++++++++++++++++++++--------- ip-atlas/database/ip_atlas.db | Bin 77824 -> 77824 bytes ip-atlas/helper.py | 3 ++ 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/ip-atlas/crud.py b/ip-atlas/crud.py index 9c87496..28823bf 100644 --- a/ip-atlas/crud.py +++ b/ip-atlas/crud.py @@ -134,6 +134,26 @@ def delete_host_by_id(id): delete_from_db("host_tag", f"{id}_{tagID}") print("Host with id: ", id, " deleted") db.session.commit() + +#deletes the portsFB from one host +def delete_portsFB_by_host_id(id): + host = get_host_by_id(id) + portsFB = host["portsFB"] + for portFB in portsFB: + portFBID = check_portFB_exists(portFB, method="id") + print("Deleting portFB with ID:", portFBID) + delete_from_db("portFB", portFBID) + db.session.commit() + +#deletes the tags from one host +def delete_tags_by_host_id(id): + host = get_host_by_id(id) + tags = host["tags"] + for tag in tags: + tagID = check_tag_exists(tag, method="id") + print("Deleting host_tag with ID:", id, tagID) + delete_from_db("host_tag", f"{id}_{tagID}") + db.session.commit() # function which updates the given data in the table def edit_db(table, data): @@ -170,22 +190,28 @@ def write_edit_db(formData): tags = formData.get("tags") print("ID:", id, "Name:", name, "Tags:", tags, "IPv4:", ipv4, "IPv6:", ipv6, "PortsFB:", portsFB) - # if portsFB: - # portsFB = ','.join(portsFB) - # if tags: - # tags = ','.join(tags) - edit_db("host", {"id": id, "name": name, "ipv4": ipv4, "ipv6": ipv6}) - - # host_id = id - # for portFB in portsFB.split(","): - # edit_db("portFB", {"id": portFB, "portFB_number": portFB}) - - # for tag in tags.split(","): - # edit_db("tag", {"id": tag, "tag_name": tag}) - # tag_id = tag - # edit_db("host_tag", {"id": f"{host_id}_{tag_id}", "host_id": host_id, "tag_id": tag_id}) + # firstly delete all ports which are in the db for the host then add the new ones + delete_portsFB_by_host_id(id) + for portFB in portsFB: + print("PortFB:", portFB) + portFBID = check_portFB_exists(portFB, method="id") + if portFBID: + print("Port ", portFB, " gibt es schon") + else: + write_to_db("portFB", {"host_id": id, "portFB_number": portFB}) + # delete all tags which are in the db for the host then add the new ones + delete_tags_by_host_id(id) + for tag in tags: + print("Tag:", tag) + tagID = check_tag_exists(tag, method="id") + if tagID: + write_to_db("host_tag", {"host_id": id, "tag_id": tagID}) + else: + write_to_db("tag", {"tag_name": tag}) + tagID = check_tag_exists(tag, method="id") + write_to_db("host_tag", {"host_id": id, "tag_id": tagID}) db.session.commit() return True except Exception as e: diff --git a/ip-atlas/database/ip_atlas.db b/ip-atlas/database/ip_atlas.db index 42fe5ed3952c82f17c2a1d70f28a364285193cfb..58aaebd23ed9a6bd712df3b346d92641e4dbe03b 100644 GIT binary patch delta 459 zcmXw!&q`ZS5XNWD%>8pNhA+XYh=!^gcVglL)KXX7`T`2k8zhiI+_}jkD87O(pcmYT z3%7!378Save=1ZkG<4ygK)Z0RLC!hMaORsI-`Bp;_Kj{&@Y2rY7hc+5*+cAbM!3US zRw|xg^n(j&yi9Rg->_S$&duj%_wM7g2_Fbgc*sgqSC~m;oChM8$b?_5rR%k{l@O7` zE#VeD(|t!_NXq<VkJlV{v`0(P+l3G!k#FwKj7rAv%N(e&Z5X=%H`CW6Y`) zIE-YN4HgV)s(Ce(cp2k2lFRzdp?2c%MEGa9|5%=DOa~&&E(p{Kdwb&jAoOD{dqme{ zKQR(1=Q4>xWzWv3dhZXuX|X7{C-m_b_qaA+=jQ1MmB2C@FY;DQ-3eoB*lk70+lSOc Nn|Q}rKJae&^D9_SWJ>@5 delta 212 zcmX|(K?;IE7={0726LqN8%Q?>)k!KkLKi5oTqfuNJVN0WxY+7J!9TvpZ(RbWXVhEVYWB=sMh&yC6ISVe9|^Lj!2E zT0Sr{3y;tiuqE`3ie}D14!>9hSW*4LvCRaycCwF?pP&Y;X)&nl;~*tN1K9ixoN+0C YTvzu2gpg9+A~7Po``Z?oWpA_k0JEq$tN;K2 diff --git a/ip-atlas/helper.py b/ip-atlas/helper.py index bd95a37..2dcb1d8 100644 --- a/ip-atlas/helper.py +++ b/ip-atlas/helper.py @@ -52,6 +52,9 @@ def check_ipv4_exists(ipv4 ,method="bool"): + + + # checks if the json document is there def checkJson(): if checkDokument(gPath):