def kml_export(request, username, id_string): # read the locations from the database owner = get_object_or_404(User, username__iexact=username) xform = get_form({'user': owner, 'id_string__iexact': id_string}) helper_auth_helper(request) if not has_permission(xform, owner, request): return HttpResponseForbidden(_(u'Not shared.')) data = {'data': kml_export_data(id_string, user=owner, xform=xform)} response = \ render(request, "survey.kml", data, content_type="application/vnd.google-earth.kml+xml") response['Content-Disposition'] = \ generate_content_disposition_header(id_string, 'kml') audit = {"xform": xform.id_string, "export_type": Export.KML_EXPORT} audit_log( Actions.EXPORT_CREATED, request.user, owner, _("Created KML export on '%(id_string)s'.") % { 'id_string': xform.id_string, }, audit, request) # log download as well audit_log( Actions.EXPORT_DOWNLOADED, request.user, owner, _("Downloaded KML export on '%(id_string)s'.") % { 'id_string': xform.id_string, }, audit, request) return response
def kml_export(request, username, id_string): """ KML export view. """ # read the locations from the database owner = get_object_or_404(User, username__iexact=username) xform = get_form({'user': owner, 'id_string__iexact': id_string}) helper_auth_helper(request) if not has_permission(xform, owner, request): return HttpResponseForbidden(_(u'Not shared.')) data = {'data': kml_export_data(id_string, user=owner, xform=xform)} response = render( request, "survey.kml", data, content_type="application/vnd.google-earth.kml+xml") response['Content-Disposition'] = \ generate_content_disposition_header(id_string, 'kml') audit = {"xform": xform.id_string, "export_type": Export.KML_EXPORT} audit_log(Actions.EXPORT_CREATED, request.user, owner, _("Created KML export on '%(id_string)s'.") % { 'id_string': xform.id_string, }, audit, request) # log download as well audit_log(Actions.EXPORT_DOWNLOADED, request.user, owner, _("Downloaded KML export on '%(id_string)s'.") % { 'id_string': xform.id_string, }, audit, request) return response
def test_generate_content_disposition_header(self): file_name = "export" extension = "ext" date_pattern = "\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2}" file_name_pattern = "%s.%s" % (file_name, extension) file_name_with_timestamp_pattern = \ "%s-%s.%s" % (file_name, date_pattern, extension) return_value_with_no_name = \ generate_content_disposition_header(None, extension) self.assertEquals(return_value_with_no_name, "attachment;") return_value_with_name_and_no_show_date = \ generate_content_disposition_header(file_name, extension) self.assertTrue(re.search(file_name_with_timestamp_pattern, return_value_with_name_and_no_show_date)) return_value_with_name_and_false_show_date = \ generate_content_disposition_header(file_name, extension, False) self.assertTrue(re.search(file_name_pattern, return_value_with_name_and_false_show_date))