Commit 2ed1d8ec authored by Luko van der Maas's avatar Luko van der Maas
Browse files

start of automatic pushnotifications for pizzas

parent 2f4aa8b3
......@@ -56,6 +56,22 @@ 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,6 +17,7 @@ class ProductAdmin(admin.ModelAdmin):
@admin.register(PizzaEvent)
class PizzaEventAdmin(admin.ModelAdmin):
list_display = ('title', 'orders')
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
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('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'),
),
migrations.AddField(
model_name='pizzaevent',
name='task_id',
field=models.CharField(blank=True, max_length=50, null=True),
),
]
......@@ -6,6 +6,10 @@ from django.utils.translation import ugettext_lazy as _
import events
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
......@@ -14,6 +18,12 @@ class PizzaEvent(models.Model):
end = models.DateTimeField(_("Order until"))
event = models.OneToOneField(events.models.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)
@property
def title(self):
return self.event.title
......@@ -50,6 +60,10 @@ class PizzaEvent(models.Model):
except PizzaEvent.DoesNotExist:
return None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._end = self.end
def validate_unique(self, exclude=None):
super().validate_unique(exclude)
for other in PizzaEvent.objects.filter(
......@@ -71,6 +85,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()
super().save(*args, **kwargs)
def delete(self, using=None, keep_parents=False):
if self.task_id:
revoke_task(self.task_id)
return super().delete(using, keep_parents)
def __str__(self):
return 'Pizzas for ' + str(self.event)
......
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