+ @property
+ def progress_percent(self):
+ if not self.target:
+ return 0
+ return (self.progress or 0) / self.target * 100
+
+ def update_progress(self):
+ # Total of new payments during the action.
+ # This definition will need to change for longer timespans.
+ if not self.since or not self.until or not self.target:
+ return
+ Schedule = apps.get_model('club', 'Schedule')
+ self.progress = Schedule.objects.filter(
+ payed_at__gte=self.since,
+ payed_at__lte=self.until,
+ ).aggregate(c=models.Sum('amount'))['c']
+ self.save(update_fields=['progress'])
+
+ @classmethod
+ def update_all_progress(cls):
+ for obj in cls.objects.exclude(target=None):
+ obj.update_progress()
+