Replaced the set() method with the kwargs-based query() method in UrlBuilder in the Mia core application.

This commit is contained in:
依瑪貓 2020-08-03 23:12:04 +08:00
parent b749581162
commit 733335c715
4 changed files with 23 additions and 22 deletions

View File

@ -861,7 +861,7 @@ def txn_store(request, txn_type, txn=None):
url = reverse("accounting:transactions.create", args=(txn_type,)) url = reverse("accounting:transactions.create", args=(txn_type,))
else: else:
url = reverse("accounting:transactions.edit", args=(txn_type, txn)) url = reverse("accounting:transactions.edit", args=(txn_type, txn))
url = str(UrlBuilder(url).set("r", request.GET.get("r"))) url = str(UrlBuilder(url).query(r=request.GET.get("r")))
return error_redirect(request, url, post) return error_redirect(request, url, post)
if txn is None: if txn is None:
@ -869,7 +869,7 @@ def txn_store(request, txn_type, txn=None):
fill_txn_from_post(txn_type, txn, post) fill_txn_from_post(txn_type, txn, post)
if not txn.is_dirty(): if not txn.is_dirty():
url = reverse("accounting:transactions.show", args=(txn_type, txn)) url = reverse("accounting:transactions.show", args=(txn_type, txn))
url = str(UrlBuilder(url).set("r", request.GET.get("r"))) url = str(UrlBuilder(url).query(r=request.GET.get("r")))
message = gettext_noop("This transaction was not modified.") message = gettext_noop("This transaction was not modified.")
return success_redirect(request, url, message) return success_redirect(request, url, message)
@ -904,7 +904,7 @@ def txn_store(request, txn_type, txn=None):
for record in txn.records: for record in txn.records:
record.save() record.save()
url = reverse("accounting:transactions.show", args=(txn_type, txn)) url = reverse("accounting:transactions.show", args=(txn_type, txn))
url = str(UrlBuilder(url).set("r", request.GET.get("r"))) url = str(UrlBuilder(url).query(r=request.GET.get("r")))
message = gettext_noop("This transaction was saved successfully.") message = gettext_noop("This transaction was saved successfully.")
return success_redirect(request, url, message) return success_redirect(request, url, message)

View File

@ -38,8 +38,8 @@ def success_redirect(request, url, success):
Returns: Returns:
HttpResponseRedirect: The redirect response. HttpResponseRedirect: The redirect response.
""" """
id = _store(request, {"success": success}) status_id = _store(request, {"success": success})
return HttpResponseRedirect(str(UrlBuilder(url).set("s", id))) return HttpResponseRedirect(str(UrlBuilder(url).query(s=status_id)))
def error_redirect(request, url, form): def error_redirect(request, url, form):
@ -57,8 +57,8 @@ def error_redirect(request, url, form):
Returns: Returns:
HttpResponseRedirect: The redirect response. HttpResponseRedirect: The redirect response.
""" """
id = _store(request, {"form": form}) status_id = _store(request, {"form": form})
return HttpResponseRedirect(str(UrlBuilder(url).set("s", id))) return HttpResponseRedirect(str(UrlBuilder(url).query(s=status_id)))
def retrieve_status(request): def retrieve_status(request):

View File

@ -95,8 +95,7 @@ def url_with_return(context, view_name, *args):
str: The URL. str: The URL.
""" """
url = reverse(view_name, args=args) url = reverse(view_name, args=args)
return_to = context.request.get_full_path() return str(UrlBuilder(url).query(r=context.request.get_full_path()))
return str(UrlBuilder(url).set("r", return_to))
@register.simple_tag(takes_context=True) @register.simple_tag(takes_context=True)
@ -112,7 +111,7 @@ def url_keep_return(context, view_name, *args):
str: The URL. str: The URL.
""" """
url = reverse(view_name, args=args) url = reverse(view_name, args=args)
return str(UrlBuilder(url).set("r", context.request.GET.get("r"))) return str(UrlBuilder(url).query(r=context.request.GET.get("r")))
@register.simple_tag(takes_context=True) @register.simple_tag(takes_context=True)

View File

@ -197,19 +197,21 @@ class UrlBuilder:
self.params = [x for x in self.params if x.name != name] self.params = [x for x in self.params if x.name != name]
return self return self
def set(self, name, value): def query(self, **kwargs):
"""Sets a query parameter. The current parameters with the """A keyword-styled query parameter setter. The existing values are
same name will be replaced. always replaced. Multiple-values are added when the value is a list or
tuple. The existing values are dropped when the value is None.
Args:
name (str): The parameter name
value (str): The parameter value
Returns:
UrlBuilder: The URL builder itself, with the parameter
modified.
""" """
return self.remove(name).add(name, value) for key in kwargs:
self.remove(key)
if isinstance(kwargs[key], list) or isinstance(kwargs[key], tuple):
for value in kwargs[key]:
self.add(key, value)
elif kwargs[key] is None:
pass
else:
self.add(key, kwargs[key])
return self
def clone(self): def clone(self):
"""Returns a copy of this URL builder. """Returns a copy of this URL builder.