Skip to content

Commit

Permalink
fix time widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Rieven committed Jan 9, 2025
1 parent 5b332b2 commit 8b012ce
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion rocky/reports/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ReportScheduleStartDateForm(BaseRockyForm):

start_time = forms.TimeField(
label=_("Start time (UTC)"),
widget=forms.TimeInput(format="%H:%M", attrs={"form": "generate_report"}),
widget=forms.TimeInput(format="%H:%M", attrs={"type": "time"}),
initial=lambda: datetime.now(tz=timezone.utc).time(),
required=True,
input_formats=["%H:%M"],
Expand Down
50 changes: 28 additions & 22 deletions rocky/reports/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from django.urls import reverse
from django.utils.http import urlencode
from django.utils.translation import gettext_lazy as _
from django.views.generic import TemplateView
from django.views.generic import FormView, TemplateView
from django_weasyprint import WeasyTemplateResponseMixin
from katalogus.client import Boefje, KATalogus, KATalogusError, Plugin
from pydantic import RootModel, TypeAdapter
Expand Down Expand Up @@ -520,8 +520,9 @@ def get_context_data(self, **kwargs):
return context


class SaveReportView(BaseReportView, SchedulerView, TemplateView):
class SaveReportView(BaseReportView, SchedulerView, FormView):
task_type = "report"
form_class = ReportScheduleStartDateForm

def get_parent_report_type(self):
if self.report_type is not None:
Expand All @@ -530,31 +531,36 @@ def get_parent_report_type(self):
return ConcatenatedReport.id
return self.report_type

def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
form = ReportScheduleStartDateForm(request.POST)
if form.is_valid():
start_datetime = form.cleaned_data["start_datetime"]
recurrence = form.cleaned_data["recurrence"]

schedule = (
self.convert_recurrence_to_cron_expressions(recurrence, start_datetime)
if recurrence is not None and recurrence != "once"
else None
)
def form_invalid(self, form):
"""
We need to overwrite this as FormView renders invalid forms with a get request,
we need to adapt it using Postredirect, returning invalid form.
"""

parent_report_type = self.get_parent_report_type()
return PostRedirect(self.get_current())

parent_report_name_format = request.POST.get("parent_report_name_format", "")
subreport_name_format = request.POST.get("subreport_name_format")
def form_valid(self, form):
start_datetime = form.cleaned_data["start_datetime"]
recurrence = form.cleaned_data["recurrence"]

report_recipe = self.create_report_recipe(
parent_report_name_format, subreport_name_format, parent_report_type, schedule
)
schedule = (
self.convert_recurrence_to_cron_expressions(recurrence, start_datetime)
if recurrence is not None and recurrence != "once"
else None
)

self.create_report_schedule(report_recipe, start_datetime)
parent_report_type = self.get_parent_report_type()

return redirect(reverse("scheduled_reports", kwargs={"organization_code": self.organization.code}))
return super().get(request, *args, **kwargs)
parent_report_name_format = self.request.POST.get("parent_report_name_format", "")
subreport_name_format = self.request.POST.get("subreport_name_format")

report_recipe = self.create_report_recipe(
parent_report_name_format, subreport_name_format, parent_report_type, schedule
)

self.create_report_schedule(report_recipe, start_datetime)

return redirect(reverse("scheduled_reports", kwargs={"organization_code": self.organization.code}))


class ViewReportView(ObservedAtMixin, OrganizationView, TemplateView):
Expand Down

0 comments on commit 8b012ce

Please sign in to comment.