Friday 24 June 2016

Two views inside one

It was some thing new for me- implementing two views inside one. In the administrator app in VMS, I used two views for report generation: one to display the form and other one to list the volunteer details. The following is the sample code that use single view that calls another two views.

class ShowReportListView(LoginRequiredMixin, ListView):
    template_name = "administrator/report.html"    
    organization_list = get_organizations_ordered_by_name()
    event_list = get_events_ordered_by_name()
    job_list = get_jobs_ordered_by_title()

    def post(self, request, *args, **kwargs):
        report_list = get_administrator_report(
            self.request.POST['first_name'],
            self.request.POST['last_name'],
            self.request.POST['organization'],
            self.request.POST['event_name'],
            self.request.POST['job_name'],
            self.request.POST['start_date'],
            self.request.POST['end_date'],
        )
        organization = self.request.POST['organization']
        event_name = self.request.POST['event_name']
        total_hours = calculate_total_report_hours(report_list)
        return render(request, 'administrator/report.html',
                      {'report_list': report_list, 
                      'total_hours': total_hours, 
                      'notification': True,
                      'organization_list': self.organization_list,                         
                      'selected_organization': organization,
                      'event_list': self.event_list,                                      
                      'selected_event': event_name, 
                      'job_list': self.job_list})

class GenerateReportView(LoginRequiredMixin, View):

    def get(self, request, *args, **kwargs):
        view = ShowFormView.as_view()
        return view(request, *args,**kwargs)

    def post(self, request, *args, **kwargs):
        view = ShowReportListView.as_view()

GenerateReportView calls ShowFormView that displays the form and ShowReportListView that list the volunteers.


No comments:

Post a Comment