mirror of
https://github.com/meeb/tubesync.git
synced 2025-06-24 14:06:36 +00:00
Provide when
for the form and the template
This commit is contained in:
parent
d8eca035ac
commit
4f68552713
@ -20,6 +20,7 @@ from django.utils.text import slugify
|
||||
from django.utils._os import safe_join
|
||||
from django.utils import timezone
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from common.timestamp import timestamp_to_datetime
|
||||
from common.utils import append_uri_params
|
||||
from background_task.models import Task, CompletedTask
|
||||
from .models import Source, Media, MediaServer
|
||||
@ -1012,16 +1013,42 @@ class TaskScheduleView(FormView, SingleObjectMixin):
|
||||
template_name = 'sync/task-schedule.html'
|
||||
form_class = ScheduleTaskForm
|
||||
model = Task
|
||||
errors = dict(
|
||||
invalid_when=_('The type ({}) was incorrect.'),
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.object = None
|
||||
self.schedule = 0
|
||||
self.timestamp = None
|
||||
self.when = None
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
self.object = self.get_object()
|
||||
self.timestamp = kwargs.get('timestamp', '')
|
||||
try:
|
||||
self.timestamp = int(self.timestamp, 10)
|
||||
except (TypeError, ValueError):
|
||||
self.timestamp = None
|
||||
else:
|
||||
try:
|
||||
self.when = timestamp_to_datetime(self.timestamp)
|
||||
except AssertionError:
|
||||
self.when = None
|
||||
if self.when is None:
|
||||
self.when = timezone.now()
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
def get_initial(self):
|
||||
initial = super().get_initial()
|
||||
initial['when'] = self.when
|
||||
return initial
|
||||
|
||||
def get_context_data(self, *args, **kwargs):
|
||||
data = super().get_context_data(*args, **kwargs)
|
||||
data['when'] = self.when
|
||||
return data
|
||||
|
||||
def get_success_url(self):
|
||||
return append_uri_params(
|
||||
reverse_lazy('sync:tasks'),
|
||||
@ -1033,9 +1060,26 @@ class TaskScheduleView(FormView, SingleObjectMixin):
|
||||
|
||||
def form_valid(self, form):
|
||||
max_attempts = getattr(settings, 'MAX_ATTEMPTS', 15)
|
||||
now = timezone.now()
|
||||
when = form.cleaned_data.get('when')
|
||||
|
||||
if not isinstance(when, now.__class__):
|
||||
form.add_error(
|
||||
'when',
|
||||
ValidationError(
|
||||
errors['invalid_when'].format(
|
||||
type(when),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
if form.errors:
|
||||
return super().form_invalid(form)
|
||||
|
||||
self.object.attempts = max_attempts // 2
|
||||
self.object.run_at = timezone.now() + timezone.timedelta(seconds=self.schedule)
|
||||
self.object.run_at = when
|
||||
self.object.save()
|
||||
|
||||
return super().form_valid(form)
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user