Setup deleted account

Pour pouvoir supprimer un compte, on crée un compte dummy qui a pour but
de recevoir les objets non supprimables (caisses, transferts/opérations
pour statistiques, etc.). Lors de la délétion d'un compte, tout est
transféré sur le dummy, qui est créé via migration.
This commit is contained in:
Ludovic Stephan 2019-05-23 16:09:19 +02:00
parent 85b1e974ff
commit 63fff6ca7c
4 changed files with 159 additions and 15 deletions

View file

@ -1 +1,3 @@
default_app_config = "kfet.apps.KFetConfig" default_app_config = "kfet.apps.KFetConfig"
KFET_DELETED_TRIGRAMME = "☠☠☠"
KFET_DELETED_USERNAME = "kfet_deleted_user"

View file

@ -0,0 +1,32 @@
# Generated by Django 2.2 on 2019-05-23 13:54
from django.db import migrations, models
from kfet import KFET_DELETED_TRIGRAMME, KFET_DELETED_USERNAME
def setup_kfet_deleted_user(apps, schema_editor):
"""
Setup models instances for the kfet deleted account.
Username and trigramme are retrieved from kfet.__init__ module.
Other data are registered here.
"""
User = apps.get_model("auth", "User")
CofProfile = apps.get_model("gestioncof", "CofProfile")
Account = apps.get_model("kfet", "Account")
user, _ = User.objects.update_or_create(
username=KFET_DELETED_USERNAME, defaults={"first_name": "Compte K-Fêt supprimé"}
)
profile, _ = CofProfile.objects.update_or_create(user=user)
account, _ = Account.objects.update_or_create(
cofprofile=profile, defaults={"trigramme": KFET_DELETED_TRIGRAMME}
)
class Migration(migrations.Migration):
dependencies = [("kfet", "0066_on_delete_actions")]
operations = [migrations.RunPython(setup_kfet_deleted_user)]

View file

@ -0,0 +1,103 @@
# Generated by Django 2.2 on 2019-05-23 16:17
from django.db import migrations, models
import kfet.models
class Migration(migrations.Migration):
dependencies = [("kfet", "0067_deleted_account")]
operations = [
migrations.AlterField(
model_name="checkout",
name="created_by",
field=models.ForeignKey(
on_delete=models.SET(kfet.models.get_deleted_account),
related_name="+",
to="kfet.Account",
),
),
migrations.AlterField(
model_name="checkoutstatement",
name="by",
field=models.ForeignKey(
on_delete=models.SET(kfet.models.get_deleted_account),
related_name="+",
to="kfet.Account",
),
),
migrations.AlterField(
model_name="inventory",
name="by",
field=models.ForeignKey(
on_delete=models.SET(kfet.models.get_deleted_account),
related_name="+",
to="kfet.Account",
),
),
migrations.AlterField(
model_name="operation",
name="addcost_for",
field=models.ForeignKey(
blank=True,
default=None,
null=True,
on_delete=models.SET(kfet.models.get_deleted_account),
related_name="addcosts",
to="kfet.Account",
),
),
migrations.AlterField(
model_name="operation",
name="canceled_by",
field=models.ForeignKey(
blank=True,
default=None,
null=True,
on_delete=models.SET(kfet.models.get_deleted_account),
related_name="+",
to="kfet.Account",
),
),
migrations.AlterField(
model_name="operationgroup",
name="on_acc",
field=models.ForeignKey(
on_delete=models.SET(kfet.models.get_deleted_account),
related_name="opesgroup",
to="kfet.Account",
),
),
migrations.AlterField(
model_name="transfer",
name="canceled_by",
field=models.ForeignKey(
blank=True,
default=None,
null=True,
on_delete=models.SET(kfet.models.get_deleted_account),
related_name="+",
to="kfet.Account",
),
),
migrations.AlterField(
model_name="transfer",
name="from_acc",
field=models.ForeignKey(
on_delete=models.SET(kfet.models.get_deleted_account),
related_name="transfers_from",
to="kfet.Account",
),
),
migrations.AlterField(
model_name="transfer",
name="to_acc",
field=models.ForeignKey(
on_delete=models.SET(kfet.models.get_deleted_account),
related_name="transfers_to",
to="kfet.Account",
),
),
]

View file

@ -12,6 +12,7 @@ from django.utils.translation import ugettext_lazy as _
from gestioncof.models import CofProfile from gestioncof.models import CofProfile
from . import KFET_DELETED_TRIGRAMME
from .auth import KFET_GENERIC_TRIGRAMME from .auth import KFET_GENERIC_TRIGRAMME
from .auth.models import GenericTeamToken # noqa from .auth.models import GenericTeamToken # noqa
from .config import kfet_config from .config import kfet_config
@ -267,12 +268,6 @@ class Account(models.Model):
self.password = hash_password(clear_password) self.password = hash_password(clear_password)
# Surcharge de delete
# Pas de suppression possible
# Cas à régler plus tard
def delete(self, *args, **kwargs):
pass
def update_negative(self): def update_negative(self):
if self.real_balance < 0: if self.real_balance < 0:
if hasattr(self, "negative") and not self.negative.start: if hasattr(self, "negative") and not self.negative.start:
@ -299,6 +294,10 @@ class Account(models.Model):
self.trigramme = trigramme self.trigramme = trigramme
def get_deleted_account():
return Account.objects.get(trigramme=KFET_DELETED_TRIGRAMME)
class AccountNegativeManager(models.Manager): class AccountNegativeManager(models.Manager):
"""Manager for AccountNegative model.""" """Manager for AccountNegative model."""
@ -350,7 +349,9 @@ class CheckoutQuerySet(models.QuerySet):
class Checkout(models.Model): class Checkout(models.Model):
created_by = models.ForeignKey(Account, on_delete=models.PROTECT, related_name="+") created_by = models.ForeignKey(
Account, on_delete=models.SET(get_deleted_account), related_name="+"
)
name = models.CharField(max_length=45) name = models.CharField(max_length=45)
valid_from = models.DateTimeField() valid_from = models.DateTimeField()
valid_to = models.DateTimeField() valid_to = models.DateTimeField()
@ -385,7 +386,9 @@ class Checkout(models.Model):
class CheckoutStatement(models.Model): class CheckoutStatement(models.Model):
by = models.ForeignKey(Account, on_delete=models.PROTECT, related_name="+") by = models.ForeignKey(
Account, on_delete=models.SET(get_deleted_account), related_name="+"
)
checkout = models.ForeignKey( checkout = models.ForeignKey(
Checkout, on_delete=models.CASCADE, related_name="statements" Checkout, on_delete=models.CASCADE, related_name="statements"
) )
@ -520,7 +523,9 @@ class Inventory(models.Model):
articles = models.ManyToManyField( articles = models.ManyToManyField(
Article, through="InventoryArticle", related_name="inventories" Article, through="InventoryArticle", related_name="inventories"
) )
by = models.ForeignKey(Account, on_delete=models.PROTECT, related_name="+") by = models.ForeignKey(
Account, on_delete=models.SET(get_deleted_account), related_name="+"
)
at = models.DateTimeField(auto_now_add=True) at = models.DateTimeField(auto_now_add=True)
# Optional # Optional
order = models.OneToOneField( order = models.OneToOneField(
@ -626,16 +631,18 @@ class Transfer(models.Model):
TransferGroup, on_delete=models.PROTECT, related_name="transfers" TransferGroup, on_delete=models.PROTECT, related_name="transfers"
) )
from_acc = models.ForeignKey( from_acc = models.ForeignKey(
Account, on_delete=models.PROTECT, related_name="transfers_from" Account,
on_delete=models.SET(get_deleted_account),
related_name="transfers_from",
) )
to_acc = models.ForeignKey( to_acc = models.ForeignKey(
Account, on_delete=models.PROTECT, related_name="transfers_to" Account, on_delete=models.SET(get_deleted_account), related_name="transfers_to"
) )
amount = models.DecimalField(max_digits=6, decimal_places=2) amount = models.DecimalField(max_digits=6, decimal_places=2)
# Optional # Optional
canceled_by = models.ForeignKey( canceled_by = models.ForeignKey(
Account, Account,
on_delete=models.PROTECT, on_delete=models.SET(get_deleted_account),
null=True, null=True,
blank=True, blank=True,
default=None, default=None,
@ -649,7 +656,7 @@ class Transfer(models.Model):
class OperationGroup(models.Model): class OperationGroup(models.Model):
on_acc = models.ForeignKey( on_acc = models.ForeignKey(
Account, on_delete=models.PROTECT, related_name="opesgroup" Account, on_delete=models.SET(get_deleted_account), related_name="opesgroup"
) )
checkout = models.ForeignKey( checkout = models.ForeignKey(
Checkout, on_delete=models.PROTECT, related_name="opesgroup" Checkout, on_delete=models.PROTECT, related_name="opesgroup"
@ -706,7 +713,7 @@ class Operation(models.Model):
article_nb = models.PositiveSmallIntegerField(blank=True, null=True, default=None) article_nb = models.PositiveSmallIntegerField(blank=True, null=True, default=None)
canceled_by = models.ForeignKey( canceled_by = models.ForeignKey(
Account, Account,
on_delete=models.PROTECT, on_delete=models.SET(get_deleted_account),
related_name="+", related_name="+",
blank=True, blank=True,
null=True, null=True,
@ -715,7 +722,7 @@ class Operation(models.Model):
canceled_at = models.DateTimeField(blank=True, null=True, default=None) canceled_at = models.DateTimeField(blank=True, null=True, default=None)
addcost_for = models.ForeignKey( addcost_for = models.ForeignKey(
Account, Account,
on_delete=models.PROTECT, on_delete=models.SET(get_deleted_account),
related_name="addcosts", related_name="addcosts",
blank=True, blank=True,
null=True, null=True,