Skip to content

Commit

Permalink
Address ReportView form initial
Browse files Browse the repository at this point in the history
  • Loading branch information
RamezIssac committed Oct 6, 2023
1 parent 51b0772 commit 660a577
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 18 deletions.
1 change: 1 addition & 0 deletions demo_proj/demo_app/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
("chartjs-examples", reports.ChartJSExample),
("apexcharts-examples", reports.ProductSalesApexChart),
("custom-export", reports.CustomExportReport),
("form-initial", reports.ReportWithFormInitial),
]


Expand Down
25 changes: 25 additions & 0 deletions demo_proj/demo_app/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,3 +672,28 @@ def export_csv(self, report_data):

export_csv.title = _("My Custom CSV export Title")
export_csv.css_class = "btn btn-primary"


class ReportWithFormInitial(ReportView):
report_title = _("Report With Form Initial")
report_model = SalesTransaction
date_field = "date"
group_by = "product"

columns = [
"name",
ComputationField.create(
method=Sum,
field="value",
name="value__sum",
verbose_name="Total sold $",
is_summable=True,
),
]

def get_initial(self):
from .models import Client

initial = super().get_initial()
initial["client_id"] = [Client.objects.first().pk, Client.objects.last().pk]
return initial
14 changes: 14 additions & 0 deletions demo_proj/templates/menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,18 @@
</a>
</li>

<li class="nav-item">
<a class="nav-link" href="{% url "form-initial" %}">
<span class="nav-link-icon d-md-none d-lg-inline-block"><!-- Download SVG icon from http://tabler-icons.io/i/home -->
<svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24"
stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path
d="M5 12l-2 0l9 -9l9 9l-2 0"/><path d="M5 12v7a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-7"/><path
d="M9 21v-6a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v6"/></svg>
</span>
<span class="nav-link-title">
Form initial
</span>
</a>
</li>
</ul>
5 changes: 3 additions & 2 deletions slick_reporting/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,14 @@ def report_form_factory(
fields["start_date"] = forms.DateTimeField(
required=False,
label=_("From date"),
initial=initial.get("start_date", app_settings.SLICK_REPORTING_DEFAULT_START_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", app_settings.SLICK_REPORTING_DEFAULT_END_DATE),
initial=initial.get("end_date", "") or app_settings.SLICK_REPORTING_SETTINGS["DEFAULT_END_DATE_TIME"],
widget=forms.DateTimeInput(attrs={"autocomplete": "off"}),
)

Expand All @@ -303,6 +303,7 @@ def report_form_factory(
field_attrs = foreign_key_widget_func(f_field)
if name in required:
field_attrs["required"] = True
field_attrs["initial"] = initial.get(name, "")
fields[name] = f_field.formfield(**field_attrs)

if crosstab_model:
Expand Down
4 changes: 2 additions & 2 deletions slick_reporting/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,8 @@ def get_full_response(
}
return data

def get_chart_settings(self, chart_settings=None, default_chart_title=None, chart_engine=None):
@staticmethod
def get_chart_settings(chart_settings=None, default_chart_title=None, chart_engine=None):
"""
Ensure the sane settings are passed to the front end.
"""
Expand All @@ -943,7 +944,6 @@ def get_chart_settings(self, chart_settings=None, default_chart_title=None, char
for i, chart in enumerate(chart_settings):
if type(chart) is Chart:
chart = chart.to_dict()

chart["id"] = chart.get("id", f"{i}")
chart_type = chart.get("type", "line")
if chart_type == "column" and SLICK_REPORTING_DEFAULT_CHARTS_ENGINE == "chartsjs":
Expand Down
34 changes: 20 additions & 14 deletions slick_reporting/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,12 @@ def get_form_kwargs(self):
}
)
elif self.request.method in ("GET", "PUT"):
# elif self.request.GET:
kwargs.update(
{
"data": self.request.GET,
}
)
if self.request.GET or self.request.headers.get("x-requested-with") == "XMLHttpRequest":
kwargs.update(
{
"data": self.request.GET,
}
)
return kwargs

def get_crosstab_ids(self):
Expand Down Expand Up @@ -392,8 +392,11 @@ def get_chart_settings(self, generator=None):
"""
Ensure the sane settings are passed to the front end.
"""
generator = generator or self.get_report_generator()
return generator.get_chart_settings(self.chart_settings or [], self.report_title, self.chart_engine)
return self.report_generator_class.get_chart_settings(
chart_settings=self.chart_settings or [],
default_chart_title=self.report_title,
chart_engine=self.chart_engine,
)

@classmethod
def get_queryset(cls):
Expand All @@ -416,10 +419,14 @@ def get_report_slug(cls):
return cls.report_slug or cls.__name__.lower()

def get_initial(self):
return {
"start_date": SLICK_REPORTING_SETTINGS["DEFAULT_START_DATE_TIME"],
"end_date": SLICK_REPORTING_SETTINGS["DEFAULT_END_DATE_TIME"],
}
initial = self.initial.copy()
initial.update(
{
"start_date": SLICK_REPORTING_SETTINGS["DEFAULT_START_DATE_TIME"],
"end_date": SLICK_REPORTING_SETTINGS["DEFAULT_END_DATE_TIME"],
}
)
return initial

def get_form_crispy_helper(self):
"""
Expand All @@ -438,8 +445,7 @@ def get_context_data(self, **kwargs):
context["report"] = self

if not (self.request.POST or self.request.GET):
# initialize empty form with initials if the no data is in the get or the post
context["form"] = self.get_form_class()()
context["form"] = self.get_form_class()(**self.get_form_kwargs())
return context

def form_invalid(self, form):
Expand Down

0 comments on commit 660a577

Please sign in to comment.