]> git.parisson.com Git - teleforma.git/commitdiff
Command to send Webclass notifications
authorGael Le Mignot <gael@pilotsystems.net>
Wed, 10 Jun 2020 12:16:20 +0000 (14:16 +0200)
committerGael Le Mignot <gael@pilotsystems.net>
Wed, 10 Jun 2020 12:16:20 +0000 (14:16 +0200)
teleforma/management/commands/teleforma-webclass-send-notifications.py [new file with mode: 0644]

diff --git a/teleforma/management/commands/teleforma-webclass-send-notifications.py b/teleforma/management/commands/teleforma-webclass-send-notifications.py
new file mode 100644 (file)
index 0000000..80546f7
--- /dev/null
@@ -0,0 +1,50 @@
+# -*- coding: utf-8 -*-
+
+from optparse import make_option
+from django.conf import settings
+from django.core.management.base import BaseCommand, CommandError
+from django.contrib.auth.models import User
+from teleforma.webclass.models import *
+from teleforma.models import *
+
+from postman.utils import email_visitor, notify_user
+from postman.models import Message
+
+WARN_DELAY_MAX = 15
+WARN_DELAY_MIN = 5
+
+class Command(BaseCommand):
+    help = "Send the Webclass will start notifications"
+
+    def handle(self, *args, **options):
+        now = datetime.datetime.now()
+        dow = now.weekday()
+        begin = now + datetime.timedelta(minutes=WARN_DELAY_MIN)
+        end = now + datetime.timedelta(minutes=WARN_DELAY_MAX)
+        slots = WebclassSlot.objects.filter(day = dow,
+                                            start_hour__gte = begin.time(),
+                                            start_hour__lt = end.time())
+        site = Site.objects.all()[0]
+        admin = User.objects.get(username = 'Admin-CRFPA')
+        date = now.strftime('%d/%m/%Y')
+        for slot in slots:
+            hour = slot.start_hour.strftime('%Hh%M')
+            print("Sending notifications for %s:%s (%s)" % (slot.webclass.course,
+                                                            slot.start_hour,
+                                                            slot.id))
+            subject = "Webclass_%s_%s_%s" % (slot.webclass.course,
+                                             date, hour)
+                                             
+            body = """Votre Webclasse va commencer."""
+
+            users = slot.participants.all()
+            for user in users:
+                print(" => %s" % user)
+                msg = Message(subject = subject,
+                              body = body,
+                              sender = admin,
+                              recipient = user)
+                msg.moderation_status = 'a'
+                msg.save()
+                notify_user(msg, 'acceptance', site)                          
+