SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit b10b8da1 authored by George Katsikas's avatar George Katsikas :goat:
Browse files

refactor(graphs): :recycle:️ extract explorer minimal URI method to form

parent aeca73db
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,7 @@ __license__ = "AGPL v3"
import io
from django import forms
from django.http import QueryDict
from graphs.graphs.plotkind import PlotKind
from graphs.graphs.plotter import ModelFieldPlotter
......@@ -169,6 +170,11 @@ class PlotOptionsForm(InitialCoalescedForm):
plot_kind_select.choices = plot_kind_choices
super().__init__(*args, **kwargs)
if not isinstance(self.data, QueryDict):
data_dict = self.data.copy()
self.data = QueryDict("", mutable=True)
self.data.update(data_dict)
self.helper = FormHelper()
self.helper.layout = Layout()
......@@ -345,3 +351,22 @@ class PlotOptionsForm(InitialCoalescedForm):
plot_svg = plot_svg.replace("<svg ", '<svg class="w-100 h-auto" ')
return plot_svg
@property
def explorer_minimal_url(self):
"""
Return the URL to the explorer with only the changed form fields.
"""
from urllib.parse import urlencode
from django.urls import reverse_lazy
url = (
reverse_lazy("graphs:explorer")
+ "?"
+ urlencode(
{key: self.data.getlist(key) for key in self.changed_data},
doseq=True,
)
)
return url
......@@ -68,7 +68,7 @@ def graphs(request):
{
"title": options.get("title", "Graph"),
"plot_svg": form.plot_as_svg,
"explore_url": reverse_lazy("graphs:explorer") + "?" + urlencode(options),
"explore_url": form.explorer_minimal_url,
}
for options in premade_graph_options
if (form := PlotOptionsForm(options))
......@@ -119,17 +119,7 @@ class PlotView(View):
# Replace the URL in the browser history with the current plot options
# so that the user can refresh the page without losing the current plot
# and share the URL with others to provide a direct link to the plot
response["HX-Replace-URL"] = (
reverse_lazy("graphs:explorer")
+ "?"
+ urlencode(
{
key: value
for key, value in self.request.GET.items()
if key in self.form.changed_data
}
)
)
response["HX-Replace-URL"] = self.form.explorer_minimal_url
return response
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment