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 export_all(request): auditEvents = AccessAudit.view("auditcare/by_date_access_events", descending=True, include_docs=True).all() response = HttpResponse() response['Content-Disposition'] = 'attachment; filename="AuditAll.xls"' writer = csv.UnicodeWriter(response) writer.writerow(['User', 'Access Type', 'Date']) for a in auditEvents: writer.writerow([a.user, a.access_type, a.event_date]) return response