Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add search for tags #1146

Draft
wants to merge 2 commits into
base: filter-incidents-tags-dropdown
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 57 additions & 8 deletions src/argus/htmx/incident/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from django.views.generic import ListView

from argus.filter import get_filter_backend
from argus.incident.models import SourceSystem
from argus.incident.models import SourceSystem, Tag
from argus.incident.constants import Level
from argus.htmx.widgets import BadgeDropdownMultiSelect
from argus.notificationprofile.models import Filter
from argus.htmx.widgets import BadgeDropdownMultiSelect, SearchDropdownMultiSelect


filter_backend = get_filter_backend()
QuerySetFilter = filter_backend.QuerySetFilter
Expand All @@ -33,7 +34,53 @@ def get_success_url(self):
return reverse("htmx:filter-list")


class IncidentFilterForm(forms.Form):
class TagFieldMixin:
def _init_tag_field(self, *args, **kwargs):
# mollify tests
initial = kwargs.get("initial", None)
if not initial:
initial = {
"tags": [(801, "location=argus")],
}
self.fields["tags"].widget.partial_get = reverse("htmx:search-tags")
query = kwargs.pop("search", None)
self.get_choices(query)

def get_choices(self, query):
# breakpoint()
if not query:
# all_tags = Tag.objects.all()
self.fields["tags"].choices = () # (tag.id, str(tag)) for tag in all_tags[:20])
return

if Tag.TAG_DELIMITER in query:
key, value = Tag.split(query)
tags = Tag.objects.filter(key=key, value__icontains=value)[:20]
else:
tags = Tag.objects.filter(key__icontains=query)[:20]

self.fields["tags"].choices = ((tag.id, str(tag)) for tag in tags)


class TagFilterForm(TagFieldMixin, forms.Form):
tags = forms.MultipleChoiceField(
widget=SearchDropdownMultiSelect(
attrs={
"placeholder": "search tags...",
},
partial_get=None,
),
choices=(), # (tag.id, str(tag)) for tag in Tag.objects.none()),
required=False,
label="Tags",
)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._init_tag_field(*args, **kwargs)


class IncidentFilterForm(TagFieldMixin, forms.Form):
open = forms.BooleanField(required=False)
closed = forms.BooleanField(required=False)
acked = forms.BooleanField(required=False)
Expand All @@ -47,13 +94,14 @@ class IncidentFilterForm(forms.Form):
required=False,
label="Sources",
)
tags = forms.CharField(
widget=forms.TextInput(
tags = forms.MultipleChoiceField(
widget=SearchDropdownMultiSelect(
attrs={
"placeholder": "enter tags...",
"class": "show-selected-box input input-accent input-bordered input-md border overflow-y-auto min-h-8 h-auto max-h-16 max-w-xs leading-tight",
}
"placeholder": "search tags...",
},
partial_get=None,
),
choices=(), # (tag.id, str(tag)) for tag in Tag.objects.all()),
required=False,
label="Tags",
)
Expand All @@ -70,6 +118,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# mollify tests
self.fields["sourceSystemIds"].widget.partial_get = reverse("htmx:incident-filter")
self._init_tag_field(*args, **kwargs)

def _tristate(self, onkey, offkey):
on = self.cleaned_data.get(onkey, None)
Expand Down
1 change: 1 addition & 0 deletions src/argus/htmx/incident/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
path("filter-list/", filter.FilterListView.as_view(), name="filter-list"),
path("select-filter/", views.filter_select, name="select-filter"),
path("filter-create/", views.create_filter, name="filter-create"),
path("search-tags/", views.search_tags, name="search-tags"),
]
27 changes: 27 additions & 0 deletions src/argus/htmx/incident/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,33 @@
return retarget(HttpResponse(), "#incident-filter-select")


class SearchForm(forms.Form):
search = forms.CharField(required=False)


@require_GET
def search_tags(request: HtmxHttpRequest):
search_form = SearchForm(request.GET or None)
search_form.is_valid()
query = search_form.cleaned_data["search"]

form = TagFilterForm(request.GET or None)

Check failure on line 147 in src/argus/htmx/incident/views.py

View workflow job for this annotation

GitHub Actions / Lint Python

Ruff (F821)

src/argus/htmx/incident/views.py:147:12: F821 Undefined name `TagFilterForm`
form.is_valid()

if not query:
return render(
request,
"htmx/forms/selected_choices.html",
{"widget": form.fields["tags"].widget},
)

return render(
request,
"htmx/forms/search_results.html",
{"widget": form.fields["tags"].widget},
)


@require_GET
def incident_list(request: HtmxHttpRequest) -> HttpResponse:
columns = get_incident_table_columns()
Expand Down
Loading
Loading