models.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. from django.db import models
  2. from django.urls import reverse
  3. from django.contrib.auth.models import User as AuthUser
  4. from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
  5. from django.contrib.contenttypes.models import ContentType
  6. from django.urls import reverse
  7. from django.utils.html import format_html
  8. from djadhere.utils import get_active_filter
  9. from banking.models import RecurringPayment, PaymentUpdate
  10. class ActiveAdhesionManager(models.Manager):
  11. def get_queryset(self):
  12. qs = super().get_queryset()
  13. qs = qs.annotate(
  14. payment_method=models.Subquery(
  15. PaymentUpdate.objects.filter(payment=models.OuterRef('membership__pk'))
  16. .values('payment_method')[:1]
  17. )
  18. )
  19. qs = qs.annotate(
  20. active=models.Case(
  21. models.When(payment_method__isnull=True, then=None),
  22. models.When(payment_method=PaymentUpdate.STOP, then=False),
  23. default=True,
  24. output_field=models.NullBooleanField()
  25. )
  26. )
  27. return qs
  28. class User(AuthUser):
  29. def get_model_perms(self, request):
  30. return {}
  31. @property
  32. def adhesions(self): # user and corporations (for which the user is a member) adhesions
  33. return self.profile.adhesions
  34. class Meta:
  35. proxy = True
  36. verbose_name = 'personne physique'
  37. verbose_name_plural = 'personnes physiques'
  38. def get_absolute_url(self):
  39. return reverse('admin:%s_%s_change' % (self._meta.app_label, self._meta.model_name), args=(self.pk,))
  40. def __str__(self):
  41. return self.get_full_name() or self.username
  42. class Corporation(models.Model):
  43. social_reason = models.CharField(max_length=256, verbose_name='Raison sociale', unique=True)
  44. description = models.TextField(blank=True, default='')
  45. members = models.ManyToManyField(User, blank=True, verbose_name='Membres',
  46. related_name='corporations', related_query_name='corporation')
  47. email = models.EmailField(verbose_name='Adresse e-mail', blank=True)
  48. phone_number = models.CharField(max_length=16, blank=True, default='',
  49. verbose_name='Numéro de téléphone')
  50. address = models.TextField(blank=True, default='', verbose_name='Adresse')
  51. notes = models.TextField(blank=True, default='')
  52. class Meta:
  53. verbose_name = 'personne morale'
  54. verbose_name_plural = 'personnes morales'
  55. def get_absolute_url(self):
  56. return reverse('admin:%s_%s_change' % (self._meta.app_label, self._meta.model_name), args=(self.pk,))
  57. def __str__(self):
  58. return self.social_reason
  59. # Terminologie : une « adhésion » désgine une instance de ce modèle
  60. # tandis qu’un·e « adhérent·e » désigne un·e user ou une corporation.
  61. class Adhesion(models.Model):
  62. limit = models.Q(app_label='auth', model='user') \
  63. | models.Q(app_label='adhesions', model='corporation')
  64. id = models.AutoField(verbose_name='Numéro d’adhérent·e', primary_key=True, editable=True)
  65. created = models.DateTimeField(null=True, blank=True, auto_now_add=True)
  66. notes = models.TextField(blank=True, default='', verbose_name='Notes (obsolète)', help_text='Obsolète, ne plus ajouter d’informations ici.')
  67. active_legacy = models.NullBooleanField(default=None, verbose_name='Adhésion en cours')
  68. user = models.OneToOneField(User, null=True, on_delete=models.PROTECT)
  69. corporation = models.OneToOneField(Corporation, null=True, on_delete=models.PROTECT)
  70. membership = models.OneToOneField(RecurringPayment, on_delete=models.CASCADE)
  71. objects = ActiveAdhesionManager()
  72. def save(self, *args, **kwargs):
  73. if not hasattr(self, 'membership'):
  74. self.membership = RecurringPayment.objects.create()
  75. super().save(*args, **kwargs)
  76. class Meta:
  77. verbose_name = 'adhésion'
  78. ordering = ('id',)
  79. def is_physical(self):
  80. return self.user is not None
  81. def is_moral(self):
  82. return self.corporation is not None
  83. @property
  84. def type(self):
  85. if self.is_physical():
  86. return 'Personne physique'
  87. else:
  88. return 'Personne morale'
  89. @property
  90. def adherent(self):
  91. if self.is_physical():
  92. return self.user
  93. else:
  94. return self.corporation
  95. def get_adherent_link(self):
  96. return format_html(u'<a href="{}">{}</a>', self.adherent.get_absolute_url(), self.adherent)
  97. get_adherent_link.short_description = 'Nom ou raison sociale'
  98. def get_adhesion_link(self):
  99. return format_html(u'<a href="{}">ADT{}</a>', self.get_absolute_url(), str(self.id))
  100. get_adhesion_link.short_description = 'Numéro d’adhérent·e'
  101. def get_absolute_url(self):
  102. return reverse('admin:%s_%s_change' % (self._meta.app_label, self._meta.model_name), args=(self.pk,))
  103. def is_active(self):
  104. return self.active
  105. is_active.boolean = True
  106. is_active.short_description = 'Actif'
  107. def __str__(self):
  108. return 'ADT%d' % self.id
  109. if self.id is None:
  110. return '?'
  111. else:
  112. return 'ADT%d (%s)' % (self.id, self.adherent)