|
3 | 3 | from .models import DeviceView
|
4 | 4 | from dcim.models import DeviceType
|
5 | 5 | from utilities.forms.fields import CSVModelChoiceField
|
| 6 | +import re |
6 | 7 |
|
7 | 8 |
|
8 | 9 | class DeviceViewForm(NetBoxModelForm):
|
9 | 10 | class Meta:
|
10 | 11 | model = DeviceView
|
11 | 12 | fields = ("device_type", "grid_template_area")
|
12 | 13 |
|
| 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 | + |
13 | 42 |
|
14 | 43 | class DeviceViewImportForm(NetBoxModelImportForm):
|
15 | 44 | device_type = CSVModelChoiceField(
|
|
0 commit comments