class ShuupReportForm(forms.Form): report = forms.CharField(widget=HiddenInput) writer = forms.ChoiceField( label=_("Output Format"), initial="html", choices=[(name, name.title()) for name in sorted(get_writer_names())], help_text=_("The format to show the report results.")) force_download = forms.BooleanField( required=False, label=_("Download"), help_text=_("Check this to download the report.")) def __init__(self, *args, **kwargs): self.request = kwargs.pop('request') super(ShuupReportForm, self).__init__(*args, **kwargs) def get_report_instance(self, request=None): """ :rtype: shuup.reports.reporter.base.ShuupReportBase """ from shuup.reports.report import get_report_class data = self.cleaned_data report_class = get_report_class(data["report"], request) writer_name = data.pop("writer") if request: data.update({"request": request}) report = report_class(writer_name=writer_name, **data) return report
class BaseReportForm(forms.Form): report = forms.CharField(widget=HiddenInput) shop = forms.ChoiceField(label=_("Shop"), help_text=_("Filter report results by shop.")) date_range = EnumField(DateRangeChoices).formfield( form_class=ChoiceField, label=_("Date Range"), initial=DateRangeChoices.RUNNING_WEEK, help_text=_("Filter report results by a date range.")) start_date = DateField(label=_("Start Date"), required=False, help_text=_("For a custom date range.")) end_date = DateField(label=_("End Date"), required=False, help_text=_("For a custom date range.")) writer = forms.ChoiceField( label=_("Output Format"), initial="html", choices=[(name, name.title()) for name in sorted(get_writer_names())], help_text=_("The format to show the report results.")) force_download = forms.BooleanField( required=False, label=_("Download"), help_text=_("Check this to download the report.")) def __init__(self, *args, **kwargs): self.request = kwargs.pop('request') super(BaseReportForm, self).__init__(*args, **kwargs) self.fields["shop"].choices = [ (shop.pk, shop.name) for shop in Shop.objects.get_for_user(self.request.user) ] def clean(self): data = self.cleaned_data if data.get("date_range") == DateRangeChoices.CUSTOM: try: data["date_range"] = parse_date_range( (data["start_date"], data["end_date"])) except Exception as exc: self.add_error("__all__", force_text(exc)) return data def get_report_instance(self): """ :rtype: shuup.reports.reporter.base.ShuupReportBase """ from shuup.reports.report import get_report_class data = self.cleaned_data writer_name = data.pop("writer") report_class = get_report_class(data["report"]) report = report_class(writer_name=writer_name, **data) return report