adherents.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from django.core.management.base import BaseCommand, CommandParser, CommandError
  2. from django.contrib.contenttypes.models import ContentType
  3. from django.contrib.auth.models import User
  4. from adhesions.models import Adhesion, Corporation
  5. class Command(BaseCommand):
  6. help = 'Gestion des adhérents'
  7. def add_arguments(self, parser):
  8. cmd = self
  9. class SubParser(CommandParser):
  10. def __init__(self, **kwargs):
  11. super().__init__(cmd, **kwargs)
  12. subparsers = parser.add_subparsers(dest='command', parser_class=SubParser)
  13. subparsers.required = True
  14. parser_list = subparsers.add_parser('list', help='Lister les adhérents')
  15. group = parser_list.add_mutually_exclusive_group()
  16. group.add_argument('--physique', action='store_true',
  17. help='Afficher uniquement les personnes physiques')
  18. group.add_argument('--morale', action='store_true',
  19. help='Afficher uniquement les personnes morales')
  20. parser_change = subparsers.add_parser('create', help='Créer un adhérent')
  21. parser_change.add_argument('-i', '--id', type=int, help='Spécifier le numéro du nouvel adhérent')
  22. subsubparsers = parser_change.add_subparsers(dest='type', parser_class=SubParser)
  23. subsubparsers.required = True
  24. parser_physique = subsubparsers.add_parser('physique', help='Créer une personne physique')
  25. parser_physique.add_argument('--login', dest='username', required=True, help='Login')
  26. parser_physique.add_argument('--firstname', dest='first_name', required=True, help='Prénom')
  27. parser_physique.add_argument('--lastname', dest='last_name', required=True, help='Nom de famille')
  28. parser_physique.add_argument('--email', help='Adresse email')
  29. parser_morale = subsubparsers.add_parser('morale', help='Créer une personne morale')
  30. parser_morale.add_argument('--name', dest='social_reason', required=True, help='Nom ou raison sociale')
  31. parser_morale.add_argument('--description', help='Description', default='')
  32. def handle(self, *args, **options):
  33. cmd = options.pop('command')
  34. getattr(self, 'handle_{cmd}'.format(cmd=cmd))(*args, **options)
  35. def handle_list(self, *args, **options):
  36. adhesions = Adhesion.objects.all()
  37. if options['physique']:
  38. adhesions = adhesions.filter(adherent_type=ContentType.objects.get(app_label='auth', model='user'))
  39. if options['morale']:
  40. adhesions = adhesions.filter(adherent_type=ContentType.objects.get(app_label='adhesions', model='corporation'))
  41. for adh in adhesions:
  42. self.stdout.write("#{id:<8}{name:<30}{type:<20}".format(**{
  43. 'id': adh.id,
  44. 'name': adh.get_adherent_name(),
  45. 'type': adh.type,
  46. }))
  47. def handle_create(self, *args, **options):
  48. if options['id'] and Adhesion.objects.filter(id=options['id']).exists():
  49. raise CommandError('Le numéro d’adherent "%d" est déjà attribué' % options['id'])
  50. getattr(self, 'handle_create_personne_{type}'.format(**{
  51. 'type': options.pop('type'),
  52. }))(*args, **options)
  53. def handle_create_personne_physique(self, *args, **options):
  54. if User.objects.filter(username=options['username']).exists():
  55. raise CommandError('Le nom d’utilisateur "%s" est déjà utilisé' % options['username'])
  56. user = User.objects.create_user(**{key: options[key] for key in ['username', 'email', 'first_name', 'last_name']})
  57. adhesion = Adhesion.objects.create(id=options['id'], adherent=user)
  58. self.stdout.write(self.style.SUCCESS('Adhérent #%d créé avec succès' % adhesion.id))
  59. def handle_create_personne_morale(self, *args, **options):
  60. if Corporation.objects.filter(social_reason=options['social_reason']).exists():
  61. raise CommandError('La raison sociale "%s" est déjà utilisé' % options['social_reason'])
  62. corporation = Corporation.objects.create(**{key: options[key] for key in ['social_reason', 'description']})
  63. adhesion = Adhesion.objects.create(id=options['id'], adherent=corporation)
  64. self.stdout.write(self.style.SUCCESS('Adhérent #%d créé avec succès' % adhesion.id))