Replaced the function-based search view with the class-based SearchListView in the accounting application.

This commit is contained in:
依瑪貓 2020-09-04 22:12:17 +08:00
parent b6a5bc3166
commit 8c390df4cd
2 changed files with 57 additions and 62 deletions

View File

@ -73,7 +73,7 @@ urlpatterns = [
path("balance-sheet/<period:period>", path("balance-sheet/<period:period>",
views.balance_sheet, name="balance-sheet"), views.balance_sheet, name="balance-sheet"),
path("search", path("search",
views.search, name="search"), views.SearchListView.as_view(), name="search"),
path("transactions/<txn-type:txn_type>/create", path("transactions/<txn-type:txn_type>/create",
views.TransactionFormView.as_view(), name="transactions.create"), views.TransactionFormView.as_view(), name="transactions.create"),
path("transactions/<txn-type:txn_type>/<txn:txn>", path("transactions/<txn-type:txn_type>/<txn:txn>",

View File

@ -39,7 +39,7 @@ from django.utils import timezone
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django.utils.translation import gettext as _, gettext_noop, gettext from django.utils.translation import gettext as _, gettext_noop, gettext
from django.views.decorators.http import require_GET, require_POST from django.views.decorators.http import require_GET, require_POST
from django.views.generic import ListView, DetailView from django.views.generic import ListView, DetailView, TemplateView
from mia_core.period import Period from mia_core.period import Period
from mia_core.utils import Pagination, PaginationException, add_default_libs, \ from mia_core.utils import Pagination, PaginationException, add_default_libs, \
@ -756,18 +756,13 @@ def balance_sheet(request: HttpRequest, period: Period) -> HttpResponse:
}) })
@require_GET @method_decorator(require_GET, name="dispatch")
def search(request: HttpRequest) -> HttpResponse: class SearchListView(TemplateView):
"""The search. "The search."""
template_name = "accounting/search.html"
Args: def get_context_data(self, **kwargs):
request: The request. query = self.request.GET.get("q")
Returns:
The response.
"""
# The accounting records
query = request.GET.get("q")
if query is None: if query is None:
records = [] records = []
else: else:
@ -811,13 +806,13 @@ def search(request: HttpRequest) -> HttpResponse:
pass pass
records = Record.objects.filter(conditions) records = Record.objects.filter(conditions)
try: try:
pagination = Pagination(request, records, True) pagination = Pagination(self.request, records, True)
except PaginationException as e: except PaginationException as e:
return redirect(e.url) return redirect(e.url)
return render(request, "accounting/search.html", { context = super().get_context_data(**kwargs)
"record_list": pagination.items, context["record_list"] = pagination.items
"pagination": pagination, context["pagination"] = pagination
}) return context
@method_decorator(require_GET, name="dispatch") @method_decorator(require_GET, name="dispatch")