From e11d3c69bc1dd683e091daae6ab85567d6397ce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Valenta?= <git@imaniti.org> Date: Tue, 24 Oct 2023 22:18:02 +0200 Subject: [PATCH] add reset timer function --- static_src/timer.js | 18 +++++++++-- timer/consumers.py | 18 +++++++---- ...5_ongoingtimer_initial_minutes_and_more.py | 31 +++++++++++++++++++ timer/models.py | 18 ++++++++++- timer/templates/timer/edit_timer.html | 6 ++++ timer/views.py | 2 ++ 6 files changed, 84 insertions(+), 9 deletions(-) create mode 100644 timer/migrations/0005_ongoingtimer_initial_minutes_and_more.py diff --git a/static_src/timer.js b/static_src/timer.js index 31525f5..784639b 100644 --- a/static_src/timer.js +++ b/static_src/timer.js @@ -5,11 +5,11 @@ import alertify from "alertifyjs"; import "alertifyjs/build/css/alertify.css"; const disableInputs = () => { - $("#pause_play,#minutes,#seconds,#update_time").prop("disabled", true) + $("#pause_play,#minutes,#seconds,#update_time,#reset_time").prop("disabled", true) } const enableInputs = () => { - $("#pause_play,#minutes,#seconds,#update_time").prop("disabled", false) + $("#pause_play,#minutes,#seconds,#update_time,#reset_time").prop("disabled", false) } const updateTimeText = () => { @@ -246,6 +246,20 @@ $(window).ready( } ) + $("#reset_time").on( + "click", + (event) => { + disableInputs() + + window.timer.pause() + + timerSocket.send(JSON.stringify({ + "reset": true, + "is_running": false + })) + } + ) + // --- END Controls --- } diff --git a/timer/consumers.py b/timer/consumers.py index 0aac79d..a333338 100644 --- a/timer/consumers.py +++ b/timer/consumers.py @@ -12,8 +12,6 @@ running_timer_threads = [] def tick_timer(timer, iteration: int, total_seconds: int) -> None: - print("ticking", timer) - second_compensation = 0 start_time = timeit.default_timer() @@ -24,8 +22,6 @@ def tick_timer(timer, iteration: int, total_seconds: int) -> None: second_compensation = end_time - start_time while not timer.is_running: - print("waiting to run ...") - time.sleep(0.05) timer.refresh_from_db() @@ -154,8 +150,18 @@ class TimerConsumer(AsyncWebsocketConsumer): await sync_to_async(self.timer.save)() if "time" in json_data: - self.timer.minutes = json_data["time"]["minutes"] - self.timer.seconds = json_data["time"]["seconds"] + # Don't save here in case there is a tick thread running + + self.timer.minutes = self.timer.initial_minutes = json_data["time"]["minutes"] + self.timer.seconds = self.timer.initial_seconds = json_data["time"]["seconds"] + + reset_timer = True + + if "reset" in json_data: + # Don't save here in case there is a tick thread running + + self.timer.minutes = self.timer.initial_minutes + self.timer.seconds = self.timer.initial_seconds reset_timer = True diff --git a/timer/migrations/0005_ongoingtimer_initial_minutes_and_more.py b/timer/migrations/0005_ongoingtimer_initial_minutes_and_more.py new file mode 100644 index 0000000..6e9bc3f --- /dev/null +++ b/timer/migrations/0005_ongoingtimer_initial_minutes_and_more.py @@ -0,0 +1,31 @@ +# Generated by Django 4.1.5 on 2023-10-24 20:06 + +import django.core.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('timer', '0004_ongoingtimer_is_running_ongoingtimer_iteration'), + ] + + operations = [ + migrations.AddField( + model_name='ongoingtimer', + name='initial_minutes', + field=models.IntegerField(default=0, validators=[django.core.validators.MinValueValidator(limit_value=0), django.core.validators.MaxValueValidator(limit_value=60)], verbose_name='PĹŻvodnĂ Minuty'), + preserve_default=False, + ), + migrations.AddField( + model_name='ongoingtimer', + name='initial_seconds', + field=models.IntegerField(default=0, validators=[django.core.validators.MinValueValidator(limit_value=0), django.core.validators.MaxValueValidator(limit_value=60)], verbose_name='PĹŻvodnĂ sekundy'), + preserve_default=False, + ), + migrations.AlterField( + model_name='ongoingtimer', + name='seconds', + field=models.IntegerField(validators=[django.core.validators.MinValueValidator(limit_value=0), django.core.validators.MaxValueValidator(limit_value=60)], verbose_name='Sekundy'), + ), + ] diff --git a/timer/models.py b/timer/models.py index a715043..a3b45aa 100644 --- a/timer/models.py +++ b/timer/models.py @@ -15,6 +15,22 @@ class OngoingTimer(models.Model): validators=[MinLengthValidator(limit_value=1)], ) + initial_minutes = models.IntegerField( + verbose_name="PĹŻvodnĂ Minuty", + validators=[ + MinValueValidator(limit_value=0), + MaxValueValidator(limit_value=60), + ], + ) + + initial_seconds = models.IntegerField( + verbose_name="PĹŻvodnĂ sekundy", + validators=[ + MinValueValidator(limit_value=0), + MaxValueValidator(limit_value=60), + ], + ) + minutes = models.IntegerField( verbose_name="Minuty", validators=[ @@ -24,7 +40,7 @@ class OngoingTimer(models.Model): ) seconds = models.IntegerField( - verbose_name="Minuty", + verbose_name="Sekundy", validators=[ MinValueValidator(limit_value=0), MaxValueValidator(limit_value=60), diff --git a/timer/templates/timer/edit_timer.html b/timer/templates/timer/edit_timer.html index d2f0add..f962499 100644 --- a/timer/templates/timer/edit_timer.html +++ b/timer/templates/timer/edit_timer.html @@ -87,6 +87,12 @@ > <div class="btn__body">Aktualizovat ÄŤas</div> </button> + <button + id="reset_time" + class="btn w-64" + > + <div class="btn__body">Resetovat ÄŤas</div> + </button> </div> <a diff --git a/timer/views.py b/timer/views.py index 252f802..26b74f4 100644 --- a/timer/views.py +++ b/timer/views.py @@ -20,6 +20,8 @@ def create(request): if form.is_valid(): timer = OngoingTimer( + initial_minutes=form.cleaned_data["minutes"], + initial_seconds=form.cleaned_data["seconds"], minutes=form.cleaned_data["minutes"], seconds=form.cleaned_data["seconds"], name=form.cleaned_data["name"], -- GitLab