Skip to content

Commit

Permalink
Only allow each slide to be advanced by next/previous once per auto_a…
Browse files Browse the repository at this point in the history
…dvance_duration

This keeps slides from being advanced erratically when multiple displays are displaying the same auto-advancing slide deck.
  • Loading branch information
sheepman4267 committed Apr 17, 2024
1 parent e207abc commit 2c4bc2c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 4.2.6 on 2024-04-17 09:32

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
("slides", "0019_alter_display_current_slide"),
]

operations = [
migrations.AddField(
model_name="display",
name="previous_slide",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="+",
to="slides.slide",
),
),
migrations.AddField(
model_name="display",
name="slide_changed_at",
field=models.DateTimeField(auto_now=True),
),
]
9 changes: 9 additions & 0 deletions OpenShow/slides/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Display(models.Model): # A set of characteristics used to modify slide ap
pixel_width = models.IntegerField(default=1920)
pixel_height = models.IntegerField(default=1080)
custom_css = models.TextField(null=True, blank=True)
slide_changed_at = models.DateTimeField(auto_now=True)
current_show = models.ForeignKey(
to='Show',
null=True,
Expand All @@ -32,6 +33,13 @@ class Display(models.Model): # A set of characteristics used to modify slide ap
blank=True,
on_delete=models.SET_NULL,
)
previous_slide = models.ForeignKey(
to='Slide',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
current_theme = models.ForeignKey(
to='Theme',
null=True,
Expand Down Expand Up @@ -339,6 +347,7 @@ def send_to_display(self, displays:Iterable, show) -> None:
"""
for display in displays:
slide_theme = self.get_theme()
display.previous_slide = display.current_slide # do the slide shuffle
display.current_slide = self
display.current_show = show
if display.current_theme != slide_theme:
Expand Down
26 changes: 21 additions & 5 deletions OpenShow/slides/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from datetime import timedelta

from django.shortcuts import render, get_object_or_404, HttpResponseRedirect, reverse
from django.utils import timezone
from django.views.generic import DetailView, FormView, ListView, UpdateView
from django.utils.decorators import method_decorator
from django.views.decorators.clickjacking import xframe_options_sameorigin
Expand All @@ -13,6 +16,7 @@
from django_eventstream import send_event
from django.shortcuts import HttpResponse


# This approach doesn't work well due to many extra characters being inserted into the HTML/CSS.
# def send_slide_to_display(request, slide, display):
# slide_template = loader.get_template('slides/slide.html')
Expand Down Expand Up @@ -74,6 +78,14 @@ def form_valid(self, form):
show = Show.objects.get(pk=form.cleaned_data['show_pk'])
if form.cleaned_data['direction']:
display = show.displays.all().first()
if display.previous_slide and display.current_slide.auto_advance:
if timezone.now() - \
display.slide_changed_at < \
timedelta(seconds=display.previous_slide.auto_advance_duration):
# Abort and continue silently if we're getting a "next slide" directive and
# the previous slide's auto_advance_duration has not passed
# Manually selecting a different slide will override this.
return HttpResponseRedirect(reverse('show', kwargs={'pk': show.pk}))
current_slide = display.current_slide
slide = current_slide.next(form.cleaned_data['direction'])
next_segment = None
Expand All @@ -85,17 +97,21 @@ def form_valid(self, form):
if current_segment.slides.first() and form.cleaned_data['direction'] == 'forward':
slide = current_segment.slides.first()
elif form.cleaned_data['direction'] == 'reverse':
slide = current_segment.next_with_slides(form.cleaned_data['direction']).get_last_slide()
slide = current_segment.next_with_slides(
form.cleaned_data['direction']).get_last_slide()
elif form.cleaned_data['direction'] == 'forward':
slide = current_segment.next_with_slides(form.cleaned_data['direction']).get_first_slide()
slide = current_segment.next_with_slides(
form.cleaned_data['direction']).get_first_slide()
else: # ..if current_slide.segment
current_segment = current_slide.segment
if form.cleaned_data['direction'] == 'reverse' and current_segment.included_deck:
slide = current_segment.included_deck.slides.last()
elif form.cleaned_data['direction'] == 'reverse':
slide = current_segment.next_with_slides(form.cleaned_data['direction']).get_last_slide()
slide = current_segment.next_with_slides(
form.cleaned_data['direction']).get_last_slide()
elif form.cleaned_data['direction'] == 'forward':
slide = current_segment.next_with_slides(form.cleaned_data['direction']).get_first_slide()
slide = current_segment.next_with_slides(
form.cleaned_data['direction']).get_first_slide()
except AttributeError:
slide = current_slide
elif show.advance_loop:
Expand All @@ -114,7 +130,7 @@ def form_valid(self, form):
else:
slide = Slide.objects.get(pk=form.cleaned_data['slide_pk'])
slide.send_to_display(show.displays.all(), show=show)
return HttpResponseRedirect(reverse('show', kwargs={'pk':show.pk}))
return HttpResponseRedirect(reverse('show', kwargs={'pk': show.pk}))


class AdvanceModeView(UpdateView):
Expand Down

0 comments on commit 2c4bc2c

Please sign in to comment.