-
Notifications
You must be signed in to change notification settings - Fork 15
Shrink stored filter widget on incident list #1621
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
Open
hmpf
wants to merge
5
commits into
main
Choose a base branch
from
shrink-stored-filter-widget
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Made the controls for storing filters take up less horizontal space. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -113,10 +113,27 @@ def incident_update(request: HtmxHttpRequest, action: str): | |||||
| return HttpResponseClientRefresh() | ||||||
|
|
||||||
|
|
||||||
| def set_selected_filter(request, filter_obj): | ||||||
| if filter_obj: | ||||||
| request.session["selected_filter_pk"] = str(filter_obj.pk) | ||||||
| request.session["selected_filter_name"] = filter_obj.name | ||||||
| else: | ||||||
| request.session["selected_filter_pk"] = None | ||||||
| request.session.pop("selected_filter_name", None) | ||||||
|
|
||||||
|
|
||||||
| def get_selected_filter(request): | ||||||
| filter_id = request.session.get("selected_filter_pk", None) | ||||||
| if filter_id: | ||||||
| return get_object_or_404(Filter, pk=filter_id, user=request.user) | ||||||
| return None | ||||||
|
Comment on lines
+116
to
+129
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Beautiful helper functions! |
||||||
|
|
||||||
|
|
||||||
| @require_GET | ||||||
| def filter_form(request: HtmxHttpRequest): | ||||||
| set_selected_filter(request, None) | ||||||
| LOG = logging.getLogger(__name__ + ".filter_form") | ||||||
| request.session["selected_filter"] = None | ||||||
| LOG.debug("filter_form calling incident_list_filter") | ||||||
| incident_list_filter = get_filter_function() | ||||||
| filter_form, _ = incident_list_filter(request, None) | ||||||
| context = {"filter_form": filter_form} | ||||||
|
|
@@ -129,13 +146,14 @@ def create_filter(request: HtmxHttpRequest): | |||||
| from argus.htmx.incident.filter import create_named_filter | ||||||
|
|
||||||
| filter_name = request.POST.get("filter_name", None) | ||||||
| LOG.debug("create_filter calling incident_list_filter") | ||||||
| incident_list_filter = get_filter_function() | ||||||
| filter_form, _ = incident_list_filter(request, None) | ||||||
| if filter_name and filter_form.is_valid(): | ||||||
| filterblob = filter_form.to_filterblob() | ||||||
| _, filter_obj = create_named_filter(request, filter_name, filterblob) | ||||||
| if filter_obj: | ||||||
| request.session["selected_filter"] = str(filter_obj.id) | ||||||
| set_selected_filter(request, filter_obj) | ||||||
| return HttpResponseClientRefresh() | ||||||
| messages.error(request, "Failed to create filter") | ||||||
| return HttpResponseBadRequest() | ||||||
|
|
@@ -144,6 +162,7 @@ def create_filter(request: HtmxHttpRequest): | |||||
| @require_POST | ||||||
| def update_filter(request: HtmxHttpRequest, pk: int): | ||||||
| filter_obj = get_object_or_404(Filter, id=pk) | ||||||
| LOG.debug("update_filter calling incident_list_filter") | ||||||
| incident_list_filter = get_filter_function() | ||||||
| filter_form, _ = incident_list_filter(request, None) | ||||||
| if filter_form.is_valid(): | ||||||
|
|
@@ -152,11 +171,12 @@ def update_filter(request: HtmxHttpRequest, pk: int): | |||||
| filter_obj.save() | ||||||
|
|
||||||
| # Immediately select the newly updated filter - keep or not? | ||||||
| # request.session["selected_filter"] = str(filter_obj.id) | ||||||
| # set_selected_filter(request, filter_obj) | ||||||
|
|
||||||
| messages.success(request, f"Updated filter '{filter_obj.name}'.") | ||||||
| return HttpResponseClientRefresh() | ||||||
| messages.error(request, f"Failed to update filter '{filter_obj.name}'.") | ||||||
| errors = f": {filter_form.errors}" if filter_form.errors else "" | ||||||
| messages.error(request, f'Failed to update filter "{filter_obj.name}": {errors}') | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| return HttpResponseBadRequest() | ||||||
|
|
||||||
|
|
||||||
|
|
@@ -166,8 +186,8 @@ def delete_filter(request: HtmxHttpRequest, pk: int): | |||||
| deleted_id = filter_obj.delete() | ||||||
| if deleted_id: | ||||||
| messages.success(request, f"Deleted filter {filter_obj.name}.") | ||||||
| if request.session.get("selected_filter") == str(pk): | ||||||
| request.session["selected_filter"] = None | ||||||
| if request.session.get("selected_filter_pk") == str(pk): | ||||||
| set_selected_filter(request, None) | ||||||
| return HttpResponseClientRefresh() | ||||||
|
|
||||||
|
|
||||||
|
|
@@ -185,22 +205,33 @@ def get_existing_filters(request: HtmxHttpRequest): | |||||
|
|
||||||
| @require_GET | ||||||
| def filter_select(request: HtmxHttpRequest): | ||||||
| LOG = logging.getLogger(__name__ + ".filter_select") | ||||||
| context = {} | ||||||
| template_name = "htmx/incident/_incident_list_filter_incidents.html" | ||||||
|
|
||||||
| LOG.debug("GET when selecting filter: %s", request.GET) | ||||||
| filter_id = request.GET.get("filter", None) | ||||||
| if filter_id and get_object_or_404(Filter, id=filter_id): | ||||||
| request.session["selected_filter"] = filter_id | ||||||
| incident_list_filter = get_filter_function() | ||||||
| filter_form, _ = incident_list_filter(request, None) | ||||||
| context = {"filter_form": filter_form} | ||||||
| return render(request, "htmx/incident/_incident_filterbox.html", context=context) | ||||||
| if filter_id: | ||||||
| use_empty_filter = False | ||||||
| filter_obj = get_object_or_404(Filter, id=filter_id) | ||||||
| set_selected_filter(request, filter_obj) | ||||||
| LOG.debug("Selecting filter: %s", filter_obj.name) | ||||||
| else: | ||||||
| request.session["selected_filter"] = None | ||||||
| if request.htmx.trigger: | ||||||
| incident_list_filter = get_filter_function() | ||||||
| filter_form, _ = incident_list_filter(request, None, use_empty_filter=True) | ||||||
| context = {"filter_form": filter_form} | ||||||
| return render(request, "htmx/incident/_incident_filterbox.html", context=context) | ||||||
| else: | ||||||
| return retarget(HttpResponse(), "#incident-filter-select") | ||||||
| use_empty_filter = True | ||||||
| set_selected_filter(request, None) | ||||||
| LOG.debug("Selecting empty filter") | ||||||
|
|
||||||
| if request.htmx.trigger: | ||||||
| LOG.debug("filter_select calling incident_list_filter") | ||||||
| incident_list_filter = get_filter_function() | ||||||
| LOG.debug("nao 1 %s", request.GET) | ||||||
| filter_form, _ = incident_list_filter(request, None, use_empty_filter=use_empty_filter) | ||||||
| context["filter_form"] = filter_form | ||||||
| LOG.debug("nao 2 %s", request.GET) | ||||||
| return render(request, template_name, context=context) | ||||||
|
|
||||||
| LOG.debug("boo %s", request.GET) | ||||||
| return retarget(HttpResponse(), "#incident-filter-select") | ||||||
|
|
||||||
|
|
||||||
| def dedupe_querydict(querydict: QueryDict): | ||||||
|
|
@@ -219,12 +250,16 @@ def dedupe_querydict(querydict: QueryDict): | |||||
|
|
||||||
| def add_param_to_querydict(querydict: QueryDict, key: str, value: Any): | ||||||
| "Set key to value if missing from querydict" | ||||||
| LOG = logging.getLogger(__name__ + ".add_param_to_querydict") | ||||||
| qd = querydict.copy() | ||||||
| LOG.debug("Current QueryDict: %s, about to alter %s", qd, key) | ||||||
| if value is None: | ||||||
| LOG.debug("Unchanged QueryDict: %s, value is None", querydict) | ||||||
| return querydict | ||||||
| if key not in qd: | ||||||
| if isinstance(value, Iterable): | ||||||
| if not value: | ||||||
| LOG.debug("Unchanged QueryDict: %s, value is falsey Iterable", querydict) | ||||||
| return querydict | ||||||
| if isinstance(value, str): | ||||||
| value = [value] | ||||||
|
|
@@ -234,7 +269,9 @@ def add_param_to_querydict(querydict: QueryDict, key: str, value: Any): | |||||
| value = [str(value)] | ||||||
| qd.setlist(key, value) | ||||||
| qd._mutable = False | ||||||
| LOG.debug("Altered QueryDict: %s", qd) | ||||||
| return qd | ||||||
| LOG.debug("Unchanged QueryDict: %s, key not found", querydict) | ||||||
| return querydict | ||||||
|
|
||||||
|
|
||||||
|
|
@@ -263,6 +300,7 @@ def search_tags(request): | |||||
| @require_GET | ||||||
| def incident_list(request: HtmxHttpRequest) -> HttpResponse: | ||||||
| LOG = logging.getLogger(__name__ + ".incident_list") | ||||||
| LOG.debug("<=====VIEW=====<") | ||||||
| LOG.debug("GET at start: %s", request.GET) | ||||||
| request.GET = dedupe_querydict(request.GET) | ||||||
| LOG.debug("after dedupe: %s", request.GET) | ||||||
|
|
@@ -276,6 +314,11 @@ def incident_list(request: HtmxHttpRequest) -> HttpResponse: | |||||
| total_count = qs.count() | ||||||
| last_refreshed = make_aware(datetime.now()) | ||||||
|
|
||||||
| # Stored filters | ||||||
| existing_filters = Filter.objects.filter(user=request.user) | ||||||
|
|
||||||
| # Get filters storable in Filter.filter | ||||||
| LOG.debug("calling incident_list_filter") | ||||||
| incident_list_filter = get_filter_function() | ||||||
| filter_form, qs = incident_list_filter(request, qs) | ||||||
|
|
||||||
|
|
@@ -327,14 +370,22 @@ def incident_list(request: HtmxHttpRequest) -> HttpResponse: | |||||
|
|
||||||
| LOG.debug("GET at end: %s", request.GET) | ||||||
| context = { | ||||||
| "columns": columns, | ||||||
| "page_title": "Incidents", | ||||||
| "base": base_template, | ||||||
| # filter box | ||||||
| "filter_form": filter_form, | ||||||
| # storing filters | ||||||
| "stored_filters": existing_filters, | ||||||
| # table | ||||||
| "columns": columns, | ||||||
| # refresh info | ||||||
| "refresh_info": refresh_info, | ||||||
| "refresh_info_forms": GET_forms, | ||||||
| "page_title": "Incidents", | ||||||
| "base": base_template, | ||||||
| "filtered_count": filtered_count, | ||||||
| "count": total_count, | ||||||
| "page": page, | ||||||
| "last_page_num": last_page_num, | ||||||
| "second_to_last_page": last_page_num - 1, | ||||||
| } | ||||||
| LOG.debug(">=====VIEW=====>") | ||||||
| return render(request, "htmx/incident/incident_list.html", context=context) | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 0 additions & 4 deletions
4
src/argus/htmx/templates/htmx/incident/_delete_filter_confirm_dialog.html
This file was deleted.
Oops, something went wrong.
16 changes: 13 additions & 3 deletions
16
src/argus/htmx/templates/htmx/incident/_filter_controls.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,16 @@ | ||
| <!-- htmx/incident/_filter_controls.html --> | ||
| <div class="join join-horizontal items-center"> | ||
| {% include "htmx/incident/_filter_select.html" %} | ||
| {% include "htmx/incident/_filter_create_modal.html" with dialog_id="create-filter-dialog" button_title="Create filter" button_class="btn-sm text-xs join-item" header="Create new filter" explanation="Create new filter from currently selected filter parameters" cancel_text="Cancel" submit_text="Submit" %} | ||
| {% include "htmx/incident/_filter_update_dropdown.html" %} | ||
| {% include "htmx/incident/_filter_delete_dropdown.html" %} | ||
| {% include "htmx/incident/_filter_create_modal.html" with dialog_id="create-filter-dialog" button_title="Save new" button_class="btn-sm text-xs join-item" header="Create new filter" explanation="Create new filter from currently selected filter parameters" cancel_text="Cancel" submit_text="Submit" %} | ||
| {% with item_id=request.session.selected_filter_pk item_title=request.session.selected_filter_name item_class="btn btn-sm text-xs join-item rounded-l-none! ml-1" %} | ||
| {% if request.session.selected_filter_pk %} | ||
| {% url 'htmx:filter-update' pk=request.session.selected_filter_pk as update_filter_url %} | ||
| {% include "htmx/incident/_filter_controls_confirm_dialog.html" with filter_url=update_filter_url modal_button_name="Update" dialog_id="filter-update-confirm" action="Update filter" confirmation_message="Are you sure you want to override this filter?" %} | ||
| {% url 'htmx:filter-delete' pk=request.session.selected_filter_pk as delete_filter_url %} | ||
| {% include "htmx/incident/_filter_controls_confirm_dialog.html" with filter_url=delete_filter_url modal_button_name="Delete" dialog_id="filter-delete-confirm" action="Delete filter" confirmation_message="Are you sure you want to delete this filter?" %} | ||
| {% else %} | ||
| <button class="{{ item_class }}" disabled>Update</button> | ||
| <button class="{{ item_class }}" disabled>Delete</button> | ||
| {% endif %} | ||
| {% endwith %} | ||
| </div> |
3 changes: 2 additions & 1 deletion
3
...cident/_update_filter_confirm_dialog.html → ...dent/_filter_controls_confirm_dialog.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| <!-- htmx/incident/_filter_alter_confirm_dialog.html --> | ||
| {% extends "htmx/_base_confirm_dialog.html" %} | ||
| {% block confirm_action_control %} | ||
| hx-post="{% url 'htmx:filter-update' pk=filter.id %}" | ||
| hx-post="{{ filter_url }}" | ||
| hx-include="#incident-filter-box fieldset" | ||
| {% endblock confirm_action_control %} |
1 change: 1 addition & 0 deletions
1
src/argus/htmx/templates/htmx/incident/_filter_create_modal.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
src/argus/htmx/templates/htmx/incident/_filter_delete_dropdown.html
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we're at it, we can even add a type hint: