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", db_column="updatedby",
related_name="updated_accounting_accounts") related_name="updated_accounting_accounts")
def __init__(self, *args, **kwargs):
super(Account, self).__init__(*args, **kwargs)
self.url = None
def __str__(self): def __str__(self):
"""Returns the string representation of this account.""" """Returns the string representation of this account."""
return self.code.__str__() + " " + self.title return self.code.__str__() + " " + self.title
class Meta:
db_table = "accounting_accounts"
@property @property
def title(self): def title(self):
return get_multi_lingual_attr(self, "title") return get_multi_lingual_attr(self, "title")
@ -63,19 +70,6 @@ class Account(models.Model):
def title(self, value): def title(self, value):
set_multi_lingual_attr(self, "title", 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): class Transaction(models.Model):
"""An accounting transaction.""" """An accounting transaction."""
@ -96,7 +90,19 @@ class Transaction(models.Model):
db_column="updatedby", db_column="updatedby",
related_name="updated_accounting_transactions") 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 @property
def records(self): def records(self):
@ -143,8 +149,6 @@ class Transaction(models.Model):
return sum([x.amount for x in self.credit_records return sum([x.amount for x in self.credit_records
if isinstance(x.amount, int)]) if isinstance(x.amount, int)])
_is_balanced = None
@property @property
def is_balanced(self): def is_balanced(self):
"""Whether the sum of the amounts of the debit records is the """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. """ so that the user can sort their orders. """
return Transaction.objects.filter(date=self.date).count() > 1 return Transaction.objects.filter(date=self.date).count() > 1
_has_order_hole = None
@property @property
def has_order_hole(self): def has_order_hole(self):
"""Whether the order of the transactions on this day is not """Whether the order of the transactions on this day is not
@ -228,14 +230,6 @@ class Transaction(models.Model):
return reverse( return reverse(
"accounting:transactions.show", args=("transfer", self)) "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): class Record(models.Model):
"""An accounting record.""" """An accounting record."""
@ -262,94 +256,15 @@ class Record(models.Model):
db_column="updatedby", db_column="updatedby",
related_name="updated_accounting_records") related_name="updated_accounting_records")
_debit_amount = None def __init__(self, *args, **kwargs):
super(Record, self).__init__(*args, **kwargs)
@property self._debit_amount = None
def debit_amount(self): self._credit_amount = None
"""The debit amount of this accounting record""" self._balance = None
if self._debit_amount is not None: self._is_balanced = None
return self._debit_amount self._has_order_hole = None
return self.amount if not self.is_credit else None self._is_credit_card_paid = None
self._is_existing_equipment = 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 __str__(self): def __str__(self):
"""Returns the string representation of this accounting """Returns the string representation of this accounting
@ -363,6 +278,81 @@ class Record(models.Model):
class Meta: class Meta:
db_table = "accounting_records" 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): class RecordSummary(models.Model):
"""A summary record.""" """A summary record."""
@ -371,7 +361,14 @@ class RecordSummary(models.Model):
debit = models.PositiveIntegerField() debit = models.PositiveIntegerField()
balance = models.IntegerField() 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 @property
def label(self): def label(self):
@ -383,8 +380,6 @@ class RecordSummary(models.Model):
def label(self, value): def label(self, value):
self._label = value self._label = value
_cumulative_balance = None
@property @property
def cumulative_balance(self): def cumulative_balance(self):
return self._cumulative_balance return self._cumulative_balance
@ -392,7 +387,3 @@ class RecordSummary(models.Model):
@cumulative_balance.setter @cumulative_balance.setter
def cumulative_balance(self, value): def cumulative_balance(self, value):
self._cumulative_balance = 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. income_statement (str): The URL of the income statement.
balance_sheet (str): The URL of the balance sheet. balance_sheet (str): The URL of the balance sheet.
""" """
_period = None
_cash = None
_ledger = None
def __init__(self, **kwargs): def __init__(self, **kwargs):
if "period" in kwargs: if "period" in kwargs:
@ -74,37 +71,29 @@ class ReportUrl:
self._ledger = Account.objects.get( self._ledger = Account.objects.get(
code=settings.ACCOUNTING["DEFAULT_LEDGER_ACCOUNT"]) code=settings.ACCOUNTING["DEFAULT_LEDGER_ACCOUNT"])
@property
def cash(self): def cash(self):
return reverse( return reverse(
"accounting:cash", args=(self._cash, self._period)) "accounting:cash", args=(self._cash, self._period))
@property
def cash_summary(self): def cash_summary(self):
return reverse("accounting:cash-summary", args=(self._cash,)) return reverse("accounting:cash-summary", args=(self._cash,))
@property
def ledger(self): def ledger(self):
return reverse( return reverse(
"accounting:ledger", args=(self._ledger, self._period)) "accounting:ledger", args=(self._ledger, self._period))
@property
def ledger_summary(self): def ledger_summary(self):
return reverse("accounting:ledger-summary", args=(self._ledger,)) return reverse("accounting:ledger-summary", args=(self._ledger,))
@property
def journal(self): def journal(self):
return reverse("accounting:journal", args=(self._period,)) return reverse("accounting:journal", args=(self._period,))
@property
def trial_balance(self): def trial_balance(self):
return reverse("accounting:trial-balance", args=(self._period,)) return reverse("accounting:trial-balance", args=(self._period,))
@property
def income_statement(self): def income_statement(self):
return reverse("accounting:income-statement", args=(self._period,)) return reverse("accounting:income-statement", args=(self._period,))
@property
def balance_sheet(self): def balance_sheet(self):
return reverse("accounting:balance-sheet", args=(self._period,)) return reverse("accounting:balance-sheet", args=(self._period,))
@ -118,7 +107,6 @@ class Populator:
Attributes: Attributes:
user (User): The user in action. user (User): The user in action.
""" """
user = None
def __init__(self, user): def __init__(self, user):
self.user = user self.user = user

View File

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

View File

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