Re-use schedules rather than creating new ones

This commit is contained in:
Mouse Reeve 2024-01-02 13:16:53 -08:00
parent f36af42f41
commit 8be9e91d21
2 changed files with 6 additions and 4 deletions

View file

@ -6,7 +6,7 @@ from django.template.response import TemplateResponse
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django.views import View from django.views import View
from django.views.decorators.http import require_POST from django.views.decorators.http import require_POST
from django_celery_beat.models import PeriodicTask from django_celery_beat.models import PeriodicTask, IntervalSchedule
from bookwyrm import forms, models from bookwyrm import forms, models
@ -54,7 +54,7 @@ def schedule_automod_task(request):
return TemplateResponse(request, "settings/automod/rules.html", data) return TemplateResponse(request, "settings/automod/rules.html", data)
with transaction.atomic(): with transaction.atomic():
schedule = form.save(request) schedule, _ = IntervalSchedule.objects.get_or_create(**form.cleaned_data)
PeriodicTask.objects.get_or_create( PeriodicTask.objects.get_or_create(
interval=schedule, interval=schedule,
name="automod-task", name="automod-task",

View file

@ -13,7 +13,7 @@ from django.template.response import TemplateResponse
from django.utils import timezone from django.utils import timezone
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django.views import View from django.views import View
from django_celery_beat.models import PeriodicTask from django_celery_beat.models import PeriodicTask, IntervalSchedule
from csp.decorators import csp_update from csp.decorators import csp_update
@ -79,7 +79,9 @@ class Dashboard(View):
schedule_form = forms.IntervalScheduleForm(request.POST) schedule_form = forms.IntervalScheduleForm(request.POST)
with transaction.atomic(): with transaction.atomic():
schedule = schedule_form.save(request) schedule, _ = IntervalSchedule.objects.get_or_create(
**schedule_form.cleaned_data
)
PeriodicTask.objects.get_or_create( PeriodicTask.objects.get_or_create(
interval=schedule, interval=schedule,
name="check-for-updates", name="check-for-updates",