Exemplo n.º 1
0
def get_filter(request):
    """When a user selects a new report type, get the proper filter form and
    render it as an html fragment to display for user"""

    try:
        report = reports.report_class(request.GET.get("report_type"))
    except ValueError:
        raise Http404("Unknown report type")

    template = get_template("_form_horizontal.html")
    content = template.render({'form': report.filter_class().form})
    formats = [f for f in models.SavedReport.FORMATS if f[0] in report.formats]
    return JsonResponse({'errors': [], 'filter': content, 'formats': formats})
Exemplo n.º 2
0
def process_form_post(post_data, user, instance):
    """Handle validing form and filter form for views"""

    form = ReportForm(post_data, instance=instance)
    form.is_valid()
    notes_formset = ReportNoteFormSet(post_data, instance=instance)
    notes_formset.is_valid()
    base_opts = {'report_id': post_data.get("report_id")}
    base_opts.update(form.cleaned_data)
    filter_form = None
    all_valid = False
    report = None
    if not (form.is_valid()
            and notes_formset.is_valid()) and "report_type" not in form.errors:
        # something wrong with base form, but we know what report_type we're trying to produce
        # so validate that form too
        notes = notes_formset.cleaned_data if notes_formset.is_valid() else []
        ReportClass = reports.report_class(form.cleaned_data['report_type'])
        report = ReportClass(base_opts=base_opts,
                             report_opts=post_data,
                             notes=notes,
                             user=user)
        filter_form = report.get_filter_form()
        filter_form.is_valid()
    elif form.is_valid():
        ReportClass = reports.report_class(form.cleaned_data['report_type'])
        notes = notes_formset.cleaned_data if notes_formset.is_valid() else []
        report = ReportClass(base_opts=base_opts,
                             report_opts=post_data,
                             notes=notes,
                             user=user)
        filter_form = report.get_filter_form()
        if filter_form.is_valid() and notes_formset.is_valid():
            all_valid = report.filter_form_valid(filter_form)

    return all_valid, report, form, filter_form, notes_formset
Exemplo n.º 3
0
 def get_report_type_display(self):
     return report_class(self.report_type).name
Exemplo n.º 4
0
 def get_report(self, user=None):
     ReportClass = report_class(self.report_type)
     return ReportClass(base_opts=self.base_opts, report_opts=self.filters)
Exemplo n.º 5
0
 def get_filter_class(self):
     return report_class(self.report_type).filter_class