def prm_export(request): #TODO: investigate using django's builtin serialization for XML company, partner, user = prm_worthy(request) file_format = request.REQUEST.get('file_format', 'csv') fields = retrieve_fields(ContactRecord) _, _, records = get_records_from_request(request) if file_format == 'xml': root = etree.Element("contact_records") for record in records: xml_record = etree.SubElement(root, "record") for field in fields: xml = etree.SubElement(xml_record, field) value = getattr(record, field, '') if hasattr(value, 'all'): value = ', '.join([val.name for val in value.all() if val]) xml.text = contact_record_val_to_str(value) response = HttpResponse(etree.tostring(root, pretty_print=True, xml_declaration=True), mimetype='application/force-download') elif file_format == 'printer_friendly': ctx = { 'company': company, 'fields': fields, 'partner': partner, 'records': records, } return render_to_response('mypartners/printer_friendly.html', ctx, RequestContext(request)) # CSV/XLS else: response = HttpResponse(content_type='text/csv') writer = unicodecsv.writer(response, encoding='utf-8') writer.writerow(fields) for record in records: values = [getattr(record, field, '') for field in fields] values = [ contact_record_val_to_str(v) if not hasattr(v, 'all') else ', '.join([val.name for val in v.all() if val]) for v in values ] # Remove the HTML and reformat. values = [strip_tags(v) for v in values] # replaces multiple occurences of space by a single sapce. values = [' '.join(filter(bool, v.split(' '))) for v in values] values = [re.sub('\s+\n\s+', '\n', v) for v in values] writer.writerow(values) response['Content-Disposition'] = 'attachment; ' \ 'filename="company_record_report".%s' \ % file_format return response
def prm_export(request): # TODO: investigate using django's builtin serialization for XML company, partner, user = prm_worthy(request) file_format = request.REQUEST.get("file_format", "csv") fields = retrieve_fields(ContactRecord) _, _, records = get_records_from_request(request) if file_format == "xml": root = etree.Element("contact_records") for record in records: xml_record = etree.SubElement(root, "record") for field in fields: xml = etree.SubElement(xml_record, field) value = getattr(record, field, "") if hasattr(value, "all"): value = ", ".join([val.name for val in value.all() if val]) xml.text = contact_record_val_to_str(value) response = HttpResponse( etree.tostring(root, pretty_print=True, xml_declaration=True), mimetype="application/force-download" ) elif file_format == "printer_friendly": ctx = {"company": company, "fields": fields, "partner": partner, "records": records} return render_to_response("mypartners/printer_friendly.html", ctx, RequestContext(request)) # CSV/XLS else: response = HttpResponse(content_type="text/csv") writer = unicodecsv.writer(response, encoding="utf-8") writer.writerow(fields) for record in records: values = [getattr(record, field, "") for field in fields] values = [ contact_record_val_to_str(v) if not hasattr(v, "all") else ", ".join([val.name for val in v.all() if val]) for v in values ] # Remove the HTML and reformat. values = [strip_tags(v) for v in values] # replaces multiple occurences of space by a single sapce. values = [" ".join(filter(bool, v.split(" "))) for v in values] values = [re.sub("\s+\n\s+", "\n", v) for v in values] writer.writerow(values) response["Content-Disposition"] = "attachment; " 'filename="company_record_report".%s' % file_format return response
def get_attr(record, field): return contact_record_val_to_str(getattr(record, field, ''))
def get_attr(record, field): return contact_record_val_to_str(getattr(record, field, ''))