Skip to content

Commit

Permalink
- Group by and crosstab reports do not need date_field set anymore. O…
Browse files Browse the repository at this point in the history
…nly time series do.
  • Loading branch information
RamezIssac committed Oct 13, 2023
1 parent 93fd357 commit b8f8f2c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ All notable changes to this project will be documented in this file.
- Breaking : [Only if you set the templates statics by hand] renamed slick_reporting to ra.hightchart.js and ra.chartjs.js to
erp_framework.highchart.js and erp_framework.chartjs.js respectively
- Fix an issue with Crosstab when there crosstab_compute_remainder = False
- Group by and crosstab reports do not need date_field set anymore. Only time series do.

## [0.7.0]

Expand Down
4 changes: 2 additions & 2 deletions demo_proj/demo_app/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class TotalProductSalesWithCustomForm(TotalProductSales):
class GroupByReport(ReportView):
report_model = SalesTransaction
report_title = _("Group By Report")
date_field = "date"
# date_field = "date"
group_by = "product"

columns = [
Expand Down Expand Up @@ -487,7 +487,7 @@ class CrosstabReport(ReportView):
report_title = _("Cross tab Report")
report_model = SalesTransaction
group_by = "client"
date_field = "date"
# date_field = "date"

columns = [
"name",
Expand Down
30 changes: 16 additions & 14 deletions slick_reporting/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ def report_form_factory(
time_series_selector_default="",
time_series_selector_label=None,
time_series_selector_allow_empty=False,
add_start_date=True,
add_end_date=True,
):
"""
Create a Report Form based on the report_model passed by
Expand Down Expand Up @@ -271,20 +273,20 @@ def report_form_factory(

fkeys_list = []
fields = OrderedDict()

fields["start_date"] = forms.DateTimeField(
required=False,
label=_("From date"),
initial=initial.get("start_date", "") or app_settings.SLICK_REPORTING_SETTINGS["DEFAULT_START_DATE_TIME"],
widget=forms.DateTimeInput(attrs={"autocomplete": "off"}),
)

fields["end_date"] = forms.DateTimeField(
required=False,
label=_("To date"),
initial=initial.get("end_date", "") or app_settings.SLICK_REPORTING_SETTINGS["DEFAULT_END_DATE_TIME"],
widget=forms.DateTimeInput(attrs={"autocomplete": "off"}),
)
if add_start_date:
fields["start_date"] = forms.DateTimeField(
required=False,
label=_("From date"),
initial=initial.get("start_date", "") or app_settings.SLICK_REPORTING_SETTINGS["DEFAULT_START_DATE_TIME"],
widget=forms.DateTimeInput(attrs={"autocomplete": "off"}),
)
if add_end_date:
fields["end_date"] = forms.DateTimeField(
required=False,
label=_("To date"),
initial=initial.get("end_date", "") or app_settings.SLICK_REPORTING_SETTINGS["DEFAULT_END_DATE_TIME"],
widget=forms.DateTimeInput(attrs={"autocomplete": "off"}),
)

if show_time_series_selector:
time_series_choices = list(TIME_SERIES_CHOICES)
Expand Down
15 changes: 9 additions & 6 deletions slick_reporting/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,9 @@ def __init__(
self.time_series_custom_dates = self.time_series_custom_dates or time_series_custom_dates
self.container_class = container_class

if not (self.date_field or (self.start_date_field_name and self.end_date_field_name)) and (
self.time_series_pattern or self.crosstab_field or self.group_by
if (
not (self.date_field or (self.start_date_field_name and self.end_date_field_name))
and self.time_series_pattern
):
raise ImproperlyConfigured(
f"date_field or [start_date_field_name and end_date_field_name] must "
Expand Down Expand Up @@ -451,10 +452,12 @@ def _prepare_report_dependencies(self):
)

q_filters = None
date_filter = {
f"{self.start_date_field_name}__gte": col_data.get("start_date", self.start_date),
f"{self.end_date_field_name}__lt": col_data.get("end_date", self.end_date),
}
date_filter = {}
if self.start_date_field_name:
date_filter[f"{self.start_date_field_name}__gte"] = col_data.get("start_date", self.start_date)
if self.end_date_field_name:
date_filter[f"{self.end_date_field_name}__lt"] = col_data.get("end_date", self.end_date)

date_filter.update(self.kwargs_filters)
if window == "crosstab" or col_data.get("computation_flag", "") == "crosstab":
q_filters, kw_filters = col_data["queryset_filters"]
Expand Down
2 changes: 2 additions & 0 deletions slick_reporting/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ def get_form_class(self):
time_series_selector_choices=self.time_series_selector_choices,
time_series_selector_default=self.time_series_selector_default,
time_series_selector_allow_empty=self.time_series_selector_allow_empty,
add_start_date=self.start_date_field_name or self.date_field,
add_end_date=self.end_date_field_name or self.date_field,
)

def get_form_kwargs(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def load():

def test_missing_date_field(self):
def load():
ReportGenerator(report_model=OrderLine, group_by="product", date_field="")
ReportGenerator(report_model=OrderLine, group_by="product", date_field="", time_series_pattern="monthly")

self.assertRaises(Exception, load)

Expand Down

0 comments on commit b8f8f2c

Please sign in to comment.