Replaced the combined localized data models into flexible, separated localized data models and their accompanying localization data models, so that adding new languages works automatically without having to change the data model definitions.
This commit is contained in:
@ -28,24 +28,24 @@ from django.db import models, transaction
|
||||
from django.db.models import Q, Max
|
||||
from django.http import HttpRequest
|
||||
|
||||
from mia_core.models import BaseModel
|
||||
from mia_core.utils import get_multi_lingual_attr, set_multi_lingual_attr
|
||||
from mia_core.models import BaseModel, L10nModel, LocalizedModel
|
||||
|
||||
|
||||
class Account(DirtyFieldsMixin, BaseModel):
|
||||
class Account(DirtyFieldsMixin, LocalizedModel, BaseModel):
|
||||
"""An account."""
|
||||
parent = models.ForeignKey(
|
||||
"self", on_delete=models.PROTECT, null=True,
|
||||
related_name="child_set")
|
||||
code = models.CharField(max_length=5, unique=True)
|
||||
title_zh_hant = models.CharField(max_length=32)
|
||||
title_en = models.CharField(max_length=128, null=True)
|
||||
title_zh_hans = models.CharField(max_length=32, null=True)
|
||||
title_l10n = models.CharField(max_length=32, db_column="title")
|
||||
CASH = "1111"
|
||||
ACCUMULATED_BALANCE = "3351"
|
||||
NET_CHANGE = "3353"
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
if "title" in kwargs:
|
||||
self.title = kwargs["title"]
|
||||
del kwargs["title"]
|
||||
super().__init__(*args, **kwargs)
|
||||
self.url = None
|
||||
self.debit_amount = None
|
||||
@ -69,12 +69,11 @@ class Account(DirtyFieldsMixin, BaseModel):
|
||||
|
||||
@property
|
||||
def title(self) -> str:
|
||||
"""The title in the current language."""
|
||||
return get_multi_lingual_attr(self, "title")
|
||||
return self.get_l10n("title")
|
||||
|
||||
@title.setter
|
||||
def title(self, value: str) -> None:
|
||||
set_multi_lingual_attr(self, "title", value)
|
||||
def title(self, value):
|
||||
self.set_l10n("title", value)
|
||||
|
||||
@property
|
||||
def option_data(self) -> Dict[str, str]:
|
||||
@ -109,6 +108,12 @@ class Account(DirtyFieldsMixin, BaseModel):
|
||||
self._is_in_use = value
|
||||
|
||||
|
||||
class AccountL10n(DirtyFieldsMixin, L10nModel, BaseModel):
|
||||
"""The localization content of an account."""
|
||||
master = models.ForeignKey(
|
||||
Account, on_delete=models.CASCADE, related_name="l10n_set")
|
||||
|
||||
|
||||
class Transaction(DirtyFieldsMixin, BaseModel):
|
||||
"""An accounting transaction."""
|
||||
date = models.DateField()
|
||||
|
@ -155,10 +155,12 @@ class DataFiller:
|
||||
code = str(code)
|
||||
parent = None if len(code) == 1\
|
||||
else Account.objects.get(code=code[:-1])
|
||||
Account(pk=new_pk(Account), parent=parent, code=code,
|
||||
title_zh_hant=data[1], title_en=data[2],
|
||||
title_zh_hans=data[3],
|
||||
created_by=self.user, updated_by=self.user).save()
|
||||
account = Account(parent=parent, code=code)
|
||||
account.current_user = self.user
|
||||
account.set_l10n_in("title", "zh-hant", data[1])
|
||||
account.set_l10n_in("title", "en", data[2])
|
||||
account.set_l10n_in("title", "zh-hans", data[3])
|
||||
account.save()
|
||||
|
||||
def add_transfer_transaction(self, date: Union[datetime.date, int],
|
||||
debit: List[RecordData],
|
||||
|
@ -39,8 +39,7 @@ from django.views.decorators.http import require_GET, require_POST
|
||||
from django.views.generic import ListView, DetailView
|
||||
|
||||
from mia_core.period import Period
|
||||
from mia_core.utils import Pagination, get_multi_lingual_search, \
|
||||
PaginationException
|
||||
from mia_core.utils import Pagination, PaginationException
|
||||
from mia_core.views import DeleteView, FormView, RedirectView
|
||||
from . import utils
|
||||
from .forms import AccountForm, TransactionForm, TransactionSortForm
|
||||
@ -767,7 +766,8 @@ def search(request: HttpRequest) -> HttpResponse:
|
||||
records = []
|
||||
else:
|
||||
records = Record.objects.filter(
|
||||
get_multi_lingual_search("account__title", query)
|
||||
Q(account__title_l10n__icontains=query)
|
||||
| Q(account__l10n_set__value__icontains=query)
|
||||
| Q(account__code__icontains=query)
|
||||
| Q(summary__icontains=query)
|
||||
| Q(transaction__notes__icontains=query))
|
||||
|
Reference in New Issue
Block a user