]> git.0d.be Git - django-panik-nonstop.git/commitdiff
add model for concrete stream occurences
authorFrédéric Péters <fpeters@0d.be>
Thu, 21 May 2020 07:06:09 +0000 (09:06 +0200)
committerFrédéric Péters <fpeters@0d.be>
Thu, 21 May 2020 07:06:09 +0000 (09:06 +0200)
nonstop/management/commands/recurring_streams.py [new file with mode: 0644]
nonstop/migrations/0024_recurringstreamoccurence.py [new file with mode: 0644]
nonstop/models.py

diff --git a/nonstop/management/commands/recurring_streams.py b/nonstop/management/commands/recurring_streams.py
new file mode 100644 (file)
index 0000000..970d1e5
--- /dev/null
@@ -0,0 +1,21 @@
+import datetime
+
+from django.core.management.base import BaseCommand
+
+from nonstop.models import RecurringStreamDiffusion, RecurringStreamOccurence
+from emissions.models import Absence
+
+
+class Command(BaseCommand):
+    def handle(self, verbosity, **kwargs):
+        for diffusion in RecurringStreamDiffusion.objects.filter(is_active=True):
+            next_planned_date = diffusion.schedule.get_next_planned_date(since=datetime.datetime.now())
+            try:
+                occurence = RecurringStreamOccurence.objects.get(diffusion=diffusion, datetime=next_planned_date)
+            except RecurringStreamOccurence.DoesNotExist:
+                occurence = RecurringStreamOccurence(diffusion=diffusion, datetime=next_planned_date)
+            if occurence.id and Absence.objects.filter(
+                    emission=diffusion.schedule.emission, datetime=next_planned_date).exists():
+                occurence.delete()
+            elif not occurence.id:
+                occurence.save()
diff --git a/nonstop/migrations/0024_recurringstreamoccurence.py b/nonstop/migrations/0024_recurringstreamoccurence.py
new file mode 100644 (file)
index 0000000..3e6999b
--- /dev/null
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.29 on 2020-05-21 09:00
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('nonstop', '0023_recurringstreamdiffusion'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='RecurringStreamOccurence',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('datetime', models.DateTimeField(db_index=True, verbose_name='Date/time')),
+                ('diffusion', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='nonstop.RecurringStreamDiffusion')),
+            ],
+        ),
+    ]
index 013154c9214d560db92e04106c3b333f70674bc0..a9013fc7daf44658e13c3b1fda256f3a9270325e 100644 (file)
@@ -282,6 +282,14 @@ class RecurringStreamDiffusion(models.Model):
     stream = models.ForeignKey(Stream)
     is_active = models.BooleanField('Active', default=True)
 
+    def __str__(self):
+        return _('Recurring Stream for %s') % self.schedule
+
+
+class RecurringStreamOccurence(models.Model):
+    diffusion = models.ForeignKey(RecurringStreamDiffusion, on_delete=models.CASCADE)
+    datetime = models.DateTimeField(_('Date/time'), db_index=True)
+
 
 @receiver(post_delete)
 def remove_soundfile(sender, instance=None, **kwargs):