Fix notification sending for multiple dispatchers or no specified dispatchers
This commit is contained in:
parent
0658a4f587
commit
bc03763a3a
2 changed files with 46 additions and 6 deletions
|
@ -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
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue