Moved the path converters from accounting.urls to accounting.converters in the accounting application.

This commit is contained in:
依瑪貓 2020-07-23 01:22:11 +08:00
parent 5c10b30c24
commit 6ae25ddca7
2 changed files with 72 additions and 54 deletions

69
accounting/converters.py Normal file
View File

@ -0,0 +1,69 @@
# The accounting application of the Mia project.
# by imacat <imacat@mail.imacat.idv.tw>, 2020/7/23
# Copyright (c) 2020 imacat.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""The URL converters.
"""
from accounting.models import Transaction
from mia_core.period import Period
class TransactionTypeConverter:
"""The path converter for the transaction types."""
regex = "income|expense|transfer"
def to_python(self, value):
return value
def to_url(self, value):
return value
class PeriodConverter:
"""The path converter for the period."""
regex = ".+"
def to_python(self, value):
"""Returns the period by the period specification.
Args:
value (str): The period specification.
Returns:
Period: The period.
"""
first_txn = Transaction.objects.order_by("date").first()
data_start = first_txn.date if first_txn is not None else None
last_txn = Transaction.objects.order_by("-date").first()
data_end = last_txn.date if last_txn is not None else None
period = Period(value, data_start, data_end)
if period.error is not None:
raise ValueError
return period
def to_url(self, value):
"""Returns the specification of a period.
Args:
value (Period|str): The period.
Returns:
str: The period specification.
"""
if isinstance(value, Period):
return value.spec
return value

View File

@ -21,64 +21,13 @@
from django.urls import path, register_converter from django.urls import path, register_converter
from mia_core.period import Period
from . import views
from mia_core import views as mia_core_views from mia_core import views as mia_core_views
from .models import Transaction from . import views, converters
from .views import reports from .views import reports
register_converter(converters.TransactionTypeConverter, "txn-type")
class TransactionTypeConverter: register_converter(converters.PeriodConverter, "period")
"""The path converter for the transaction types."""
regex = "income|expense|transfer"
def to_python(self, value):
return value
def to_url(self, value):
return value
register_converter(TransactionTypeConverter, "txn-type")
class PeriodConverter:
"""The path converter for the period."""
regex = ".+"
def to_python(self, value):
"""Returns the period by the period specification.
Args:
value (str): The period specification.
Returns:
Period: The period.
"""
first_txn = Transaction.objects.order_by("date").first()
data_start = first_txn.date if first_txn is not None else None
last_txn = Transaction.objects.order_by("-date").first()
data_end = last_txn.date if last_txn is not None else None
period = Period(value, data_start, data_end)
if period.error is not None:
raise ValueError
return period
def to_url(self, value):
"""Returns the specification of a period.
Args:
value (Period|str): The period.
Returns:
str: The period specification.
"""
if isinstance(value, Period):
return value.spec
return value
register_converter(PeriodConverter, "period")
app_name = "accounting" app_name = "accounting"
urlpatterns = [ urlpatterns = [