|
@@ -4,7 +4,7 @@ from __future__ import unicode_literals
|
|
|
import datetime
|
|
|
|
|
|
from django.db import models
|
|
|
-from django.db.models import Q
|
|
|
+from django.db.models import Count, Q
|
|
|
from django.core.validators import MinValueValidator
|
|
|
|
|
|
|
|
@@ -75,6 +75,27 @@ class Offer(models.Model):
|
|
|
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').annotate(
|
|
|
+ num_subscriptions=Count('offer'))
|
|
|
+
|
|
|
+
|
|
|
class OfferSubscription(models.Model):
|
|
|
"""Only contains administrative details about a subscription, not
|
|
|
technical. Nothing here should end up into the LDAP backend.
|
|
@@ -103,6 +124,8 @@ class OfferSubscription(models.Model):
|
|
|
member = models.ForeignKey('members.Member', verbose_name='membre')
|
|
|
offer = models.ForeignKey('Offer', verbose_name='offre')
|
|
|
|
|
|
+ objects = OfferSubscriptionQuerySet().as_manager()
|
|
|
+
|
|
|
def __unicode__(self):
|
|
|
return '%s - %s - %s' % (self.member, self.offer.name,
|
|
|
self.subscription_date)
|