Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Individual start date (via get_initial()) for report not honored in dashboard widget #114

Open
wherget opened this issue Nov 21, 2024 · 1 comment

Comments

@wherget
Copy link

wherget commented Nov 21, 2024

I have this sort of report:

class SampleYearlyReport(ReportView):
    report_title = "Example"
    report_model = Example
    date_field = "start_date"
    time_series_pattern = "annually"
    time_series_columns = [
        ComputationField.create(Count, "id", "count", "Count")
    ]

    columns = [
        "__time_series__",
    ]

    chart_settings = [
        Chart(
            "Count",
            Chart.BAR,
            data_source=["count"],
            title_source=["count"],
        )
    ]

    def get_initial(self):
        default = super().get_initial()
        default["start_date"] = datetime.datetime(
            datetime.date.today().year - 3, 1, 1, 0, 0, 0
        )
        return default

Where since this is a yearly report, I obviously want it to start a good deal earlier than the default "1st January of this year" (which is fine for other reports). This works fine in a plain old report view, but breaks in a dashboard.

AFAICS, this is due to

def get_start_date(self):
return self.cleaned_data.get("start_date")
def get_end_date(self):
return self.cleaned_data.get("end_date")

As for a dashboard widget, there will be no cleaned_data, since it doesn't take a round trip via the rendered form. Should the ReportForm be trying to get the start_date from initial_data if there is none in cleaned data?

Do you (again) have a good idea where to monkey-patch this in? I'm not yet ready to write a complete custom form (although that will happen in the future, especially to integrate a date picker, but for now...), so ideally I could keep using report_form_factory.

@wherget
Copy link
Author

wherget commented Nov 21, 2024

Well, thanks for rubber ducking, GitHub. My current workaround is this:

    def get_form_class(self):
        base_form_class = super().get_form_class()  # uses report_form_factory
        class PatchedDates(base_form_class):
            def get_start_date(self):
                return self.cleaned_data.get("start_date") or self.initial.get("start_date")

            def get_end_date(self):
                return self.cleaned_data.get("end_date") or self.initial.get("end_date")

        return PatchedDates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant