Skip to content

Commit

Permalink
🔥(backend) remove field owner and is_main on CreditCard model
Browse files Browse the repository at this point in the history
Since the latest developments where we use `owners` many-to-many
relation instead of `owner` and `is_main` on CreditCardOwnership
model instead of the CreditCard model, it was time to completely
remove those fields from the CreditCard model.

Fix #1057
  • Loading branch information
jonathanreveille committed Feb 20, 2025
1 parent 3471ba5 commit 3e34abe
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 73 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to

### Changed

- Remove `owner` and `is_main` in CreditCard model permanently
- Remove `is_active` on order group client serializer

## [2.16.0] - 2025-02-13
Expand Down
1 change: 0 additions & 1 deletion src/backend/joanie/payment/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class PaymentConfig(AppConfig):
# pylint: disable=import-outside-toplevel, unused-import
def ready(self):
"""Import credit card post delete receiver."""
import joanie.payment.checks
from joanie.payment.models import ( # ,
credit_card_post_delete_receiver,
)
60 changes: 0 additions & 60 deletions src/backend/joanie/payment/checks.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.2.18 on 2025-02-18 08:55

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('payment', '0010_creditcardownership_and_more'),
]

operations = [
migrations.RemoveField(
model_name='creditcard',
name='is_main',
),
migrations.RemoveField(
model_name='creditcard',
name='owner',
),
]
10 changes: 0 additions & 10 deletions src/backend/joanie/payment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,23 +485,13 @@ class CreditCard(BaseModel):
)
expiration_year = models.PositiveSmallIntegerField(_("expiration year"))
last_numbers = models.CharField(_("last 4 numbers"), max_length=4)
# Deprecated
owner = models.ForeignKey(
to=User,
verbose_name=_("owner"),
related_name="credit_cards",
on_delete=models.CASCADE,
blank=True,
null=True,
)
owners = models.ManyToManyField(
to=User,
verbose_name=_("owners"),
related_name="payment_cards",
through="CreditCardOwnership",
through_fields=("credit_card", "owner"),
)
is_main = models.BooleanField(_("main"), default=False) # Deprecated
payment_provider = models.CharField(
_("payment provider"), max_length=50, null=True, blank=True
)
Expand Down
2 changes: 1 addition & 1 deletion src/backend/joanie/tests/payment/test_backend_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def test_payment_backend_base_do_on_payment_success(self):

# - Credit card has been deleted
self.assertIsNone(order.credit_card)
self.assertEqual(owner.credit_cards.count(), 0)
self.assertEqual(owner.payment_cards.count(), 0)
with self.assertRaises(CreditCard.DoesNotExist):
CreditCard.objects.get(id=credit_card.id)
credit_card.refresh_from_db()
Expand Down
28 changes: 27 additions & 1 deletion src/backend/joanie/tests/payment/test_models_credit_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from unittest import mock

from django.core.exceptions import ValidationError
from django.core.exceptions import FieldError, ValidationError

from joanie.core import enums
from joanie.core.factories import OrderFactory, UserFactory
Expand Down Expand Up @@ -263,3 +263,29 @@ def test_models_credit_card_delete_unused(self):
self.assertFalse(
CreditCard.objects.filter(orders__state__in=no_card_order_states).exists()
)

def test_models_credit_card_owner_field_is_removed(self):
"""
The `owner` field of the CreditCard is removed, it should raise an error if
when we want to set a User.
"""
with self.assertRaises(FieldError) as context:
CreditCardFactory(owner=UserFactory())

self.assertEqual(
str(context.exception),
"Invalid field name(s) for model CreditCard: 'owner'.",
)

def test_models_credit_card_is_main_field_is_removed(self):
"""
The `is_main` field of the CreditCard is removed, it should raise an error if
when we want to set a boolean value.
"""
with self.assertRaises(FieldError) as context:
CreditCardFactory(is_main=True)

self.assertEqual(
str(context.exception),
"Invalid field name(s) for model CreditCard: 'is_main'.",
)

0 comments on commit 3e34abe

Please sign in to comment.