Skip to content

Commit c65d5b2

Browse files
committed
Merge branch 'fix_test' into 'main'
Clean up debug, fixed filtering for segments features See merge request 701/netbox/cesnet_service_path_plugin!32
2 parents 8a8adf0 + 31792a2 commit c65d5b2

File tree

6 files changed

+49
-31
lines changed

6 files changed

+49
-31
lines changed

cesnet_service_path_plugin/filtersets/segment.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from extras.filters import TagFilter
77
from netbox.filtersets import NetBoxModelFilterSet
88

9+
910
from cesnet_service_path_plugin.models import Segment
1011
from cesnet_service_path_plugin.models.custom_choices import StatusChoices
1112
from cesnet_service_path_plugin.models.segment_types import SegmentTypeChoices
@@ -86,7 +87,7 @@ class SegmentFilterSet(NetBoxModelFilterSet):
8687
)
8788

8889
# Path data filter
89-
has_path_data = django_filters.MultipleChoiceFilter(
90+
has_path_data = django_filters.ChoiceFilter(
9091
choices=[
9192
(True, "Yes"),
9293
(False, "No"),
@@ -95,6 +96,16 @@ class SegmentFilterSet(NetBoxModelFilterSet):
9596
label="Has Path Data",
9697
)
9798

99+
# Type specific data filter
100+
has_type_specific_data = django_filters.ChoiceFilter(
101+
choices=[
102+
(True, "Yes"),
103+
(False, "No"),
104+
],
105+
method="_has_type_specific_data",
106+
label="Has Type Specific Data",
107+
)
108+
98109
# =============================================================================
99110
# TYPE-SPECIFIC FILTERS
100111
# =============================================================================
@@ -218,6 +229,7 @@ class Meta:
218229
"site_b",
219230
"location_b",
220231
"has_path_data",
232+
"has_type_specific_data",
221233
]
222234

223235
def _at_any_site(self, queryset, name, value):
@@ -239,38 +251,33 @@ def _at_any_location(self, queryset, name, value):
239251
def _has_path_data(self, queryset, name, value):
240252
"""
241253
Filter segments based on whether they have path data or not
242-
243-
Args:
244-
value: List of selected values from choices
245-
[True] - show only segments with path data
246-
[False] - show only segments without path data
247-
[True, False] - show all segments (both with and without)
248-
[] - show all segments (nothing selected)
249254
"""
250-
if not value:
255+
if value in (None, "", []):
251256
# Nothing selected, show all segments
252257
return queryset
253258

254-
# Convert string values to boolean (django-filter sometimes passes strings)
255-
bool_values = []
256-
for v in value:
257-
if v is True or v == "True" or v:
258-
bool_values.append(True)
259-
elif v is False or v == "False" or not v:
260-
bool_values.append(False)
259+
has_data = value in [True, "True", "true", "1"]
261260

262-
if True in bool_values and False in bool_values:
263-
# Both selected, show all segments
264-
return queryset
265-
elif True in bool_values:
261+
if has_data:
266262
# Only "Yes" selected, show segments with path data
267263
return queryset.filter(path_geometry__isnull=False)
268-
elif False in bool_values:
264+
else:
269265
# Only "No" selected, show segments without path data
270266
return queryset.filter(path_geometry__isnull=True)
267+
268+
def _has_type_specific_data(self, queryset, name, value):
269+
"""Filter segments by whether they have type-specific data"""
270+
if value == "" or value is None:
271+
return queryset # No filtering
272+
273+
has_data = value in [True, "True", "true", "1"]
274+
275+
if has_data:
276+
# Has data: exclude null and empty dict
277+
return queryset.exclude(Q(type_specific_data__isnull=True) | Q(type_specific_data={}))
271278
else:
272-
# Fallback: show all segments
273-
return queryset
279+
# No data: include null or empty dict
280+
return queryset.filter(Q(type_specific_data__isnull=True) | Q(type_specific_data={}))
274281

275282
def _parse_smart_numeric_value(self, value, field_type="float"):
276283
"""

cesnet_service_path_plugin/forms/segment.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,16 +454,28 @@ class SegmentFilterForm(NetBoxModelFilterSetForm):
454454
label=_("Circuits"),
455455
)
456456

457-
has_path_data = forms.MultipleChoiceField(
457+
has_path_data = forms.ChoiceField(
458458
required=False,
459459
choices=[
460+
("", "Any"),
460461
(True, "Yes"),
461462
(False, "No"),
462463
],
463464
label=_("Has Path Data"),
464465
help_text="Filter segments that have path geometry data",
465466
)
466467

468+
has_type_specific_data = forms.ChoiceField(
469+
required=False,
470+
choices=[
471+
("", "Any"),
472+
(True, "Yes"),
473+
(False, "No"),
474+
],
475+
label=_("Has Type-Specific Data"),
476+
help_text="Filter segments that have type-specific data defined",
477+
)
478+
467479
# =============================================================================
468480
# TYPE-SPECIFIC FILTER FIELDS - SIMPLIFIED (NO SmartNumericField needed)
469481
# =============================================================================
@@ -620,7 +632,9 @@ class SegmentFilterForm(NetBoxModelFilterSetForm):
620632

621633
fieldsets = (
622634
FieldSet("q", "tag", "filter_id", name="General"),
623-
FieldSet("name", "status", "segment_type", "network_label", "has_path_data", name="Basic"),
635+
FieldSet(
636+
"name", "status", "segment_type", "network_label", "has_path_data", "has_type_specific_data", name="Basic"
637+
),
624638
FieldSet(
625639
"provider_id",
626640
"provider_segment_id",

cesnet_service_path_plugin/graphql/filters.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from strawberry_django import FilterLookup
77
from django.db.models import Q
88

9+
import logging
10+
911
from netbox.graphql.filter_mixins import NetBoxModelFilterMixin
1012

1113
if TYPE_CHECKING:
@@ -76,7 +78,6 @@ class SegmentFilter(NetBoxModelFilterMixin):
7678
@strawberry_django.filter_field
7779
def has_path_data(self, value: bool, prefix: str) -> Q:
7880
"""Filter segments based on whether they have path geometry data"""
79-
8081
if value:
8182
# Filter for segments WITH path data
8283
return Q(**{f"{prefix}path_geometry__isnull": False})

cesnet_service_path_plugin/templates/cesnet_service_path_plugin/segment_edit.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{% load i18n %}
33
{% load static %}
44
{% block title %}
5-
TEST
65
<link rel="stylesheet"
76
href="{% static 'cesnet_service_path_plugin/css/segment_form.css' %}">
87
<script src="{% static 'cesnet_service_path_plugin/js/segment_form.js' %}"></script>

cesnet_service_path_plugin/views/segment.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,6 @@ def segment_map_view(request, pk):
159159
# if hasattr(segment, "status") and hasattr(segment, "get_status_display"):
160160
# segment_style["color"] = status_colors.get(segment.get_status_display(), "#dc3545")
161161

162-
print("has path data:", segment.has_path_data())
163-
print("has fallback line:", has_fallback_line)
164-
165162
context = {
166163
"object": segment,
167164
"segment": segment,

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "cesnet_service_path_plugin"
7-
version = "5.1.0"
7+
version = "5.1.1"
88
description = "Adds ability to create, edit and view service paths in the CESNET network."
99
authors = [
1010
{name = "Jan Krupa", email = "[email protected]"},

0 commit comments

Comments
 (0)