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,68 +756,63 @@ 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")
if query is None:
Returns: records = []
The response. else:
""" conditions =\
# The accounting records Q(account__in=Account.objects.filter(
query = request.GET.get("q") Q(title_l10n__icontains=query)
if query is None: | Q(l10n_set__value__icontains=query)
records = [] | Q(code=query)))\
else: | Q(summary__icontains=query)\
conditions =\ | Q(transaction__notes__icontains=query)
Q(account__in=Account.objects.filter( if re.match("^[0-9]+(?:\\.[0-9]+)?$", query):
Q(title_l10n__icontains=query) conditions = conditions | Q(amount=Decimal(query))
| Q(l10n_set__value__icontains=query) if re.match("^[1-9][0-8]{9}$", query):
| Q(code=query)))\ conditions = conditions\
| Q(summary__icontains=query)\ | Q(pk=int(query))\
| Q(transaction__notes__icontains=query) | Q(transaction__pk=int(query))\
if re.match("^[0-9]+(?:\\.[0-9]+)?$", query): | Q(account__pk=int(query))
conditions = conditions | Q(amount=Decimal(query)) try:
if re.match("^[1-9][0-8]{9}$", query): conditions = conditions | Q(transaction__date=parse_date(query))
conditions = conditions\ except ValueError:
| Q(pk=int(query))\ pass
| Q(transaction__pk=int(query))\ try:
| Q(account__pk=int(query)) date = datetime.datetime.strptime(query, "%Y")
conditions = conditions\
| Q(transaction__date__year=date.year)
except ValueError:
pass
try:
date = datetime.datetime.strptime(query, "%Y/%m")
conditions = conditions\
| (Q(transaction__date__year=date.year)
& Q(transaction__date__month=date.month))
except ValueError:
pass
try:
date = datetime.datetime.strptime(query, "%m/%d")
conditions = conditions\
| (Q(transaction__date__month=date.month)
& Q(transaction__date__day=date.day))
except ValueError:
pass
records = Record.objects.filter(conditions)
try: try:
conditions = conditions | Q(transaction__date=parse_date(query)) pagination = Pagination(self.request, records, True)
except ValueError: except PaginationException as e:
pass return redirect(e.url)
try: context = super().get_context_data(**kwargs)
date = datetime.datetime.strptime(query, "%Y") context["record_list"] = pagination.items
conditions = conditions\ context["pagination"] = pagination
| Q(transaction__date__year=date.year) return context
except ValueError:
pass
try:
date = datetime.datetime.strptime(query, "%Y/%m")
conditions = conditions\
| (Q(transaction__date__year=date.year)
& Q(transaction__date__month=date.month))
except ValueError:
pass
try:
date = datetime.datetime.strptime(query, "%m/%d")
conditions = conditions\
| (Q(transaction__date__month=date.month)
& Q(transaction__date__day=date.day))
except ValueError:
pass
records = Record.objects.filter(conditions)
try:
pagination = Pagination(request, records, True)
except PaginationException as e:
return redirect(e.url)
return render(request, "accounting/search.html", {
"record_list": pagination.items,
"pagination": pagination,
})
@method_decorator(require_GET, name="dispatch") @method_decorator(require_GET, name="dispatch")