Skip to content

Commit ef3d3f6

Browse files
committed
- **Refactored model definitions and improved transaction query logic:**
- Reorganized imports across affected files for consistency (`bank_account.py`, `journal_entry.py`, `data_import.py`). - Enhanced `BankAccountModel` by streamlining field definitions and `configure` method logic to reduce redundancy. - Refactored queryset methods in `StagedTransactionModel` and `ImportJobModel` for improved clarity: - Introduced new annotations (`txs_imported_count`, `txs_pending`, `ready_to_match`) for better transaction state tracking. - Refined `is_imported` and `is_pending` methods to handle parent-child relationships and improve state evaluation. - Added additional URL methods to `ImportJobModel` for easier navigation (`get_list_url`, `get_delete_url`, etc.). - Expanded `StagedTransactionModel` with new fields such as `matched_transaction_model`, `matched_transaction`, and `notes` for better transfer handling and description support. - Introduced `ready_to_match` and `is_ready_to_match` for staged transactions to support enhanced matching workflows. ### **Summary** Refactored model structure and enhanced transaction tracking/query logic for better clarity, maintainability, and functionality. Improvements include streamlined method definitions, additional annotations, and new fields for transaction operations. ### **Backwards Compatibility** No breaking changes introduced; existing workflows remain unaffected. Newly added functionality is fully compatible with previous logic.
1 parent dc53b5d commit ef3d3f6

File tree

4 files changed

+458
-178
lines changed

4 files changed

+458
-178
lines changed

django_ledger/models/bank_account.py

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
A bank account usually holds cash, which is a Current Asset. Transactions may be imported using the open financial
77
format specification OFX into a staging area for final disposition into the EntityModel ledger.
88
"""
9+
910
import warnings
1011
from typing import Optional
11-
from uuid import uuid4, UUID
12+
from uuid import UUID, uuid4
1213

1314
from django.contrib.auth import get_user_model
1415
from django.core.exceptions import ValidationError
1516
from django.db import models
16-
from django.db.models import Q, QuerySet, Manager
17+
from django.db.models import Manager, Q, QuerySet
1718
from django.shortcuts import get_object_or_404
1819
from django.utils.translation import gettext_lazy as _
1920

@@ -37,10 +38,7 @@ class BankAccountModelQuerySet(QuerySet):
3738
def for_user(self, user_model) -> 'BankAccountModelQuerySet':
3839
if user_model.is_superuser:
3940
return self
40-
return self.filter(
41-
Q(entity_model__admin=user_model) |
42-
Q(entity_model__managers__in=[user_model])
43-
)
41+
return self.filter(Q(entity_model__admin=user_model) | Q(entity_model__managers__in=[user_model]))
4442

4543
def active(self) -> 'BankAccountModelQuerySet':
4644
"""
@@ -75,7 +73,7 @@ def get_queryset(self) -> BankAccountModelQuerySet:
7573
return BankAccountModelQuerySet(self.model, using=self._db)
7674

7775
@deprecated_entity_slug_behavior
78-
def for_entity(self, entity_model: 'EntityModel | str | UUID' = None, **kwargs) -> BankAccountModelQuerySet:
76+
def for_entity(self, entity_model: 'EntityModel | str | UUID' = None, **kwargs) -> BankAccountModelQuerySet: # noqa: F821
7977
"""
8078
Allows only the authorized user to query the BankAccountModel for a given EntityModel.
8179
This is the recommended initial QuerySet.
@@ -93,7 +91,7 @@ def for_entity(self, entity_model: 'EntityModel | str | UUID' = None, **kwargs)
9391
'user_model parameter is deprecated and will be removed in a future release. '
9492
'Use for_user(user_model).for_entity(entity_model) instead to keep current behavior.',
9593
DeprecationWarning,
96-
stacklevel=2
94+
stacklevel=2,
9795
)
9896
if DJANGO_LEDGER_USE_DEPRECATED_BEHAVIOR:
9997
qs = qs.for_user(kwargs['user_model'])
@@ -140,25 +138,21 @@ class BankAccountModelAbstract(FinancialAccountInfoMixin, CreateUpdateMixIn):
140138

141139
# todo: rename to account_name?...
142140
name = models.CharField(max_length=150, null=True, blank=True)
143-
entity_model = models.ForeignKey('django_ledger.EntityModel',
144-
on_delete=models.CASCADE,
145-
verbose_name=_('Entity Model'))
146-
147-
account_model = models.ForeignKey('django_ledger.AccountModel',
148-
on_delete=models.RESTRICT,
149-
help_text=_(
150-
'Account model be used to map transactions from financial institution'
151-
),
152-
verbose_name=_('Associated Account Model'))
141+
entity_model = models.ForeignKey(
142+
'django_ledger.EntityModel', on_delete=models.CASCADE, verbose_name=_('Entity Model')
143+
)
144+
145+
account_model = models.ForeignKey(
146+
'django_ledger.AccountModel',
147+
on_delete=models.RESTRICT,
148+
help_text=_('Account model be used to map transactions from financial institution'),
149+
verbose_name=_('Associated Account Model'),
150+
)
153151
active = models.BooleanField(default=False)
154152
hidden = models.BooleanField(default=False)
155153
objects = BankAccountModelManager()
156154

157-
def configure(self,
158-
entity_slug,
159-
user_model: Optional[UserModel],
160-
commit: bool = False):
161-
155+
def configure(self, entity_slug, user_model: Optional[UserModel], commit: bool = False):
162156
EntityModel = lazy_loader.get_entity_model()
163157
if isinstance(entity_slug, str):
164158
if not user_model:
@@ -173,10 +167,7 @@ def configure(self,
173167
self.entity_model = entity_model
174168
self.clean()
175169
if commit:
176-
self.save(update_fields=[
177-
'entity_model',
178-
'updated'
179-
])
170+
self.save(update_fields=['entity_model', 'updated'])
180171
return self, entity_model
181172

182173
def is_active(self):
@@ -188,11 +179,11 @@ class Meta:
188179
indexes = [
189180
models.Index(fields=['account_type']),
190181
models.Index(fields=['account_model']),
191-
models.Index(fields=['entity_model'])
182+
models.Index(fields=['entity_model']),
192183
]
193184
unique_together = [
194185
('entity_model', 'account_number'),
195-
('entity_model', 'account_model', 'account_number', 'routing_number')
186+
('entity_model', 'account_model', 'account_number', 'routing_number'),
196187
]
197188

198189
def __str__(self):
@@ -216,21 +207,15 @@ def mark_as_active(self, commit: bool = False, raise_exception: bool = True, **k
216207
raise BankAccountValidationError('Bank Account cannot be activated.')
217208
self.active = True
218209
if commit:
219-
self.save(update_fields=[
220-
'active',
221-
'updated'
222-
])
210+
self.save(update_fields=['active', 'updated'])
223211

224212
def mark_as_inactive(self, commit: bool = False, raise_exception: bool = True, **kwargs):
225213
if not self.can_inactivate():
226214
if raise_exception:
227215
raise BankAccountValidationError('Bank Account cannot be deactivated.')
228216
self.active = False
229217
if commit:
230-
self.save(update_fields=[
231-
'active',
232-
'updated'
233-
])
218+
self.save(update_fields=['active', 'updated'])
234219

235220

236221
class BankAccountModel(BankAccountModelAbstract):

0 commit comments

Comments
 (0)