def render_to_response(self): response = self.response_class( getattr(self.get_dataset(), self.get_format()), content_type=mimetype_map.get(self.get_format(), "application/octet-stream"), ) response["Content-Disposition"] = 'attachment; filename="%s.%s"' % (self.get_filename(), self.get_format()) return response
def export_reporting(request, location_code=None): if location_code is None: location_code = settings.COUNTRY location = get_object_or_404(Location, code=location_code) queryset = ProductReport.objects.filter(supply_point__location__in=location.get_descendants(include_self=True))\ .select_related("supply_point__name", "supply_point__location__parent__name", "supply_point__location__parent__parent__name", "product__name", "report_type__name", "message__text").order_by('report_date') response = HttpResponse(mimetype=mimetype_map.get(format, 'application/octet-stream')) response['Content-Disposition'] = 'attachment; filename=reporting.xls' writer = csv.writer(response) writer.writerow(['ID', 'Location Grandparent', 'Location Parent', 'Facility', 'Commodity', 'Report Type', 'Quantity', 'Date', 'Message']) for q in queryset: parent = q.supply_point.location.parent.name if q.supply_point.location.parent else None grandparent = q.supply_point.location.parent.parent.name if q.supply_point.location.parent.parent else None message = q.message.text if q.message else None writer.writerow([q.id, grandparent, parent, q.supply_point.name, q.product.name, q.report_type.name, q.quantity, q.report_date, message]) return response
def export_reporting(request, location_code=None): if location_code is None: location_code = settings.COUNTRY location = get_object_or_404(Location, code=location_code) queryset = ProductReport.objects.filter(supply_point__location__in=location.get_descendants(include_self=True))\ .select_related("supply_point__name", "supply_point__location__parent__name", "supply_point__location__parent__parent__name", "product__name", "report_type__name", "message__text").order_by('report_date') response = HttpResponse( mimetype=mimetype_map.get(format, 'application/octet-stream')) response['Content-Disposition'] = 'attachment; filename=reporting.xls' writer = csv.UnicodeWriter(response) writer.writerow([ 'ID', 'Location Grandparent', 'Location Parent', 'Facility', 'Commodity', 'Report Type', 'Quantity', 'Date', 'Message' ]) for q in queryset: parent = q.supply_point.location.parent.name if q.supply_point.location.parent else None grandparent = q.supply_point.location.parent.parent.name if q.supply_point.location.parent.parent else None message = q.message.text if q.message else None writer.writerow([ q.id, grandparent, parent, q.supply_point.name, q.product.name, q.report_type.name, q.quantity, q.report_date, message ]) return response
def render_to_response(self): response = self.response_class(getattr(self.get_dataset(), self.get_format()), content_type=mimetype_map.get( self.get_format(), 'application/octet-stream')) response['Content-Disposition'] = ('attachment; filename="%s.%s"' % ( self.get_filename(), self.get_format(), )) return response
def export_stockonhand(request, facility_code, format='xls', filename='stockonhand'): class ProductReportDataset(ModelDataset): class Meta: queryset = ProductReport.objects.filter(supply_point__code=facility_code).order_by('report_date') dataset = getattr(ProductReportDataset(), format) filename = '%s_%s.%s' % (filename, facility_code, format) response = HttpResponse( dataset, mimetype=mimetype_map.get(format, 'application/octet-stream') ) response['Content-Disposition'] = 'attachment; filename=%s' % filename return response
def export_stockonhand(request, facility_code, format='xls', filename='stockonhand'): class ProductReportDataset(ModelDataset): class Meta: queryset = ProductReport.objects.filter(supply_point__code=facility_code).order_by('report_date') dataset = getattr(ProductReportDataset(), format) filename = '%s_%s.%s' % (filename, facility_code, format) response = HttpResponse( dataset, mimetype=mimetype_map.get(format, 'application/octet-stream') ) response['Content-Disposition'] = 'attachment; filename=%s' % filename return response
def export_messagelog(request, format='xls'): class MessageDataSet(ModelDataset): class Meta: # hack to limit the # of messages returns # so that we don't crash the server when the log gets too big # in the long term, should implement asynchronous processing + progress bar queryset = Message.objects.order_by('-date')[:10000] dataset = getattr(MessageDataSet(), format) response = HttpResponse( dataset, mimetype=mimetype_map.get(format, 'application/octet-stream') ) response['Content-Disposition'] = 'attachment; filename=messagelog.xls' return response
def export_messagelog(request, format='xls'): class MessageDataSet(ModelDataset): class Meta: # hack to limit the # of messages returns # so that we don't crash the server when the log gets too big # in the long term, should implement asynchronous processing + progress bar queryset = Message.objects.order_by('-date')[:10000] dataset = getattr(MessageDataSet(), format) response = HttpResponse(dataset, mimetype=mimetype_map.get( format, 'application/octet-stream')) response['Content-Disposition'] = 'attachment; filename=messagelog.xls' return response
def export(request, queryset=None, model=None, headers=None, format='xls', filename='export'): if queryset is None: queryset = model.objects.all() dataset = SimpleDataset(queryset, headers=headers) filename = '%s.%s' % (filename, format) if not hasattr(dataset, format): raise Http404 response = HttpResponse( getattr(dataset, format), mimetype=mimetype_map.get(format, 'application/octet-stream') ) response['Content-Disposition'] = 'attachment; filename=%s' % filename return response
def tablib_export_action(modeladmin, request, queryset, format="xls"): """ Allow the user to download the current filtered list of items :param format: One of the formats supported by tablib (e.g. "xls", "csv", "html", etc.) """ dataset = SimpleDataset(queryset, headers=None) filename = '%s.%s' % ( smart_str(modeladmin.model._meta.verbose_name_plural), format) response = HttpResponse( getattr(dataset, format), mimetype=mimetype_map.get( format, 'application/octet-stream')) response['Content-Disposition'] = 'attachment; filename=%s' % filename return response
def tablib_export_action(modeladmin, request, queryset, format="xls"): """ Allow the user to download the current filtered list of items :param format: One of the formats supported by tablib (e.g. "xls", "csv", "html", etc.) """ dataset = SimpleDataset(queryset, headers=None) filename = '%s.%s' % (smart_str( modeladmin.model._meta.verbose_name_plural), format) response = HttpResponse(getattr(dataset, format), mimetype=mimetype_map.get( format, 'application/octet-stream')) response['Content-Disposition'] = 'attachment; filename=%s' % filename return response
def export(request, queryset=None, model=None, headers=None, format='xls', filename='export'): if queryset is None: queryset = model.objects.all() dataset = SimpleDataset(queryset, headers=headers) filename = '%s.%s' % (filename, format) if not hasattr(dataset, format): raise Http404 response = HttpResponse(getattr(dataset, format), mimetype=mimetype_map.get( format, 'application/octet-stream')) response['Content-Disposition'] = 'attachment; filename=%s' % filename return response
def tablib_export_action(modeladmin, request, queryset, file_type="xls"): """ Allow the user to download the current filtered list of items :param format: One of the formats supported by tablib (e.g. "xls", "csv", "html", etc.) """ dataset = SimpleDataset(queryset, headers=None) filename = '%s.%s' % ( smart_str(modeladmin.model._meta.verbose_name_plural), file_type) response_kwargs = {} key = 'content_type' if get_version().split('.')[1] > 6 else 'mimetype' response_kwargs[key] = mimetype_map.get( file_type, 'application/octet-stream') response = HttpResponse(getattr(dataset, file_type), **response_kwargs) response['Content-Disposition'] = 'attachment; filename=%s' % filename return response