Skip to content

Commit 8f7f7ea

Browse files
committed
BE: Trigger failing if object non-existent + emoji removal in upgrade message
Signed-off-by: jokob-sk <jokob.sk@gmail.com>
1 parent 48e587f commit 8f7f7ea

3 files changed

Lines changed: 83 additions & 55 deletions

File tree

server/initialise.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,13 +724,13 @@ def importConfigs(pm, db, all_plugins):
724724

725725
write_notification(
726726
f"""[Upgrade]: App upgraded from <code>{prev_version}</code> to \
727-
<code>{new_version}</code> 🚀 Please clear the cache: \
727+
<code>{new_version}</code> <i class="fa-solid fa-rocket"></i> Please clear the cache: \
728728
<ol> <li>Click OK below</li> \
729729
<li>Clear the browser cache (shift + browser refresh button)</li> \
730730
<li> Clear app cache with the <i class="fa-solid fa-rotate"></i> (reload) button in the header</li>\
731731
<li>Go to Settings and click Save</li> </ol>\
732732
Check out new features and what has changed in the \
733-
<a href="https://github.com/netalertx/NetAlertX/releases" target="_blank">📓 release notes</a>.""",
733+
<a href="https://github.com/netalertx/NetAlertX/releases" target="_blank"><i class="fa-solid fa-file-pen"></i> release notes</a>.""",
734734
'interrupt',
735735
timeNowUTC()
736736
)

server/workflows/actions.py

Lines changed: 76 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,60 @@ class Action:
1414
def __init__(self, trigger):
1515
self.trigger = trigger
1616

17-
def execute(self, obj):
18-
"""Executes the action on the given object."""
17+
def get_object(self):
18+
"""Safely get and normalize the trigger object."""
19+
obj = getattr(self.trigger, "object", None)
20+
21+
if isinstance(obj, sqlite3.Row):
22+
obj = dict(obj)
23+
24+
return obj
25+
26+
def execute(self):
1927
raise NotImplementedError("Subclasses must implement execute()")
2028

2129

2230
class UpdateFieldAction(Action):
2331
"""Action to update a specific field of an object."""
2432

2533
def __init__(self, db, field, value, trigger):
26-
super().__init__(trigger) # Call the base class constructor
34+
super().__init__(trigger)
2735
self.field = field
2836
self.value = value
2937
self.db = db
3038

3139
def execute(self):
3240
mylog("verbose", f"[WF] Updating field '{self.field}' to '{self.value}' for event object {self.trigger.object_type}")
3341

34-
obj = self.trigger.object
35-
36-
# convert to dict for easeir handling
37-
if isinstance(obj, sqlite3.Row):
38-
obj = dict(obj) # Convert Row object to a standard dictionary
42+
obj = self.get_object()
3943

40-
processed = False
44+
if obj is None:
45+
mylog("none", "[WF] Object no longer exists")
46+
return None
4147

42-
# currently unused
4348
if isinstance(obj, dict) and "objectGuid" in obj:
44-
mylog("debug", f"[WF] Updating Object '{obj}' ")
45-
plugin_instance = PluginObjectInstance()
46-
plugin_instance.updateField(obj["objectGuid"], self.field, self.value)
47-
processed = True
49+
mylog("debug", f"[WF] Updating Object '{obj}'")
50+
51+
PluginObjectInstance().updateField(
52+
obj["objectGuid"],
53+
self.field,
54+
self.value,
55+
)
56+
57+
return obj
58+
59+
if isinstance(obj, dict) and "devGUID" in obj:
60+
mylog("debug", f"[WF] Updating Device '{obj}'")
4861

49-
elif isinstance(obj, dict) and "devGUID" in obj:
50-
mylog("debug", f"[WF] Updating Device '{obj}' ")
51-
device_instance = DeviceInstance()
52-
device_instance.updateField(obj["devGUID"], self.field, self.value)
53-
processed = True
62+
DeviceInstance().updateField(
63+
obj["devGUID"],
64+
self.field,
65+
self.value,
66+
)
5467

55-
if not processed:
56-
mylog("none", f"[WF] Could not process action for object: {obj}")
68+
return obj
69+
70+
mylog("none", f"[WF] Unsupported object format: {obj}")
5771

5872
return obj
5973

@@ -62,67 +76,78 @@ class DeleteObjectAction(Action):
6276
"""Action to delete an object."""
6377

6478
def __init__(self, db, trigger):
65-
super().__init__(trigger) # Call the base class constructor
79+
super().__init__(trigger)
6680
self.db = db
6781

6882
def execute(self):
6983
mylog("verbose", f"[WF] Deleting event object {self.trigger.object_type}")
7084

71-
obj = self.trigger.object
72-
73-
# convert to dict for easeir handling
74-
if isinstance(obj, sqlite3.Row):
75-
obj = dict(obj) # Convert Row object to a standard dictionary
85+
obj = self.get_object()
7686

77-
processed = False
87+
if obj is None:
88+
mylog("none", "[WF] Object no longer exists")
89+
return None
7890

79-
# currently unused
8091
if isinstance(obj, dict) and "objectGuid" in obj:
81-
mylog("debug", f"[WF] Updating Object '{obj}' ")
82-
plugin_instance = PluginObjectInstance()
83-
plugin_instance.delete(obj["objectGuid"])
84-
processed = True
92+
mylog("debug", f"[WF] Deleting Object '{obj}'")
8593

86-
elif isinstance(obj, dict) and "devGUID" in obj:
87-
mylog("debug", f"[WF] Updating Device '{obj}' ")
88-
device_instance = DeviceInstance()
89-
device_instance.delete(obj["devGUID"])
90-
processed = True
94+
PluginObjectInstance().delete(obj["objectGuid"])
9195

92-
if not processed:
93-
mylog("none", f"[WF] Could not process action for object: {obj}")
96+
return obj
97+
98+
if isinstance(obj, dict) and "devGUID" in obj:
99+
mylog("debug", f"[WF] Deleting Device '{obj}'")
100+
101+
DeviceInstance().delete(obj["devGUID"])
102+
103+
return obj
104+
105+
mylog("none", f"[WF] Unsupported object format: {obj}")
94106

95107
return obj
96108

97109

98110
class RunPluginAction(Action):
99111
"""Action to run a specific plugin."""
100112

101-
def __init__(self, plugin_name, params, trigger): # Add trigger
102-
super().__init__(trigger) # Call parent constructor
113+
def __init__(self, plugin_name, params, trigger):
114+
super().__init__(trigger)
103115
self.plugin_name = plugin_name
104116
self.params = params
105117

106118
def execute(self):
107-
obj = self.trigger.object
119+
obj = self.get_object()
120+
121+
if obj is None:
122+
mylog("none", "[WF] Object no longer exists")
123+
return None
124+
125+
mylog("verbose", f"[WF] Executing plugin '{self.plugin_name}' with parameters {self.params} for object {obj}")
126+
127+
# PluginManager.run(self.plugin_name, self.params)
108128

109-
mylog("verbose", f"Executing plugin '{self.plugin_name}' with parameters {self.params} for object {obj}")
110-
# PluginManager.run(self.plugin_name, self.parameters)
111129
return obj
112130

113131

114132
class SendNotificationAction(Action):
115133
"""Action to send a notification."""
116134

117135
def __init__(self, method, message, trigger):
118-
super().__init__(trigger) # Call parent constructor
119-
self.method = method # Fix attribute name
136+
super().__init__(trigger)
137+
self.method = method
120138
self.message = message
121139

122140
def execute(self):
123-
obj = self.trigger.object
124-
mylog("verbose", f"Sending notification via '{self.method}': {self.message} for object {obj}")
141+
obj = self.get_object()
142+
143+
if obj is None:
144+
mylog("none", "[WF] Object no longer exists")
145+
return None
146+
147+
mylog("verbose", f"[WF] Sending notification via '{self.method}': {self.message} for object {obj}")
148+
125149
# NotificationManager.send(self.method, self.message)
150+
126151
return obj
127152

128153

@@ -132,7 +157,6 @@ class ActionGroup:
132157
def __init__(self, actions):
133158
self.actions = actions
134159

135-
def execute(self, obj):
160+
def execute(self):
136161
for action in self.actions:
137-
action.execute(obj)
138-
return obj
162+
action.execute()

server/workflows/triggers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ def __init__(self, triggerJson, event, db):
4848
mylog("debug", [query])
4949

5050
result = db.sql.execute(query).fetchall()
51-
self.object = result[0]
51+
52+
if len(result) > 0:
53+
self.object = result[0]
54+
else:
55+
self.object = None
5256
else:
5357
self.object = None
5458

0 commit comments

Comments
 (0)