Added to search the year, date, and the month/day in the search query, and revised the query on the primary key in the accounting application.

This commit is contained in:
依瑪貓 2020-09-03 23:30:52 +08:00
parent 0adec7557b
commit 00180d28b9

View File

@ -18,6 +18,7 @@
"""The view controllers of the accounting application. """The view controllers of the accounting application.
""" """
import datetime
import json import json
import re import re
from decimal import Decimal from decimal import Decimal
@ -41,7 +42,8 @@ from django.views.decorators.http import require_GET, require_POST
from django.views.generic import ListView, DetailView from django.views.generic import ListView, DetailView
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, \
parse_date
from mia_core.views import DeleteView, FormView, RedirectView from mia_core.views import DeleteView, FormView, RedirectView
from . import utils from . import utils
from .forms import AccountForm, TransactionForm, TransactionSortForm from .forms import AccountForm, TransactionForm, TransactionSortForm
@ -776,12 +778,26 @@ def search(request: HttpRequest) -> HttpResponse:
| Q(code=query)))\ | Q(code=query)))\
| Q(summary__icontains=query)\ | Q(summary__icontains=query)\
| Q(transaction__notes__icontains=query) | Q(transaction__notes__icontains=query)
if re.match("^[0-9]+(?:\\.[0-9]+)$", query): if re.match("^[0-9]+(?:\\.[0-9]+)?$", query):
conditions = conditions | Q(amount=Decimal(query)) conditions = conditions | Q(amount=Decimal(query))
if re.match("^[0-9]+$", query): if re.match("^[0-9]{9}$", query):
conditions = conditions\ conditions = conditions\
| Q(pk=int(query))\ | Q(pk=int(query))\
| Q(transaction__pk=int(query)) | Q(transaction__pk=int(query))
if re.match("^[0-9]{4}$", query):
conditions = conditions\
| Q(transaction__date__year=int(query))
try:
conditions = conditions | Q(transaction__date=parse_date(query))
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) records = Record.objects.filter(conditions)
try: try:
pagination = Pagination(request, records, True) pagination = Pagination(request, records, True)