Moved the path converters from accounting.urls to accounting.converters in the accounting application.
This commit is contained in:
parent
5c10b30c24
commit
6ae25ddca7
69
accounting/converters.py
Normal file
69
accounting/converters.py
Normal 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
|
@ -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 = [
|
||||||
|
Loading…
Reference in New Issue
Block a user