예제 #1
0
파일: api.py 프로젝트: Alotor/intranet
 def report(self, request, report=None, queryset=None, template_name='autoreports/autoreports_form.html', extra_context=None):
     export_report = (request.GET.get('__report_csv', None) and 'csv') or (request.GET.get('__report_excel', None) and 'excel')
     data = request.GET or None
     fields_form_filter, fields_form_display = self.get_fields_of_form(report)
     form_filter = self.get_report_form_filter(data, fields_form_filter)
     form_display = self.get_report_form_display(data, fields_form_display)
     are_valid = False
     if data:
         are_valid = form_display.is_valid() and form_filter.is_valid()
     if export_report and are_valid:
         return self.get_report(request, queryset, form_filter, form_display, report, export_report)
     extra_context = extra_context or {}
     _adavanced_filters = extra_context.get('_adavanced_filters', None)
     django_query = None
     if are_valid and _adavanced_filters:
         django_query = self.get_django_query(_adavanced_filters)
     context = {'form_filter': form_filter,
                'form_display': form_display,
                'template_base': getattr(settings, 'AUTOREPORTS_BASE_TEMPLATE', 'base.html'),
                'export_formats': get_available_formats(),
                'api': self,
                'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,
                'report': report,
                'django_query': django_query,
               }
     context.update(extra_context)
     return render_to_response(template_name,
                              context,
                              context_instance=RequestContext(request))
예제 #2
0
def reports_view(request,
                 app_name,
                 model_name,
                 fields=None,
                 list_headers=None,
                 ordering=None,
                 filters=Q(),
                 api=None,
                 queryset=None,
                 report_to='csv',
                 report=None,
                 separated_field=SEPARATED_FIELD,
                 pre_procession_lite=False):
    class_model = models.get_model(app_name, model_name)
    request = pre_procession_request(request, class_model, pre_procession_lite)
    list_fields = fields
    formats = get_available_formats()

    if not list_fields:
        api = api or site._registry.get(class_model, None)
        if api:
            list_fields = api.list_display
            set_fields = set(list_fields) - set(EXCLUDE_FIELDS)
            list_fields = list(set_fields)
        else:
            list_fields = ['__unicode__']
            list_headers = [_('Object')]

    list_headers = list_headers
    if not list_headers:
        list_headers = translate_fields(list_fields, class_model)
    name = "%s-%s.%s" % (app_name, model_name,
                         formats[report_to]['file_extension'])

    object_list = queryset and queryset.filter(
        filters) or class_model.objects.filter(filters)

    filters, object_list = filtering_from_request(request,
                                                  object_list,
                                                  report=report)

    if ordering:
        object_list = object_list.order_by(*ordering)

    response = csv_head(name, list_headers)
    csv_body(response,
             class_model,
             object_list,
             list_fields,
             separated_field=separated_field,
             api=api)
    if report_to == 'excel':
        convert_to_excel(response)
    return response
예제 #3
0
 def report(self,
            request,
            report=None,
            queryset=None,
            template_name='autoreports/autoreports_form.html',
            extra_context=None):
     export_report = (request.GET.get('__report_csv', None) and 'csv') or (
         request.GET.get('__report_excel', None) and 'excel')
     data = request.GET or None
     fields_form_filter, fields_form_display = self.get_fields_of_form(
         report)
     form_filter = self.get_report_form_filter(data, fields_form_filter)
     form_display = self.get_report_form_display(data, fields_form_display)
     are_valid = False
     if data:
         are_valid = form_display.is_valid() and form_filter.is_valid()
     if export_report and are_valid:
         return self.get_report(request, queryset, form_filter,
                                form_display, report, export_report)
     extra_context = extra_context or {}
     _adavanced_filters = extra_context.get('_adavanced_filters', None)
     django_query = None
     if are_valid and _adavanced_filters:
         django_query = self.get_django_query(_adavanced_filters)
     context = {
         'form_filter':
         form_filter,
         'form_display':
         form_display,
         'template_base':
         getattr(settings, 'AUTOREPORTS_BASE_TEMPLATE', 'base.html'),
         'export_formats':
         get_available_formats(),
         'api':
         self,
         'ADMIN_MEDIA_PREFIX':
         settings.ADMIN_MEDIA_PREFIX,
         'report':
         report,
         'django_query':
         django_query,
     }
     context.update(extra_context)
     return render_to_response(template_name,
                               context,
                               context_instance=RequestContext(request))
예제 #4
0
def reports_view(request, app_name, model_name, fields=None,
                 list_headers=None, ordering=None, filters=Q(),
                 api=None, queryset=None,
                 report_to='csv',
                 report=None,
                 separated_field=SEPARATED_FIELD,
                 pre_procession_lite=False):
    class_model = models.get_model(app_name, model_name)
    request = pre_procession_request(request, class_model, pre_procession_lite)
    list_fields = fields
    formats = get_available_formats()

    if not list_fields:
        api = api or site._registry.get(class_model, None)
        if api:
            list_fields = api.list_display
            set_fields = set(list_fields) - set(EXCLUDE_FIELDS)
            list_fields = list(set_fields)
        else:
            list_fields = ['__unicode__']
            list_headers = [_('Object')]

    list_headers = list_headers
    if not list_headers:
        list_headers = translate_fields(list_fields, class_model)
    name = "%s-%s.%s" % (app_name, model_name, formats[report_to]['file_extension'])

    object_list = queryset and queryset.filter(filters) or class_model.objects.filter(filters)

    filters, object_list = filtering_from_request(request, object_list, report=report)

    if ordering:
        object_list = object_list.order_by(*ordering)

    response = csv_head(name, list_headers)
    csv_body(response, class_model, object_list, list_fields,
             separated_field=separated_field, api=api)
    if report_to == 'excel':
        convert_to_excel(response)
    return response