Commit 8f5b3b5b authored by Luko van der Maas's avatar Luko van der Maas
Browse files

Added automatic pushnotifications for the end of pizza event

parent 2ed1d8ec
......@@ -56,22 +56,6 @@ pizzas.services module
:undoc-members:
:show-inheritance:
pizzas.tasks module
-------------------
.. automodule:: pizzas.tasks
:members:
:undoc-members:
:show-inheritance:
pizzas.tasks module
-------------------
.. automodule:: pizzas.tasks
:members:
:undoc-members:
:show-inheritance:
pizzas.urls module
------------------
......
......@@ -17,7 +17,7 @@ class ProductAdmin(admin.ModelAdmin):
@admin.register(PizzaEvent)
class PizzaEventAdmin(admin.ModelAdmin):
list_display = ('title', 'orders')
readonly_fields = ('task_id',)
# readonly_fields = ('task_id',)
def orders(self, obj):
return format_html(_('<strong><a href="{link}">Orders</a></strong>'),
......
# Generated by Django 2.0.6 on 2018-08-31 10:55
# Generated by Django 2.1.3 on 2018-12-19 19:32
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('pushnotifications', '0013_auto_20181111_1319'),
('pizzas', '0006_product_restricted_squashed_0008_auto_20180102_1635'),
]
operations = [
migrations.AddField(
model_name='pizzaevent',
name='send_notification',
field=models.BooleanField(default=True, verbose_name='Send an order notification'),
name='end_reminder',
field=models.OneToOneField(null=True, on_delete=django.db.models.deletion.CASCADE, to='pushnotifications.ScheduledMessage'),
),
migrations.AddField(
model_name='pizzaevent',
name='task_id',
field=models.CharField(blank=True, max_length=50, null=True),
name='send_notification',
field=models.BooleanField(default=True, verbose_name='Send an order notification'),
),
]
......@@ -4,25 +4,27 @@ from django.db.models import Q
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
import events
from events.models import Event
import members
from members.models import Member
from .tasks import create_send_message
from pushnotifications.models import ScheduledMessage, Category
from utils.tasks import schedule_task, revoke_task
from utils.translation import ModelTranslateMeta, MultilingualField
class PizzaEvent(models.Model):
start = models.DateTimeField(_("Order from"))
end = models.DateTimeField(_("Order until"))
event = models.OneToOneField(events.models.Event, on_delete=models.CASCADE)
event = models.OneToOneField(Event, on_delete=models.CASCADE)
send_notification = models.BooleanField(
_("Send an order notification"),
default=True
)
task_id = models.CharField(max_length=50, blank=True, null=True)
end_reminder = models.OneToOneField(
ScheduledMessage,
models.CASCADE,
null=True
)
@property
def title(self):
......@@ -85,28 +87,29 @@ class PizzaEvent(models.Model):
'end': _('The end is before the start of this event.'),
})
def schedule(self):
"""Schedules a Celery task to send this message"""
return schedule_task(
create_send_message,
args=(self.pk,),
eta=self.end - timezone.timedelta(minutes=10)
)
def save(self, *args, **kwargs):
if not (self._end == self.end):
if self.task_id:
# Revoke that task in case its time has changed
revoke_task(self.task_id)
super().save(*args, **kwargs)
self.task_id = self.schedule()
if not self.end_reminder:
end_reminder = ScheduledMessage()
end_reminder.title_en = 'Order pizza'
end_reminder.title_nl = 'Pizza bestellen'
end_reminder.body_en = 'You can order pizzas for 10 more minutes'
end_reminder.body_nl = "Je kan nog 10 minuten pizza's bestellen"
end_reminder.category = Category.objects.get(key='pizza')
end_reminder.time = self.end
end_reminder.save()
if self.event.registration_required:
end_reminder.users.set(self.event.registrations)
else:
end_reminder.users.set(Member.active_members.all())
super().save(*args, **kwargs)
self.end_reminder = end_reminder
def delete(self, using=None, keep_parents=False):
if self.task_id:
revoke_task(self.task_id)
return super().delete(using, keep_parents)
if self._end != self.end:
self.end_reminder.time = self.end
self.end_reminder.save()
super().save(*args, **kwargs)
def __str__(self):
return 'Pizzas for ' + str(self.event)
......@@ -170,6 +173,18 @@ class Order(models.Model):
'name': _('Either specify a member or a name'),
})
def save(self, *args, **kwargs):
if not self.id:
self.pizza_event.end_reminder.users.remove(self.member)
super().save(*args, **kwargs)
def delete(self, using=None, keep_parents=False):
if not self.id:
self.pizza_event.end_reminder.users.add(self.member)
super().delete(using, keep_parents)
@property
def member_name(self):
if self.member is not None:
......
from __future__ import absolute_import
from celery import shared_task
from django.apps import apps
from django.utils import timezone
@shared_task
def create_send_message(pizza_id):
"""Send a push notification"""
print('Sending push notification for pizza event {}'.format(pizza_id))
PizzaEvent = apps.get_model('pizzas', 'PizzaEvent')
try:
pizza_event = PizzaEvent.objects.get(pk=pizza_id)
except PizzaEvent.DoesNotExist:
print('Cannot find pizza event')
return
Message = apps.get_model('pushnotifications', 'Message')
Category = apps.get_model('pushnotifications', 'Category')
Member = apps.get_model('members', 'Member')
end_reminder = Message()
end_reminder.title_en = 'Order pizza'
end_reminder.title_nl = 'Pizza bestellen'
end_reminder.body_en = 'You can order pizzas for 10 more minutes'
end_reminder.body_nl = "Je kan nog 10 minuten pizza's bestellen"
end_reminder.category = Category.objects.get(key='pizza')
end_reminder.save()
if pizza_event.event.registration_required:
pizza_event.end_reminder.users.set(
pizza_event.event.registration_set.filter(
date_cancelled__isnull=True
)
)
else:
pizza_event.end_reminder.users.set(Member.active_members.all())
pizza_event.save()
end_reminder.send()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment