Browse Source

convertion personne physique vers personne morale

Élie Bouttier 7 years ago
parent
commit
6370a82fd7
2 changed files with 62 additions and 4 deletions
  1. 54 0
      adhesions/management/commands/physique_vers_morale.py
  2. 8 4
      adhesions/signals.py

+ 54 - 0
adhesions/management/commands/physique_vers_morale.py

@@ -0,0 +1,54 @@
+from django.core.management.base import BaseCommand, CommandParser, CommandError
+from django.contrib.contenttypes.models import ContentType
+
+from django.contrib.auth.models import User
+from adhesions.models import Adhesion, Corporation
+
+
+def boolean_input(question, default=None):
+    result = input("%s " % question)
+    if not result and default is not None:
+        return default
+    while len(result) < 1 or result[0].lower() not in "yn":
+        result = input("Please answer yes or no: ")
+    return result[0].lower() == "y"
+
+
+class Command(BaseCommand):
+    help = 'Convertir une personne physique en personne morale'
+
+    def add_arguments(self, parser):
+        parser.add_argument('adt', type=int, help='Numéro d’adhérent')
+        parser.add_argument('raison_sociale', help='Raison sociale')
+
+    def handle(self, *args, **options):
+        pk = options['adt']
+        try:
+            adh = Adhesion.objects.get(pk=pk)
+        except Adhesion.DoesNotExist:
+            raise CommandError('L’adherent ADT%d n’existe pas.' % pk)
+        if adh.corporation:
+            raise CommandError('L’adherent ADT%d est déjà une personne morale.' % pk)
+        user = adh.user
+        if user.first_name != 'FIXME':
+            self.stdout.write(self.style.WARNING('L’adherent ADT%d s’est vu spécifier un prénom (%s), risque de perte de données.' % (pk, user.first_name)))
+            if not boolean_input('Continuer ? [y/n]'):
+                return
+        if user.last_name != '%d' % pk:
+            self.stdout.write(self.style.WARNING('L’adherent ADT%d s’est vu spécifier un nom (%s), risque de perte de données.' % (pk, user.last_name)))
+            if not boolean_input('Continuer ? [y/n]'):
+                return
+        email = user.email
+        tel = user.profile.phone_number
+        address = user.profile.address
+        if user.profile.ssh_keys:
+            self.stdout.write(self.style.WARNING('L’adherent ADT%d s’est vu spécifier une clef SSH, risque de perte de données.' % (pk, user.last_name)))
+            if not boolean_input('Continuer ? [y/n]'):
+                return
+        notes = user.profile.notes
+        corporation = Corporation(social_reason=options['raison_sociale'], email=email, phone_number=tel, address=address, notes=notes)
+        corporation.skip_adhesion_creation = True
+        corporation.save()
+        Adhesion.objects.filter(pk=pk).update(user=None, corporation=corporation)
+        user.delete()
+        self.stdout.write(self.style.SUCCESS('Adhérent ADT%d convertie en personne morale avec succès.' % pk))

+ 8 - 4
adhesions/signals.py

@@ -6,16 +6,20 @@ from .models import User, Corporation, Adhesion
 
 @receiver(post_save, sender=User, dispatch_uid='create_user_adhesion')
 def create_user_adhesion(sender, instance, created, **kwargs):
-    if created:
+    if created and not hasattr(instance, 'skip_adhesion_creation'):
         Adhesion.objects.create(user=instance)
 
 
 @receiver(post_save, sender=Corporation, dispatch_uid='create_corporation_adhesion')
 def create_corporation_adhesion(sender, instance, created, **kwargs):
-    if created:
+    if created and not hasattr(instance, 'skip_adhesion_creation'):
         Adhesion.objects.create(corporation=instance)
 
 
-@receiver(post_delete, sender=Adhesion, dispatch_uid='delete_adhesion_membership')
-def delete_adhesion_membership(sender, instance, **kwargs):
+@receiver(post_delete, sender=Adhesion, dispatch_uid='delete_adhesion')
+def delete_adhesion(sender, instance, **kwargs):
+    if instance.is_physical():
+        instance.user.delete()
+    if instance.is_moral():
+        instance.corporation.delete()
     instance.membership.delete()