|
@@ -5,7 +5,7 @@ import datetime
|
|
|
|
|
|
from django.conf import settings
|
|
from django.conf import settings
|
|
from django.db import models
|
|
from django.db import models
|
|
-from django.db.models import Q
|
|
|
|
|
|
+from django.db.models import Count, Q
|
|
from django.core.validators import MinValueValidator
|
|
from django.core.validators import MinValueValidator
|
|
|
|
|
|
|
|
|
|
@@ -79,6 +79,27 @@ class Offer(models.Model):
|
|
verbose_name = 'offre'
|
|
verbose_name = 'offre'
|
|
|
|
|
|
|
|
|
|
|
|
+class OfferSubscriptionQuerySet(models.QuerySet):
|
|
|
|
+ def running(self, at_date=None):
|
|
|
|
+ """ Only the running contracts at a given date.
|
|
|
|
+
|
|
|
|
+ Running mean already started and not stopped yet
|
|
|
|
+ """
|
|
|
|
+
|
|
|
|
+ if at_date is None:
|
|
|
|
+ at_date = datetime.date.today()
|
|
|
|
+
|
|
|
|
+ return self.filter(Q(subscription_date__lte=at_date) &
|
|
|
|
+ (Q(resign_date__gt=at_date) |
|
|
|
|
+ Q(resign_date__isnull=True)))
|
|
|
|
+
|
|
|
|
+ def offer_summary(self):
|
|
|
|
+ """ Agregates as a count of subscriptions per offer
|
|
|
|
+ """
|
|
|
|
+ return self.values('offer__name', 'offer__reference').annotate(
|
|
|
|
+ num_subscriptions=Count('offer'))
|
|
|
|
+
|
|
|
|
+
|
|
class OfferSubscription(models.Model):
|
|
class OfferSubscription(models.Model):
|
|
"""Only contains administrative details about a subscription, not
|
|
"""Only contains administrative details about a subscription, not
|
|
technical. Nothing here should end up into the LDAP backend.
|
|
technical. Nothing here should end up into the LDAP backend.
|
|
@@ -107,6 +128,8 @@ class OfferSubscription(models.Model):
|
|
member = models.ForeignKey('members.Member', verbose_name='membre')
|
|
member = models.ForeignKey('members.Member', verbose_name='membre')
|
|
offer = models.ForeignKey('Offer', verbose_name='offre')
|
|
offer = models.ForeignKey('Offer', verbose_name='offre')
|
|
|
|
|
|
|
|
+ objects = OfferSubscriptionQuerySet().as_manager()
|
|
|
|
+
|
|
def get_subscription_reference(self):
|
|
def get_subscription_reference(self):
|
|
return settings.SUBSCRIPTION_REFERENCE.format(subscription=self)
|
|
return settings.SUBSCRIPTION_REFERENCE.format(subscription=self)
|
|
get_subscription_reference.short_description = 'Référence'
|
|
get_subscription_reference.short_description = 'Référence'
|