Skip to content

Commit 49e689f

Browse files
committed
Refactor authoritative field handling and enhance device update logic
- Updated `get_source_for_field_update_with_value` to determine source values based on new field values, including handling for empty and unknown values. - Introduced `get_overwrite_sql_clause` to build SQL conditions for authoritative overwrite checks based on plugin settings. - Enhanced `update_devices_data_from_scan` to utilize new authoritative settings and conditions for updating device fields. - Added new tests for source value determination and device creation to ensure proper handling of source fields. - Created in-memory SQLite database fixtures for testing device creation and updates.
1 parent 422a048 commit 49e689f

File tree

5 files changed

+932
-144
lines changed

5 files changed

+932
-144
lines changed

front/deviceDetailsEdit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ function toggleFieldLock(mac, fieldName) {
601601
const sourceIndicator = lockBtn.next();
602602
if (sourceIndicator.hasClass("input-group-addon")) {
603603
const sourceValue = shouldLock ? "LOCKED" : "UNKNOWN";
604-
const sourceClass = shouldLock ? "input-group-addon text-danger" : "input-group-addon text-muted";
604+
const sourceClass = shouldLock ? "input-group-addon text-danger" : "input-group-addon pointer text-muted";
605605
sourceIndicator.text(sourceValue);
606606
sourceIndicator.attr("class", sourceClass);
607607
sourceIndicator.attr("title", getString("FieldLock_Source_Label") + sourceValue);

server/db/authoritative_handler.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,21 +118,64 @@ def can_overwrite_field(field_name, current_source, plugin_prefix, plugin_settin
118118
return not current_source or current_source == "NEWDEV"
119119

120120

121-
def get_source_for_field_update(field_name, plugin_prefix, is_user_override=False):
121+
def get_overwrite_sql_clause(field_name, source_column, plugin_settings):
122122
"""
123-
Determine what source value should be set when a field is updated.
123+
Build a SQL condition for authoritative overwrite checks.
124+
125+
Returns a SQL snippet that permits overwrite for the given field
126+
based on SET_ALWAYS/SET_EMPTY and USER/LOCKED protection.
127+
128+
Args:
129+
field_name: The field being updated (e.g., "devName").
130+
source_column: The *Source column name (e.g., "devNameSource").
131+
plugin_settings: dict with "set_always" and "set_empty" lists.
132+
133+
Returns:
134+
str: SQL condition snippet (no leading WHERE).
135+
"""
136+
set_always = plugin_settings.get("set_always", [])
137+
set_empty = plugin_settings.get("set_empty", [])
138+
139+
if field_name in set_always:
140+
return f"COALESCE({source_column}, '') NOT IN ('USER', 'LOCKED')"
141+
142+
if field_name in set_empty or field_name not in set_always:
143+
return f"COALESCE({source_column}, '') IN ('', 'NEWDEV')"
144+
145+
return f"COALESCE({source_column}, '') IN ('', 'NEWDEV')"
146+
147+
148+
def get_source_for_field_update_with_value(
149+
field_name, plugin_prefix, field_value, is_user_override=False
150+
):
151+
"""
152+
Determine the source value for a field update based on the new value.
153+
154+
If the new value is empty or an "unknown" placeholder, return NEWDEV.
155+
Otherwise, fall back to standard source selection rules.
124156
125157
Args:
126158
field_name: The field being updated.
127159
plugin_prefix: The unique prefix of the plugin writing (e.g., "UNIFIAPI").
128-
Ignored if is_user_override is True.
129-
is_user_override: If True, return "USER"; if False, return plugin_prefix.
160+
field_value: The new value being written.
161+
is_user_override: If True, return "USER".
130162
131163
Returns:
132164
str: The source value to set for the *Source field.
133165
"""
134166
if is_user_override:
135167
return "USER"
168+
169+
if field_value is None:
170+
return "NEWDEV"
171+
172+
if isinstance(field_value, str):
173+
stripped = field_value.strip()
174+
if stripped in ("", "null"):
175+
return "NEWDEV"
176+
if stripped.lower() in ("(unknown)", "(name not found)"):
177+
return "NEWDEV"
178+
136179
return plugin_prefix
137180

138181

0 commit comments

Comments
 (0)