Skip to content

Commit ba2906b

Browse files
committed
Add cleaning method for grid_template_area to standardize whitespace - Fixes #43
1 parent e3ad567 commit ba2906b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

netbox_device_view/forms.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,42 @@
33
from .models import DeviceView
44
from dcim.models import DeviceType
55
from utilities.forms.fields import CSVModelChoiceField
6+
import re
67

78

89
class DeviceViewForm(NetBoxModelForm):
910
class Meta:
1011
model = DeviceView
1112
fields = ("device_type", "grid_template_area")
1213

14+
def clean_grid_template_area(self):
15+
"""
16+
Cleans the grid_template_area field input, replacing common
17+
non-standard whitespace characters with standard spaces.
18+
"""
19+
# Get the data from the cleaned_data dictionary
20+
grid_string = self.cleaned_data["grid_template_area"]
21+
22+
if grid_string: # Ensure the string is not empty before processing
23+
# Replace Non-Breaking Spaces (U+00A0) with standard spaces
24+
cleaned_string = grid_string.replace("\u00a0", " ")
25+
26+
# Add replacements for other problematic unicode whitespaces.
27+
cleaned_string = cleaned_string.replace("\u2005", " ") # Four-per-em space
28+
29+
# Optional: You could add further standardization here, e.g.,
30+
# replacing any sequence of whitespace with a single space:
31+
# cleaned_string = re.sub(r'\s+', ' ', cleaned_string).strip()
32+
# But be cautious this doesn't alter intended structure if internal
33+
# newlines/spacing within quotes were significant in the design.
34+
# Simple replacement of specific characters is safer if unsure.
35+
36+
else:
37+
cleaned_string = grid_string # Keep empty string if input was empty
38+
39+
# Return the cleaned value
40+
return cleaned_string
41+
1342

1443
class DeviceViewImportForm(NetBoxModelImportForm):
1544
device_type = CSVModelChoiceField(

0 commit comments

Comments
 (0)