Skip to content

Commit

Permalink
new_compliant_account and new_delinquent_account signals (#18)
Browse files Browse the repository at this point in the history
* new_compliant_account and new_delinquent_account signals

* Credit card deleted delinquent consequences

* Add delete model also to tabular credit card admin

* Delete unused code

* Update version
  • Loading branch information
bertini36 authored May 20, 2021
1 parent 6a212e9 commit f17210e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion billing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '1.7.22'
__version__ = '1.7.23'
__copyright__ = 'Copyright (c) 2020, Skioo SA'
__licence__ = 'MIT'
__URL__ = 'https://github.com/skioo/django-customer-billing'
3 changes: 3 additions & 0 deletions billing/actions/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
Account, CARRIED_FORWARD, CREDIT_REMAINING, Charge, CreditCard,
EventLog, Invoice, ProductProperty, Transaction, total_amount,
)
from ..signals import new_compliant_account, new_delinquent_account

logger = get_logger()

Expand Down Expand Up @@ -271,6 +272,7 @@ def mark_account_as_delinquent(account_id: UUID, reason: str):
type=EventLog.NEW_DELINQUENT,
text=reason,
)
new_delinquent_account.send(sender=mark_account_as_delinquent, account=account)


def mark_account_as_compliant(account_id: UUID, reason: str):
Expand All @@ -284,6 +286,7 @@ def mark_account_as_compliant(account_id: UUID, reason: str):
type=EventLog.NEW_COMPLIANT,
text=reason,
)
new_compliant_account.send(sender=mark_account_as_compliant, account=account)


def charge_pending_invoices(account_id: UUID) -> Dict[str, int]:
Expand Down
6 changes: 6 additions & 0 deletions billing/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Account, Charge, CreditCard, EventLog, Invoice, ProductProperty,
Transaction,
)
from .signals import credit_card_deleted

logger = get_logger()

Expand Down Expand Up @@ -188,6 +189,11 @@ class CreditCardAdmin(AppendOnlyModelAdmin):
raw_id_fields = ['account']
readonly_fields = ['created', 'modified', 'expiry_date']

def delete_model(self, request, obj):
account = obj.account
super().delete_model(request, obj)
credit_card_deleted.send(sender=CreditCardAdmin, account=account)


class CreditCardInline(admin.TabularInline):
model = CreditCard
Expand Down
4 changes: 4 additions & 0 deletions billing/signals/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
invoice_ready = Signal(use_caching=True)

credit_card_registered = Signal()
credit_card_deleted = Signal()

debt_paid = Signal()

new_delinquent_account = Signal()
new_compliant_account = Signal()
17 changes: 15 additions & 2 deletions billing/signals/handlers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.dispatch import receiver
from structlog import get_logger

from . import credit_card_registered
from ..models import CreditCard
from . import credit_card_deleted, credit_card_registered
from ..actions import accounts
from ..models import Account, CreditCard

logger = get_logger()

Expand All @@ -20,3 +20,16 @@ def credit_card_registered_handler(sender, credit_card: CreditCard, **kwargs):
account.id,
reason='A valid credit card has been registered'
)


@receiver(credit_card_deleted)
def credit_card_deleted_handler(sender, account: Account, **kwargs):
if account.delinquent:
return

reasons = accounts.get_reasons_account_is_violating_delinquent_criteria(account.id)
if reasons:
accounts.mark_account_as_delinquent(
account.id,
reason='Account has not any valid credit card registered'
)

0 comments on commit f17210e

Please sign in to comment.