Fixed and replaced the incorrect class property declaration with the instance property declaration, and replaced unnecessary pseudo properties with instance properties.

This commit is contained in:
依瑪貓 2020-08-02 01:04:47 +08:00
parent fda8a2c523
commit 1b89fef01c
4 changed files with 236 additions and 282 deletions

View File

@ -51,10 +51,17 @@ class Account(models.Model):
db_column="updatedby",
related_name="updated_accounting_accounts")
def __init__(self, *args, **kwargs):
super(Account, self).__init__(*args, **kwargs)
self.url = None
def __str__(self):
"""Returns the string representation of this account."""
return self.code.__str__() + " " + self.title
class Meta:
db_table = "accounting_accounts"
@property
def title(self):
return get_multi_lingual_attr(self, "title")
@ -63,19 +70,6 @@ class Account(models.Model):
def title(self, value):
set_multi_lingual_attr(self, "title", value)
_url = None
@property
def url(self):
return self._url
@url.setter
def url(self, value):
self._url = value
class Meta:
db_table = "accounting_accounts"
class Transaction(models.Model):
"""An accounting transaction."""
@ -96,7 +90,19 @@ class Transaction(models.Model):
db_column="updatedby",
related_name="updated_accounting_transactions")
_records = None
def __init__(self, *args, **kwargs):
super(Transaction, self).__init__(*args, **kwargs)
self._records = None
self._is_balanced = None
self._has_order_hole = None
def __str__(self):
"""Returns the string representation of this accounting
transaction."""
return self.date.__str__() + " #" + self.ord.__str__()
class Meta:
db_table = "accounting_transactions"
@property
def records(self):
@ -143,8 +149,6 @@ class Transaction(models.Model):
return sum([x.amount for x in self.credit_records
if isinstance(x.amount, int)])
_is_balanced = None
@property
def is_balanced(self):
"""Whether the sum of the amounts of the debit records is the
@ -165,8 +169,6 @@ class Transaction(models.Model):
so that the user can sort their orders. """
return Transaction.objects.filter(date=self.date).count() > 1
_has_order_hole = None
@property
def has_order_hole(self):
"""Whether the order of the transactions on this day is not
@ -228,14 +230,6 @@ class Transaction(models.Model):
return reverse(
"accounting:transactions.show", args=("transfer", self))
def __str__(self):
"""Returns the string representation of this accounting
transaction."""
return self.date.__str__() + " #" + self.ord.__str__()
class Meta:
db_table = "accounting_transactions"
class Record(models.Model):
"""An accounting record."""
@ -262,94 +256,15 @@ class Record(models.Model):
db_column="updatedby",
related_name="updated_accounting_records")
_debit_amount = None
@property
def debit_amount(self):
"""The debit amount of this accounting record"""
if self._debit_amount is not None:
return self._debit_amount
return self.amount if not self.is_credit else None
@debit_amount.setter
def debit_amount(self, value):
self._debit_amount = value
_credit_amount = None
@property
def credit_amount(self):
"""The credit amount of this accounting record"""
if self._credit_amount is not None:
return self._credit_amount
return self.amount if self.is_credit else None
@credit_amount.setter
def credit_amount(self, value):
self._credit_amount = value
_balance = None
@property
def balance(self):
return self._balance
@balance.setter
def balance(self, value):
self._balance = value
_is_balanced = None
@property
def is_balanced(self):
"""Whether the transaction of this record is balanced. """
if self._is_balanced is None:
self._is_balanced = self.transaction.is_balanced
return self._is_balanced
@is_balanced.setter
def is_balanced(self, value):
self._is_balanced = value
_has_order_hole = None
@property
def has_order_hole(self):
"""Whether the order of the transactions on this day is not
1, 2, 3, 4, 5..., and should be reordered. """
if self._has_order_hole is None:
self._has_order_hole = self.transaction.has_order_hole
return self._has_order_hole
@has_order_hole.setter
def has_order_hole(self, value):
self._has_order_hole = value
_is_credit_card_paid = None
@property
def is_credit_card_paid(self):
# TODO: To be done
if self._is_credit_card_paid is None:
self._is_credit_card_paid = True
return self._is_credit_card_paid
@is_credit_card_paid.setter
def is_credit_card_paid(self, value):
self._is_credit_card_paid = value
_is_existing_equipment = None
@property
def is_existing_equipment(self):
# TODO: To be done
if self._is_existing_equipment is None:
self._is_existing_equipment = False
return self._is_existing_equipment
@is_existing_equipment.setter
def is_existing_equipment(self, value):
self._is_existing_equipment = value
def __init__(self, *args, **kwargs):
super(Record, self).__init__(*args, **kwargs)
self._debit_amount = None
self._credit_amount = None
self._balance = None
self._is_balanced = None
self._has_order_hole = None
self._is_credit_card_paid = None
self._is_existing_equipment = None
def __str__(self):
"""Returns the string representation of this accounting
@ -363,6 +278,81 @@ class Record(models.Model):
class Meta:
db_table = "accounting_records"
@property
def debit_amount(self):
"""The debit amount of this accounting record"""
if self._debit_amount is not None:
return self._debit_amount
return self.amount if not self.is_credit else None
@debit_amount.setter
def debit_amount(self, value):
self._debit_amount = value
@property
def credit_amount(self):
"""The credit amount of this accounting record"""
if self._credit_amount is not None:
return self._credit_amount
return self.amount if self.is_credit else None
@credit_amount.setter
def credit_amount(self, value):
self._credit_amount = value
@property
def balance(self):
return self._balance
@balance.setter
def balance(self, value):
self._balance = value
@property
def is_balanced(self):
"""Whether the transaction of this record is balanced. """
if self._is_balanced is None:
self._is_balanced = self.transaction.is_balanced
return self._is_balanced
@is_balanced.setter
def is_balanced(self, value):
self._is_balanced = value
@property
def has_order_hole(self):
"""Whether the order of the transactions on this day is not
1, 2, 3, 4, 5..., and should be reordered. """
if self._has_order_hole is None:
self._has_order_hole = self.transaction.has_order_hole
return self._has_order_hole
@has_order_hole.setter
def has_order_hole(self, value):
self._has_order_hole = value
@property
def is_credit_card_paid(self):
# TODO: To be done
if self._is_credit_card_paid is None:
self._is_credit_card_paid = True
return self._is_credit_card_paid
@is_credit_card_paid.setter
def is_credit_card_paid(self, value):
self._is_credit_card_paid = value
@property
def is_existing_equipment(self):
# TODO: To be done
if self._is_existing_equipment is None:
self._is_existing_equipment = False
return self._is_existing_equipment
@is_existing_equipment.setter
def is_existing_equipment(self, value):
self._is_existing_equipment = value
class RecordSummary(models.Model):
"""A summary record."""
@ -371,7 +361,14 @@ class RecordSummary(models.Model):
debit = models.PositiveIntegerField()
balance = models.IntegerField()
_label = None
def __init__(self, *args, **kwargs):
super(RecordSummary, self).__init__(*args, **kwargs)
self._label = None
self._cumulative_balance = None
class Meta:
db_table = None
managed = False
@property
def label(self):
@ -383,8 +380,6 @@ class RecordSummary(models.Model):
def label(self, value):
self._label = value
_cumulative_balance = None
@property
def cumulative_balance(self):
return self._cumulative_balance
@ -392,7 +387,3 @@ class RecordSummary(models.Model):
@cumulative_balance.setter
def cumulative_balance(self, value):
self._cumulative_balance = value
class Meta:
db_table = None
managed = False

View File

@ -54,9 +54,6 @@ class ReportUrl:
income_statement (str): The URL of the income statement.
balance_sheet (str): The URL of the balance sheet.
"""
_period = None
_cash = None
_ledger = None
def __init__(self, **kwargs):
if "period" in kwargs:
@ -74,37 +71,29 @@ class ReportUrl:
self._ledger = Account.objects.get(
code=settings.ACCOUNTING["DEFAULT_LEDGER_ACCOUNT"])
@property
def cash(self):
return reverse(
"accounting:cash", args=(self._cash, self._period))
@property
def cash_summary(self):
return reverse("accounting:cash-summary", args=(self._cash,))
@property
def ledger(self):
return reverse(
"accounting:ledger", args=(self._ledger, self._period))
@property
def ledger_summary(self):
return reverse("accounting:ledger-summary", args=(self._ledger,))
@property
def journal(self):
return reverse("accounting:journal", args=(self._period,))
@property
def trial_balance(self):
return reverse("accounting:trial-balance", args=(self._period,))
@property
def income_statement(self):
return reverse("accounting:income-statement", args=(self._period,))
@property
def balance_sheet(self):
return reverse("accounting:balance-sheet", args=(self._period,))
@ -118,7 +107,6 @@ class Populator:
Attributes:
user (User): The user in action.
"""
user = None
def __init__(self, user):
self.user = user

View File

@ -77,10 +77,6 @@ class Period:
month_picker_params (str): The month-picker parameters, as a
JSON text string
"""
_period = None
_data_start = None
_data_end = None
def __init__(self, spec=None, data_start=None, data_end=None):
self._period = self.Parser(spec)
self._data_start = data_start
@ -337,13 +333,13 @@ class Period:
error (str): The period specification format error, or
None on success.
"""
spec = None
start = None
end = None
description = None
error = None
def __init__(self, spec):
self.spec = None
self.start = None
self.end = None
self.description = None
self.error = None
if spec is None:
self.set_this_month()
return

View File

@ -36,11 +36,11 @@ def new_pk(cls):
int: The new random ID.
"""
while True:
id = random.randint(100000000, 999999999)
pk = random.randint(100000000, 999999999)
try:
cls.objects.get(pk=id)
cls.objects.get(pk=pk)
except cls.DoesNotExist:
return id
return pk
def strip_form(form):
@ -65,11 +65,6 @@ class Language:
locale (str); The locale name of this language.
is_default (bool): Whether this is the default language.
"""
id = None
db = None
locale = None
is_default = False
def __init__(self, language):
self.id = language
self.db = "_" + language.lower().replace("-", "_")
@ -153,9 +148,6 @@ class UrlBuilder:
base_path (str): the base path
params (list[Param]): The query parameters
"""
base_path = None
params = []
def __init__(self, start_url):
"""Constructs a new URL builder.
@ -165,6 +157,7 @@ class UrlBuilder:
pos = start_url.find("?")
if pos == -1:
self.base_path = start_url
self.params = []
return
self.base_path = start_url[:pos]
self.params = []
@ -240,9 +233,6 @@ class UrlBuilder:
name (str): The parameter name
value (str): The parameter value
"""
name = None
value = None
def __init__(self, name, value):
"""Constructs a new query parameter
@ -270,58 +260,47 @@ class Pagination:
"""The pagination.
Args:
request (HttpRequest): The request
items (list): All the items
page_no (int): The specified page number
page_size (int): The specified number of items per page
is_reversed (bool): Whether we should display the last
page first
request (HttpRequest): The request.
items (list): All the items.
is_reversed (bool): Whether we should display the last page first.
Raises:
PaginationException: With invalid pagination parameters
Attributes:
is_reversed (bool): Whether we should display the last
page first
current_url (str): The current request URL.
is_reversed (bool): Whether we should display the last page first.
page_size (int): The page size.
total_pages (int): The total number of pages available.
is_paged (bool): Whether there are more than one page.
page_no (int): The current page number.
items (list[Model]): The items in the current page.
links (list[Link]): The navigation links in the pagination
bar.
page_size_options(list[PageSizeOptions]): The page size
options
"""
_current_url = None
is_reversed = False
page_size = None
total_pages = None
is_paged = None
page_no = None
items = None
DEFAULT_PAGE_SIZE = 10
def __init__(self, request, items, is_reversed=False):
current_url = request.get_full_path()
self._current_url = current_url
self.current_url = request.get_full_path()
self.is_reversed = is_reversed
self.page_size = self.DEFAULT_PAGE_SIZE
self.total_pages = None
self.is_paged = None
self.page_no = 1
self.items = []
# The page size
try:
self.page_size = int(request.GET["page-size"])
if self.page_size == self.DEFAULT_PAGE_SIZE:
raise PaginationException(str(
UrlBuilder(current_url).remove("page-size")))
UrlBuilder(self.current_url).remove("page-size")))
if self.page_size < 1:
raise PaginationException(str(
UrlBuilder(current_url).remove("page-size")))
UrlBuilder(self.current_url).remove("page-size")))
except KeyError:
self.page_size = self.DEFAULT_PAGE_SIZE
except ValueError:
raise PaginationException(str(
UrlBuilder(current_url).remove("page-size")))
UrlBuilder(self.current_url).remove("page-size")))
self.total_pages = int(
(len(items) - 1) / self.page_size) + 1
default_page_no = 1 if not is_reversed else self.total_pages
@ -332,21 +311,21 @@ class Pagination:
self.page_no = int(request.GET["page"])
if not self.is_paged:
raise PaginationException(str(
UrlBuilder(current_url).remove("page")))
UrlBuilder(self.current_url).remove("page")))
if self.page_no == default_page_no:
raise PaginationException(str(
UrlBuilder(current_url).remove("page")))
UrlBuilder(self.current_url).remove("page")))
if self.page_no < 1:
raise PaginationException(str(
UrlBuilder(current_url).remove("page")))
UrlBuilder(self.current_url).remove("page")))
if self.page_no > self.total_pages:
raise PaginationException(str(
UrlBuilder(current_url).remove("page")))
UrlBuilder(self.current_url).remove("page")))
except KeyError:
self.page_no = default_page_no
except ValueError:
raise PaginationException(str(
UrlBuilder(current_url).remove("page")))
UrlBuilder(self.current_url).remove("page")))
if not self.is_paged:
self.page_no = 1
@ -355,98 +334,98 @@ class Pagination:
start_no = self.page_size * (self.page_no - 1)
self.items = items[start_no:start_no + self.page_size]
_links = None
@property
def links(self):
"""Returns the navigation links of the pagination bar."""
if self._links is None:
base_url = UrlBuilder(self._current_url).remove("page")
self._links = []
# The previous page
link = self.Link()
link.title = pgettext("Pagination|", "Previous")
if self.page_no > 1:
if self.page_no - 1 == 1:
if not self.is_reversed:
link.url = str(base_url)
else:
link.url = str(base_url.clone().add(
"page", "1"))
"""Returns the navigation links of the pagination bar.
Returns:
list[Link]: The navigation links of the pagination bar.
"""
base_url = UrlBuilder(self.current_url).remove("page")
links = []
# The previous page
link = self.Link()
link.title = pgettext("Pagination|", "Previous")
if self.page_no > 1:
if self.page_no - 1 == 1:
if not self.is_reversed:
link.url = str(base_url)
else:
link.url = str(base_url.clone().add(
"page", str(self.page_no - 1)))
link.is_small_screen = True
self._links.append(link)
# The first page
link = self.Link()
link.title = "1"
if not self.is_reversed:
link.url = str(base_url)
"page", "1"))
else:
link.url = str(base_url.clone().add(
"page", "1"))
if self.page_no == 1:
link.is_active = True
self._links.append(link)
# The previous ellipsis
if self.page_no > 4:
link = self.Link()
if self.page_no > 5:
link.title = pgettext("Pagination|", "...")
else:
link.title = "2"
link.url = str(base_url.clone().add(
"page", "2"))
self._links.append(link)
# The nearby pages
for no in range(self.page_no - 2, self.page_no + 3):
if no <= 1 or no >= self.total_pages:
continue
link = self.Link()
link.title = str(no)
link.url = str(base_url.clone().add(
"page", str(no)))
if no == self.page_no:
link.is_active = True
self._links.append(link)
# The next ellipsis
if self.page_no + 3 < self.total_pages:
link = self.Link()
if self.page_no + 4 < self.total_pages:
link.title = pgettext("Pagination|", "...")
else:
link.title = str(self.total_pages - 1)
link.url = str(base_url.clone().add(
"page", str(self.total_pages - 1)))
self._links.append(link)
# The last page
"page", str(self.page_no - 1)))
link.is_small_screen = True
links.append(link)
# The first page
link = self.Link()
link.title = "1"
if not self.is_reversed:
link.url = str(base_url)
else:
link.url = str(base_url.clone().add(
"page", "1"))
if self.page_no == 1:
link.is_active = True
links.append(link)
# The previous ellipsis
if self.page_no > 4:
link = self.Link()
link.title = str(self.total_pages)
if self.is_reversed:
link.url = str(base_url)
if self.page_no > 5:
link.title = pgettext("Pagination|", "...")
else:
link.title = "2"
link.url = str(base_url.clone().add(
"page", "2"))
links.append(link)
# The nearby pages
for no in range(self.page_no - 2, self.page_no + 3):
if no <= 1 or no >= self.total_pages:
continue
link = self.Link()
link.title = str(no)
link.url = str(base_url.clone().add(
"page", str(no)))
if no == self.page_no:
link.is_active = True
links.append(link)
# The next ellipsis
if self.page_no + 3 < self.total_pages:
link = self.Link()
if self.page_no + 4 < self.total_pages:
link.title = pgettext("Pagination|", "...")
else:
link.title = str(self.total_pages - 1)
link.url = str(base_url.clone().add(
"page", str(self.total_pages - 1)))
links.append(link)
# The last page
link = self.Link()
link.title = str(self.total_pages)
if self.is_reversed:
link.url = str(base_url)
else:
link.url = str(base_url.clone().add(
"page", str(self.total_pages)))
if self.page_no == self.total_pages:
link.is_active = True
links.append(link)
# The next page
link = self.Link()
link.title = pgettext("Pagination|", "Next")
if self.page_no < self.total_pages:
if self.page_no + 1 == self.total_pages:
if self.is_reversed:
link.url = str(base_url)
else:
link.url = str(base_url.clone().add(
"page", str(self.total_pages)))
else:
link.url = str(base_url.clone().add(
"page", str(self.total_pages)))
if self.page_no == self.total_pages:
link.is_active = True
self._links.append(link)
# The next page
link = self.Link()
link.title = pgettext("Pagination|", "Next")
if self.page_no < self.total_pages:
if self.page_no + 1 == self.total_pages:
if self.is_reversed:
link.url = str(base_url)
else:
link.url = str(base_url.clone().add(
"page", str(self.total_pages)))
else:
link.url = str(base_url.clone().add(
"page", str(self.page_no + 1)))
link.is_small_screen = True
self._links.append(link)
return self._links
"page", str(self.page_no + 1)))
link.is_small_screen = True
links.append(link)
return links
class Link:
"""A navigation link in the pagination bar.
@ -458,14 +437,19 @@ class Pagination:
is_small_screen (bool): Whether this link is for small
screens
"""
url = None
title = None
is_active = False
is_small_screen = False
def __int__(self):
self.url = None
self.title = None
self.is_active = False
self.is_small_screen = False
@property
def page_size_options(self):
base_url = UrlBuilder(self._current_url).remove(
"""Returns the page size options.
Returns:
list[PageSizeOption]: The page size options.
"""
base_url = UrlBuilder(self.current_url).remove(
"page").remove("page-size")
return [self.PageSizeOption(x, self._page_size_url(base_url, x))
for x in [10, 100, 200]]
@ -496,9 +480,6 @@ class Pagination:
size (int): The page size.
url (str): The URL for this page size.
"""
size = None
url = None
def __init__(self, size, url):
self.size = size
self.url = url
@ -513,7 +494,5 @@ class PaginationException(Exception):
Attributes:
url (str): The canonical URL to redirect to.
"""
url = None
def __init__(self, url):
self.url = url