From bc03763a3a13075be856a3516b1dd320a870ba80 Mon Sep 17 00:00:00 2001 From: Kumi Date: Fri, 15 Jan 2021 15:14:08 +0100 Subject: [PATCH] Fix notification sending for multiple dispatchers or no specified dispatchers --- habits/models.py | 39 +++++++++++++++++++++++++++++++++++---- msgio/models.py | 13 +++++++++++-- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/habits/models.py b/habits/models.py index 5cba07a..2ce83df 100644 --- a/habits/models.py +++ b/habits/models.py @@ -11,13 +11,13 @@ from common.fields import WeekdayField, DayOfMonthField class Habit(models.Model): name = models.CharField(max_length=64) - icon = models.CharField(default="fas fa-user-clock", max_length=64) + icon = models.CharField(default="fas fa-user-clock") color = ColorField(default="#000000") description = models.TextField(null=True, blank=True) - active = models.BooleanField(default=True) class HabitSchedule(PolymorphicModel): habit = models.ForeignKey(Habit, models.CASCADE) + active = models.BooleanField(default=True) @property def next_scheduled(self, today=True, now=timezone.now()): @@ -29,16 +29,47 @@ class MonthlyHabitSchedule(HabitSchedule): @property def next_scheduled(self, today=True, now=timezone.now()): if self.day < now.day: - date = now.replace(day=self.day) + relativedelta(months=1) + date = now + relativedelta(months=1) + relativedelta(day=self.day) elif self.day == now.day: date = now if today else now + relativedelta(months=1) else: date = now + relativedelta(day=self.day) + if date.date() == now.date() and not today: + date = now + relativedelta(months=1) + relativedelta(day=self.day) + return date.date() class WeeklyHabitSchedule(HabitSchedule): weekdays = WeekdayField() + @property + def next_scheduled(self, today=True, now=timezone.now()): + found = None + for weekday in self.weekdays: + on = now + relativedelta(weekday=weekday) + if on < now: + on += relativedelta(weeks=1) + if on.day == now.day: + if today: + found = now + break + on += relativedelta(weeks=1) + if (not found) or on.date() < found.date(): + found = on + + return found.date() + class DailyHabitSchedule(HabitSchedule): - pass + @property + def next_scheduled(self, today=True, now=timezone.now()): + if self.active: + return (now if today else (now + relativedelta(days=1))).date() + +class DateHabitSchedule(HabitSchedule): + date = models.DateField() + + @property + def next_scheduled(self, today=True, now=timezone.now()): + if self.active and ((self.date > now.date()) or (today and (self.date == now.date()))): + return self.date \ No newline at end of file diff --git a/msgio/models.py b/msgio/models.py index ab0c308..e421527 100644 --- a/msgio/models.py +++ b/msgio/models.py @@ -12,8 +12,17 @@ class Notification(models.Model): data = models.CharField(max_length=128, null=True, blank=True) def send(self): - for dispatcher in self.notificationdispatcher_set.all(): - return send_message.send_robust(self.__class__, dispatcher=dispatcher.dispatcher, notification=self) + dispatchers = self.notificationdispatcher_set.all() + response = [] + + if dispatchers: + for dispatcher in dispatchers: + response.append(send_message.send_robust(self.__class__, dispatcher=dispatcher.dispatcher, notification=self)) + else: + for dispatcher in GatewayUser.objects.filter(user=self.recipient): + response.append(send_message.send_robust(self.__class__, dispatcher=dispatcher.gateway, notification=self)) + + return response class NotificationDispatcher(models.Model): notification = models.ForeignKey(Notification, models.CASCADE)