def download_xlsform(request, username, id_string): xform = get_object_or_404(XForm, user__username=username, id_string=id_string) owner = User.objects.get(username=username) helper_auth_helper(request) if not has_permission(xform, owner, request, xform.shared): return HttpResponseForbidden('Not shared.') file_path = xform.xls.name default_storage = get_storage_class()() if default_storage.exists(file_path): audit = { "xform": xform.id_string } audit_log( Actions.FORM_XLS_DOWNLOADED, request.user, xform.user, _("Downloaded XLS file for form '%(id_string)s'.") % { "id_string": xform.id_string }, audit, request) split_path = file_path.split(os.extsep) extension = 'xls' if len(split_path) > 1: extension = split_path[len(split_path) - 1] response = response_with_mimetype_and_name( 'vnd.ms-excel', id_string, show_date=False, extension=extension, file_path=file_path) return response else: messages.add_message(request, messages.WARNING, _(u'No XLS file for your form ' u'<strong>%(id)s</strong>') % {'id': id_string}) return HttpResponseRedirect("/%s" % username)
def kml_export(request, username, id_string): # read the locations from the database owner = get_object_or_404(User, username__iexact=username) xform = get_object_or_404(XForm, id_string__exact=id_string, user=owner) 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)} response = \ render(request, "survey.kml", data, content_type="application/vnd.google-earth.kml+xml") response['Content-Disposition'] = \ disposition_ext_and_date(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 download_xlsform(request, username, id_string): xform = get_object_or_404(XForm, user__username=username, id_string=id_string) owner = User.objects.get(username=username) helper_auth_helper(request) if not has_permission(xform, owner, request, xform.shared): return HttpResponseForbidden('Not shared.') file_path = xform.xls.name default_storage = get_storage_class()() if default_storage.exists(file_path): audit = {"xform": xform.id_string} audit_log( Actions.FORM_XLS_DOWNLOADED, request.user, xform.user, _("Downloaded XLS file for form '%(id_string)s'.") % {"id_string": xform.id_string}, audit, request) split_path = file_path.split(os.extsep) extension = 'xls' if len(split_path) > 1: extension = split_path[len(split_path) - 1] response = response_with_mimetype_and_name('vnd.ms-excel', id_string, show_date=False, extension=extension, file_path=file_path) return response else: messages.add_message( request, messages.WARNING, _(u'No XLS file for your form ' u'<strong>%(id)s</strong>') % {'id': id_string}) return HttpResponseRedirect("/%s" % username)
def download_jsonform(request, username, id_string): """ XForm JSON view. """ owner = get_object_or_404(User, username__iexact=username) xform = get_form({ 'user__username__iexact': username, 'id_string__iexact': id_string }) if request.method == "OPTIONS": response = HttpResponse() add_cors_headers(response) return response helper_auth_helper(request) if not has_permission(xform, owner, request, xform.shared): response = HttpResponseForbidden(_(u'Not shared.')) add_cors_headers(response) return response response = response_with_mimetype_and_name( 'json', id_string, show_date=False) if 'callback' in request.GET and request.GET.get('callback') != '': callback = request.GET.get('callback') response.content = "%s(%s)" % (callback, xform.json) else: add_cors_headers(response) response.content = xform.json return response
def download_excel_analyser(request, username, form_id_string): xform = get_object_or_404(XForm, user__username__iexact=username, id_string__exact=form_id_string) owner = User.objects.get(username__iexact=username) helper_auth_helper(request) if not has_permission(xform, owner, request, xform.shared): return HttpResponseForbidden('Not shared.') # Get the XLSForm. xlsform_io= _get_xlsform(request, username, form_id_string) # FIXME: Really don't like this overloading... if isinstance(xlsform_io, HttpResponse): return xlsform_io # Get the data. data_export= Export.objects.filter( xform=xform, export_type=Export.XLS_EXPORT).order_by('-created_on').first() if not data_export: raise Http404('Please generate an XLS export of your data before generating an Excel Analyser copy.') analyser_filename= os.path.splitext(data_export.filename)[0] + '_EXCEL_ANALYSER.xlsx' with get_storage_class()().open(data_export.filepath) as data_file_xlsx: analyser_io= generate_analyser(xlsform_io, data_file_xlsx) response = StreamingHttpResponse(FileWrapper(analyser_io), content_type='application/vnd.ms-excel; charset=utf-8') response['Content-Disposition'] = 'attachment; filename={}'.format(analyser_filename) return response
def zip_export(request, username, id_string): owner = get_object_or_404(User, username=username) xform = get_object_or_404(XForm, id_string=id_string, user=owner) helper_auth_helper(request) if not has_permission(xform, owner, request): return HttpResponseForbidden(_(u'Not shared.')) if request.GET.get('raw'): id_string = None attachments = Attachment.objects.filter(instance__xform=xform) zip_file = create_attachments_zipfile(attachments) audit = {"xform": xform.id_string, "export_type": Export.ZIP_EXPORT} audit_log( Actions.EXPORT_CREATED, request.user, owner, _("Created ZIP 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 ZIP export on '%(id_string)s'.") % { 'id_string': xform.id_string, }, audit, request) if request.GET.get('raw'): id_string = None response = response_with_mimetype_and_name('zip', id_string, file_path=zip_file, use_local_filesystem=True) return response
def _get_xlsform(request, username, form_id_string): xform = get_object_or_404(XForm, user__username__iexact=username, id_string__exact=form_id_string) owner = User.objects.get(username__iexact=username) helper_auth_helper(request) if not has_permission(xform, owner, request, xform.shared): # FIXME: Is there not a 403 exception equivalent to `Http404`? return HttpResponseForbidden('Not shared.') file_path = xform.xls.name default_storage = get_storage_class()() try: if file_path != '' and default_storage.exists(file_path): with default_storage.open(file_path) as xlsform_file: if file_path.endswith('.csv'): xlsform_io = convert_csv_to_xls(xlsform_file.read()) else: xlsform_io= io.BytesIO(xlsform_file.read()) return xlsform_io else: messages.add_message(request, messages.WARNING, _(u'No XLS file for your form ' u'<strong>%(id)s</strong>') % {'id': form_id_string}) return HttpResponseRedirect("/%s" % username) except: return HttpResponseServerError('Error retrieving XLSForm.')
def download_spss_labels(request, username, form_id_string): xform = get_object_or_404(XForm, user__username__iexact=username, id_string__exact=form_id_string) owner = User.objects.get(username__iexact=username) helper_auth_helper(request) if not has_permission(xform, owner, request, xform.shared): return HttpResponseForbidden('Not shared.') try: xlsform_io= xform.to_xlsform() if not xlsform_io: messages.add_message(request, messages.WARNING, _(u'No XLS file for your form ' u'<strong>%(id)s</strong>') % {'id': form_id_string}) return HttpResponseRedirect("/%s" % username) except: return HttpResponseServerError('Error retrieving XLSForm.') survey= survey_from.xls(filelike_obj=xlsform_io) zip_filename= '{}_spss_labels.zip'.format(xform.id_string) zip_io= survey_to_spss_label_zip(survey, xform.id_string) response = StreamingHttpResponse(FileWrapper(zip_io), content_type='application/zip; charset=utf-8') response['Content-Disposition'] = 'attachment; filename={}'.format(zip_filename) return response
def download_spss_labels(request, username, form_id_string): xform = get_object_or_404(XForm, user__username__iexact=username, id_string__exact=form_id_string) owner = User.objects.get(username__iexact=username) helper_auth_helper(request) if not has_permission(xform, owner, request, xform.shared): return HttpResponseForbidden('Not shared.') try: xlsform_io= xform.to_xlsform() if not xlsform_io: messages.add_message(request, messages.WARNING, _(u'No XLS file for your form ' u'<strong>%(id)s</strong>') % {'id': form_id_string}) return HttpResponseRedirect("/%s" % username) except: return HttpResponseServerError('Error retrieving XLSForm.') survey= Survey.from_xls(filelike_obj=xlsform_io) zip_filename= '{}_spss_labels.zip'.format(xform.id_string) zip_io= survey_to_spss_label_zip(survey, xform.id_string) response = StreamingHttpResponse(FileWrapper(zip_io), content_type='application/zip; charset=utf-8') response['Content-Disposition'] = 'attachment; filename={}'.format(zip_filename) return response
def export_download(request, username, id_string, export_type, filename): owner = get_object_or_404(User, username__iexact=username) xform = get_object_or_404(XForm, id_string__exact=id_string, user=owner) helper_auth_helper(request) if not has_permission(xform, owner, request): return HttpResponseForbidden(t('Not shared.')) # find the export entry in the db export = get_object_or_404(Export, xform=xform, filename=filename) ext, mime_type = export_def_from_filename(export.filename) audit = {"xform": xform.id_string, "export_type": export.export_type} audit_log( Actions.EXPORT_DOWNLOADED, request.user, owner, t("Downloaded %(export_type)s export '%(filename)s' " "on '%(id_string)s'.") % { 'export_type': export.export_type.upper(), 'filename': export.filename, 'id_string': xform.id_string, }, audit, request) if request.GET.get('raw'): id_string = None default_storage = get_storage_class()() if not isinstance(default_storage, FileSystemStorage): return HttpResponseRedirect(default_storage.url(export.filepath)) basename = os.path.splitext(export.filename)[0] response = response_with_mimetype_and_name(mime_type, name=basename, extension=ext, file_path=export.filepath, show_date=False) return response
def download_jsonform(request, username, id_string): """ XForm JSON view. """ owner = get_object_or_404(User, username__iexact=username) xform = get_form({ 'user__username__iexact': username, 'id_string__iexact': id_string }) if request.method == "OPTIONS": response = HttpResponse() add_cors_headers(response) return response helper_auth_helper(request) if not has_permission(xform, owner, request, xform.shared): response = HttpResponseForbidden(_(u'Not shared.')) add_cors_headers(response) return response response = response_with_mimetype_and_name('json', id_string, show_date=False) if 'callback' in request.GET and request.GET.get('callback') != '': callback = request.GET.get('callback') response.content = "%s(%s)" % (callback, xform.json) else: add_cors_headers(response) response.content = xform.json return response
def zip_export(request, username, id_string): owner = get_object_or_404(User, username=username) xform = get_object_or_404(XForm, id_string=id_string, user=owner) helper_auth_helper(request) if not has_permission(xform, owner, request): return HttpResponseForbidden(_(u'Not shared.')) if request.GET.get('raw'): id_string = None attachments = Attachment.objects.filter(instance__xform=xform) zip_file = create_attachments_zipfile(attachments) audit = { "xform": xform.id_string, "export_type": Export.ZIP_EXPORT } audit_log( Actions.EXPORT_CREATED, request.user, owner, _("Created ZIP 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 ZIP export on '%(id_string)s'.") % { 'id_string': xform.id_string, }, audit, request) if request.GET.get('raw'): id_string = None response = response_with_mimetype_and_name('zip', id_string, file_path=zip_file, use_local_filesystem=True) return response
def kml_export(request, username, id_string): # read the locations from the database owner = get_object_or_404(User, username__iexact=username) xform = get_object_or_404(XForm, id_string__exact=id_string, user=owner) 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)} response = \ render(request, "survey.kml", data, content_type="application/vnd.google-earth.kml+xml") response['Content-Disposition'] = \ disposition_ext_and_date(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 api(request, username=None, id_string=None): """ Returns all results as JSON. If a parameter string is passed, it takes the 'query' parameter, converts this string to a dictionary, an that is then used as a MongoDB query string. NOTE: only a specific set of operators are allow, currently $or and $and. Please send a request if you'd like another operator to be enabled. NOTE: Your query must be valid JSON, double check it here, http://json.parser.online.fr/ E.g. api?query='{"last_name": "Smith"}' """ if request.method == "OPTIONS": response = HttpResponse() add_cors_headers(response) return response helper_auth_helper(request) helper_auth_helper(request) xform, owner = check_and_set_user_and_form(username, id_string, request) if not xform: return HttpResponseForbidden(_(u'Not shared.')) try: args = { 'username': username, 'id_string': id_string, 'query': request.GET.get('query'), 'fields': request.GET.get('fields'), 'sort': request.GET.get('sort') } if 'start' in request.GET: args["start"] = int(request.GET.get('start')) if 'limit' in request.GET: args["limit"] = int(request.GET.get('limit')) if 'count' in request.GET: args["count"] = True if int(request.GET.get('count')) > 0\ else False cursor = ParsedInstance.query_mongo(**args) except ValueError as e: return HttpResponseBadRequest(e.__str__()) records = list(record for record in cursor) response_text = json_util.dumps(records) if 'callback' in request.GET and request.GET.get('callback') != '': callback = request.GET.get('callback') response_text = ("%s(%s)" % (callback, response_text)) response = HttpResponse(response_text, mimetype='application/json') add_cors_headers(response) return response
def download_xlsform(request, username, id_string): xform = get_object_or_404(XForm, user__username__iexact=username, id_string__exact=id_string) owner = User.objects.get(username__iexact=username) helper_auth_helper(request) if not has_permission(xform, owner, request, xform.shared): return HttpResponseForbidden('Not shared.') file_path = xform.xls.name default_storage = get_storage_class()() if file_path != '' and default_storage.exists(file_path): audit = { "xform": xform.id_string } audit_log( Actions.FORM_XLS_DOWNLOADED, request.user, xform.user, _("Downloaded XLS file for form '%(id_string)s'.") % { "id_string": xform.id_string }, audit, request) if file_path.endswith('.csv'): with default_storage.open(file_path) as ff: xls_io = convert_csv_to_xls(ff.read()) response = StreamingHttpResponse( xls_io, content_type='application/vnd.ms-excel; charset=utf-8') response[ 'Content-Disposition'] = 'attachment; filename=%s.xls' % xform.id_string return response split_path = file_path.split(os.extsep) extension = 'xls' if len(split_path) > 1: extension = split_path[len(split_path) - 1] response = response_with_mimetype_and_name( 'vnd.ms-excel', id_string, show_date=False, extension=extension, file_path=file_path) return response else: messages.add_message(request, messages.WARNING, _(u'No XLS file for your form ' u'<strong>%(id)s</strong>') % {'id': id_string}) return HttpResponseRedirect("/%s" % username)
def download_xlsform(request, username, id_string): xform = get_object_or_404(XForm, user__username__iexact=username, id_string__exact=id_string) owner = User.objects.get(username__iexact=username) helper_auth_helper(request) if not has_permission(xform, owner, request, xform.shared): return HttpResponseForbidden('Not shared.') file_path = xform.xls.name default_storage = get_storage_class()() if file_path != '' and default_storage.exists(file_path): audit = { "xform": xform.id_string } audit_log( Actions.FORM_XLS_DOWNLOADED, request.user, xform.user, _("Downloaded XLS file for form '%(id_string)s'.") % { "id_string": xform.id_string }, audit, request) if file_path.endswith('.csv'): with default_storage.open(file_path) as ff: xls_io = convert_csv_to_xls(ff.read()) response = StreamingHttpResponse( xls_io, content_type='application/vnd.ms-excel; charset=utf-8') response[ 'Content-Disposition'] = 'attachment; filename=%s.xls' % xform.id_string return response split_path = file_path.split(os.extsep) extension = 'xls' if len(split_path) > 1: extension = split_path[len(split_path) - 1] response = response_with_mimetype_and_name( 'vnd.ms-excel', id_string, show_date=False, extension=extension, file_path=file_path) return response else: messages.add_message(request, messages.WARNING, _(u'No XLS file for your form ' u'<strong>%(id)s</strong>') % {'id': id_string}) return HttpResponseRedirect("/%s" % username)
def download_excel_analyser(request, username, form_id_string): xform = get_object_or_404(XForm, user__username__iexact=username, id_string__exact=form_id_string) owner = User.objects.get(username__iexact=username) helper_auth_helper(request) if not has_permission(xform, owner, request, xform.shared): return HttpResponseForbidden("Not shared.") # Get the XLSForm. xlsform_io = _get_xlsform(request, username, form_id_string) # FIXME: Really don't like this overloading... if isinstance(xlsform_io, HttpResponse): return xlsform_io # Get the data. data_export = Export.objects.filter(xform=xform, export_type=Export.XLS_EXPORT).order_by("-created_on").first() if not data_export or not data_export.filename: if not data_export: err_msg = _(u"Please generate an XLS export of your data before generating an Excel Analyser copy.") elif data_export.status == Export.PENDING: err_msg = _( u"Please wait for your XLS export to be generated " u"before trying to generate an Excel Analyser copy." ) elif data_export.status == Export.FAILED: err_msg = _( u"Last attempt for XLS export creation failed. An XLS export of your data must " u"have been successfully generated before trying to generate an Excel Analyser copy." ) else: err_msg = _( u"Unknown XLS export state. " u"Please generate an XLS export of your data before generating an Excel Analyser copy." ) return HttpResponseNotFound( loader.render_to_string("404_xls_analyzer_error.html", {"error_message": err_msg}, RequestContext(request)) ) analyser_filename = os.path.splitext(data_export.filename)[0] + "_EXCEL_ANALYSER.xlsx" with get_storage_class()().open(data_export.filepath) as data_file_xlsx: analyser_io = generate_analyser(xlsform_io, data_file_xlsx) response = StreamingHttpResponse(FileWrapper(analyser_io), content_type="application/vnd.ms-excel; charset=utf-8") response["Content-Disposition"] = "attachment; filename={}".format(analyser_filename) return response
def export_download(request, username, id_string, export_type, filename): """ Export download view. """ 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.')) # find the export entry in the db export = get_object_or_404(Export, xform=xform, filename=filename) if (export_type == Export.GOOGLE_SHEETS_EXPORT or export_type == Export.EXTERNAL_EXPORT) and \ export.export_url is not None: return HttpResponseRedirect(export.export_url) ext, mime_type = export_def_from_filename(export.filename) audit = {"xform": xform.id_string, "export_type": export.export_type} audit_log(Actions.EXPORT_DOWNLOADED, request.user, owner, _("Downloaded %(export_type)s export '%(filename)s' " "on '%(id_string)s'.") % { 'export_type': export.export_type.upper(), 'filename': export.filename, 'id_string': xform.id_string, }, audit, request) if request.GET.get('raw'): id_string = None default_storage = get_storage_class()() if not isinstance(default_storage, FileSystemStorage): return HttpResponseRedirect(default_storage.url(export.filepath)) basename = os.path.splitext(export.filename)[0] response = response_with_mimetype_and_name( mime_type, name=basename, extension=ext, file_path=export.filepath, show_date=False) return response
def zip_export(request, username, id_string): owner = get_object_or_404(User, username__iexact=username) xform = get_object_or_404(XForm, id_string__exact=id_string, user=owner) helper_auth_helper(request) if not has_permission(xform, owner, request): return HttpResponseForbidden(_(u'Not shared.')) if request.GET.get('raw'): id_string = None attachments = Attachment.objects.filter(instance__xform=xform) zip_file = None try: zip_file = create_attachments_zipfile(attachments) audit = { "xform": xform.id_string, "export_type": Export.ZIP_EXPORT } audit_log( Actions.EXPORT_CREATED, request.user, owner, _("Created ZIP 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 ZIP export on '%(id_string)s'.") % { 'id_string': xform.id_string, }, audit, request) if request.GET.get('raw'): id_string = None response = response_with_mimetype_and_name('zip', id_string) response.write(FileWrapper(zip_file)) response['Content-Length'] = zip_file.tell() zip_file.seek(0) finally: zip_file and zip_file.close() return response
def api(request, username=None, id_string=None): """ Returns all results as JSON. If a parameter string is passed, it takes the 'query' parameter, converts this string to a dictionary, an that is then used as a MongoDB query string. NOTE: only a specific set of operators are allow, currently $or and $and. Please send a request if you'd like another operator to be enabled. NOTE: Your query must be valid JSON, double check it here, http://json.parser.online.fr/ E.g. api?query='{"last_name": "Smith"}' """ if request.method == "OPTIONS": response = HttpResponse() add_cors_headers(response) return response helper_auth_helper(request) helper_auth_helper(request) xform, owner = check_and_set_user_and_form(username, id_string, request) if not xform: return HttpResponseForbidden(_(u"Not shared.")) try: args = { "username": username, "id_string": id_string, "query": request.GET.get("query"), "fields": request.GET.get("fields"), "sort": request.GET.get("sort"), } if "start" in request.GET: args["start"] = int(request.GET.get("start")) if "limit" in request.GET: args["limit"] = int(request.GET.get("limit")) if "count" in request.GET: args["count"] = True if int(request.GET.get("count")) > 0 else False cursor = ParsedInstance.query_mongo(**args) except ValueError, e: return HttpResponseBadRequest(e.__str__())
def download_excel_analyser(request, username, form_id_string): xform = get_object_or_404(XForm, user__username__iexact=username, id_string__exact=form_id_string) owner = User.objects.get(username__iexact=username) helper_auth_helper(request) if not has_permission(xform, owner, request, xform.shared): return HttpResponseForbidden('Not shared.') # Get the XLSForm. xlsform_io= _get_xlsform(request, username, form_id_string) # FIXME: Really don't like this overloading... if isinstance(xlsform_io, HttpResponse): return xlsform_io # Get the data. data_export= Export.objects.filter( xform=xform, export_type=Export.XLS_EXPORT).order_by('-created_on').first() if not data_export or not data_export.filename: if not data_export: err_msg = _(u'Please generate an XLS export of your data before generating an Excel Analyser copy.') elif data_export.status == Export.PENDING: err_msg = _(u'Please wait for your XLS export to be generated ' u'before trying to generate an Excel Analyser copy.'); elif data_export.status == Export.FAILED: err_msg = _(u'Last attempt for XLS export creation failed. An XLS export of your data must ' u'have been successfully generated before trying to generate an Excel Analyser copy.') else: err_msg = _(u'Unknown XLS export state. ' u'Please generate an XLS export of your data before generating an Excel Analyser copy.') return HttpResponseNotFound(loader.render_to_string('404_xls_analyzer_error.html', {'error_message': err_msg}, RequestContext(request))) analyser_filename= os.path.splitext(data_export.filename)[0] + '_EXCEL_ANALYSER.xlsx' with get_storage_class()().open(data_export.filepath) as data_file_xlsx: analyser_io= generate_analyser(xlsform_io, data_file_xlsx) response = StreamingHttpResponse(FileWrapper(analyser_io), content_type='application/vnd.ms-excel; charset=utf-8') response['Content-Disposition'] = 'attachment; filename={}'.format(analyser_filename) return response
def zip_export(request, username, id_string): owner = get_object_or_404(User, username=username) xform = get_object_or_404(XForm, id_string=id_string, user=owner) helper_auth_helper(request) if not has_permission(xform, owner, request): return HttpResponseForbidden(_(u'Not shared.')) if request.GET.get('raw'): id_string = None attachments = Attachment.objects.filter(instance__xform=xform) zip_file = None try: zip_file = create_attachments_zipfile(attachments) audit = { "xform": xform.id_string, "export_type": Export.ZIP_EXPORT } audit_log( Actions.EXPORT_CREATED, request.user, owner, _("Created ZIP 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 ZIP export on '%(id_string)s'.") % { 'id_string': xform.id_string, }, audit, request) if request.GET.get('raw'): id_string = None response = response_with_mimetype_and_name('zip', id_string) response.write(FileWrapper(zip_file)) response['Content-Length'] = zip_file.tell() zip_file.seek(0) finally: zip_file and zip_file.close() return response
def download_jsonform(request, username, id_string): owner = get_object_or_404(User, username__iexact=username) xform = get_object_or_404(XForm, user__username__iexact=username, id_string__exact=id_string) if request.method == "OPTIONS": response = HttpResponse() add_cors_headers(response) return response helper_auth_helper(request) if not has_permission(xform, owner, request, xform.shared): response = HttpResponseForbidden(_(u"Not shared.")) add_cors_headers(response) return response response = response_with_mimetype_and_name("json", id_string, show_date=False) if "callback" in request.GET and request.GET.get("callback") != "": callback = request.GET.get("callback") response.content = "%s(%s)" % (callback, xform.json) else: add_cors_headers(response) response.content = xform.json return response
def data_export(request, username, id_string, export_type): owner = get_object_or_404(User, username__iexact=username) xform = get_object_or_404(XForm, id_string__exact=id_string, user=owner) helper_auth_helper(request) if not has_permission(xform, owner, request): return HttpResponseForbidden(_(u'Not shared.')) query = request.GET.get("query") extension = export_type # check if we should force xlsx force_xlsx = request.GET.get('xls') != 'true' if export_type == Export.XLS_EXPORT and force_xlsx: extension = 'xlsx' elif export_type in [Export.CSV_ZIP_EXPORT, Export.SAV_ZIP_EXPORT]: extension = 'zip' audit = {"xform": xform.id_string, "export_type": export_type} # check if we need to re-generate, # we always re-generate if a filter is specified if should_create_new_export(xform, export_type) or query or\ 'start' in request.GET or 'end' in request.GET: # check for start and end params if 'start' in request.GET or 'end' in request.GET: if not query: query = '{}' query = json.dumps( _set_submission_time_to_query(json.loads(query), request)) try: export = generate_export(export_type, extension, username, id_string, None, query) audit_log( Actions.EXPORT_CREATED, request.user, owner, _("Created %(export_type)s export on '%(id_string)s'.") % { 'id_string': xform.id_string, 'export_type': export_type.upper() }, audit, request) except NoRecordsFoundError: return HttpResponseNotFound(_("No records found to export")) else: export = newset_export_for(xform, export_type) # log download as well audit_log( Actions.EXPORT_DOWNLOADED, request.user, owner, _("Downloaded %(export_type)s export on '%(id_string)s'.") % { 'id_string': xform.id_string, 'export_type': export_type.upper() }, audit, request) if not export.filename: # tends to happen when using newset_export_for. return HttpResponseNotFound("File does not exist!") # get extension from file_path, exporter could modify to # xlsx if it exceeds limits path, ext = os.path.splitext(export.filename) ext = ext[1:] if request.GET.get('raw'): id_string = None response = response_with_mimetype_and_name(Export.EXPORT_MIMES[ext], id_string, extension=ext, file_path=export.filepath) return response
def data_export(request, username, id_string, export_type): """ Data export view. """ 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.')) query = request.GET.get("query") extension = export_type # check if we should force xlsx force_xlsx = request.GET.get('xls') != 'true' if export_type == Export.XLS_EXPORT and force_xlsx: extension = 'xlsx' elif export_type in [Export.CSV_ZIP_EXPORT, Export.SAV_ZIP_EXPORT]: extension = 'zip' audit = {"xform": xform.id_string, "export_type": export_type} options = { "extension": extension, "username": username, "id_string": id_string } if query: options['query'] = query # check if we need to re-generate, # we always re-generate if a filter is specified if should_create_new_export(xform, export_type, options) or query or\ 'start' in request.GET or 'end' in request.GET: # check for start and end params start, end = _get_start_end_submission_time(request) options.update({"start": start, "end": end}) try: export = generate_export(export_type, xform, None, options) audit_log( Actions.EXPORT_CREATED, request.user, owner, _("Created %(export_type)s export on '%(id_string)s'.") % { 'id_string': xform.id_string, 'export_type': export_type.upper() }, audit, request) except NoRecordsFoundError: return HttpResponseNotFound(_("No records found to export")) except SPSSIOError as e: return HttpResponseBadRequest(str(e)) else: export = newest_export_for(xform, export_type, options) # log download as well audit_log( Actions.EXPORT_DOWNLOADED, request.user, owner, _("Downloaded %(export_type)s export on '%(id_string)s'.") % {'id_string': xform.id_string, 'export_type': export_type.upper()}, audit, request) if not export.filename and not export.error_message: # tends to happen when using newset_export_for. return HttpResponseNotFound("File does not exist!") elif not export.filename and export.error_message: return HttpResponseBadRequest(str(export.error_message)) # get extension from file_path, exporter could modify to # xlsx if it exceeds limits __, extension = os.path.splitext(export.filename) extension = extension[1:] if request.GET.get('raw'): id_string = None response = response_with_mimetype_and_name( Export.EXPORT_MIMES[extension], id_string, extension=extension, file_path=export.filepath) return response
def data_export(request, username, id_string, export_type): owner = get_object_or_404(User, username=username) xform = get_object_or_404(XForm, id_string=id_string, user=owner) helper_auth_helper(request) if not has_permission(xform, owner, request): return HttpResponseForbidden(_(u'Not shared.')) query = request.GET.get("query") extension = export_type # check if we should force xlsx force_xlsx = request.GET.get('xls') != 'true' if export_type == Export.XLS_EXPORT and force_xlsx: extension = 'xlsx' elif export_type in [Export.CSV_ZIP_EXPORT, Export.SAV_ZIP_EXPORT]: extension = 'zip' audit = { "xform": xform.id_string, "export_type": export_type } # check if we need to re-generate, # we always re-generate if a filter is specified if should_create_new_export(xform, export_type) or query or\ 'start' in request.GET or 'end' in request.GET: format_date_for_mongo = lambda x, datetime: datetime.strptime( x, '%y_%m_%d_%H_%M_%S').strftime('%Y-%m-%dT%H:%M:%S') # check for start and end params if 'start' in request.GET or 'end' in request.GET: if not query: query = '{}' query = json.loads(query) query[SUBMISSION_TIME] = {} try: if request.GET.get('start'): query[SUBMISSION_TIME]['$gte'] = format_date_for_mongo( request.GET['start'], datetime) if request.GET.get('end'): query[SUBMISSION_TIME]['$lte'] = format_date_for_mongo( request.GET['end'], datetime) except ValueError: return HttpResponseBadRequest( _("Dates must be in the format YY_MM_DD_hh_mm_ss")) else: query = json.dumps(query) try: export = generate_export( export_type, extension, username, id_string, None, query) audit_log( Actions.EXPORT_CREATED, request.user, owner, _("Created %(export_type)s export on '%(id_string)s'.") % { 'id_string': xform.id_string, 'export_type': export_type.upper() }, audit, request) except NoRecordsFoundError: return HttpResponseNotFound(_("No records found to export")) else: export = newset_export_for(xform, export_type) # log download as well audit_log( Actions.EXPORT_DOWNLOADED, request.user, owner, _("Downloaded %(export_type)s export on '%(id_string)s'.") % { 'id_string': xform.id_string, 'export_type': export_type.upper() }, audit, request) if not export.filename: # tends to happen when using newset_export_for. return HttpResponseNotFound("File does not exist!") # get extension from file_path, exporter could modify to # xlsx if it exceeds limits path, ext = os.path.splitext(export.filename) ext = ext[1:] if request.GET.get('raw'): id_string = None response = response_with_mimetype_and_name( Export.EXPORT_MIMES[ext], id_string, extension=ext, file_path=export.filepath) return response
def data_export(request, username, id_string, export_type): owner = get_object_or_404(User, username__iexact=username) xform = get_object_or_404(XForm, id_string__exact=id_string, user=owner) helper_auth_helper(request) if not has_permission(xform, owner, request): return HttpResponseForbidden(_(u'Not shared.')) query = request.GET.get("query") extension = export_type # check if we should force xlsx force_xlsx = request.GET.get('xls') != 'true' if export_type == Export.XLS_EXPORT and force_xlsx: extension = 'xlsx' elif export_type in [Export.CSV_ZIP_EXPORT, Export.SAV_ZIP_EXPORT]: extension = 'zip' audit = { "xform": xform.id_string, "export_type": export_type } # check if we need to re-generate, # we always re-generate if a filter is specified if should_create_new_export(xform, export_type) or query or\ 'start' in request.GET or 'end' in request.GET: # check for start and end params if 'start' in request.GET or 'end' in request.GET: if not query: query = '{}' query = json.dumps( _set_submission_time_to_query(json.loads(query), request)) try: export = generate_export( export_type, extension, username, id_string, None, query) audit_log( Actions.EXPORT_CREATED, request.user, owner, _("Created %(export_type)s export on '%(id_string)s'.") % { 'id_string': xform.id_string, 'export_type': export_type.upper() }, audit, request) except NoRecordsFoundError: return HttpResponseNotFound(_("No records found to export")) else: export = newset_export_for(xform, export_type) # log download as well audit_log( Actions.EXPORT_DOWNLOADED, request.user, owner, _("Downloaded %(export_type)s export on '%(id_string)s'.") % { 'id_string': xform.id_string, 'export_type': export_type.upper() }, audit, request) if not export.filename: # tends to happen when using newset_export_for. return HttpResponseNotFound("File does not exist!") # get extension from file_path, exporter could modify to # xlsx if it exceeds limits path, ext = os.path.splitext(export.filename) ext = ext[1:] if request.GET.get('raw'): id_string = None response = response_with_mimetype_and_name( Export.EXPORT_MIMES[ext], id_string, extension=ext, file_path=export.filepath) return response
def data_export(request, username, id_string, export_type): """ Data export view. """ 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.')) query = request.GET.get("query") extension = export_type # check if we should force xlsx force_xlsx = request.GET.get('xls') != 'true' if export_type == Export.XLS_EXPORT and force_xlsx: extension = 'xlsx' elif export_type in [Export.CSV_ZIP_EXPORT, Export.SAV_ZIP_EXPORT]: extension = 'zip' audit = {"xform": xform.id_string, "export_type": export_type} options = { "extension": extension, "username": username, "id_string": id_string } if query: options['query'] = query # check if we need to re-generate, # we always re-generate if a filter is specified if should_create_new_export(xform, export_type, options) or query or\ 'start' in request.GET or 'end' in request.GET: # check for start and end params start, end = _get_start_end_submission_time(request) options.update({"start": start, "end": end}) try: export = generate_export(export_type, xform, None, options) audit_log( Actions.EXPORT_CREATED, request.user, owner, _("Created %(export_type)s export on '%(id_string)s'.") % { 'id_string': xform.id_string, 'export_type': export_type.upper() }, audit, request) except NoRecordsFoundError: return HttpResponseNotFound(_("No records found to export")) except SPSSIOError as e: return HttpResponseBadRequest(str(e)) else: export = newest_export_for(xform, export_type, options) # log download as well audit_log( Actions.EXPORT_DOWNLOADED, request.user, owner, _("Downloaded %(export_type)s export on '%(id_string)s'.") % { 'id_string': xform.id_string, 'export_type': export_type.upper() }, audit, request) if not export.filename and not export.error_message: # tends to happen when using newset_export_for. return HttpResponseNotFound("File does not exist!") elif not export.filename and export.error_message: return HttpResponseBadRequest(str(export.error_message)) # get extension from file_path, exporter could modify to # xlsx if it exceeds limits __, extension = os.path.splitext(export.filename) extension = extension[1:] if request.GET.get('raw'): id_string = None response = response_with_mimetype_and_name(Export.EXPORT_MIMES[extension], id_string, extension=extension, file_path=export.filepath) return response
def data_export(request, username, id_string, export_type): owner = get_object_or_404(User, username=username) xform = get_object_or_404(XForm, id_string=id_string, user=owner) helper_auth_helper(request) if not has_permission(xform, owner, request): return HttpResponseForbidden(_(u'Not shared.')) query = request.GET.get("query") extension = export_type # check if we should force xlsx force_xlsx = request.GET.get('xls') != 'true' if export_type == Export.XLS_EXPORT and force_xlsx: extension = 'xlsx' elif export_type in [Export.CSV_ZIP_EXPORT, Export.SAV_ZIP_EXPORT]: extension = 'zip' audit = { "xform": xform.id_string, "export_type": export_type } # check if we need to re-generate, # we always re-generate if a filter is specified if should_create_new_export(xform, export_type) or query or\ 'start' in request.GET or 'end' in request.GET: format_date_for_mongo = lambda x, datetime: datetime.strptime( x, '%y_%m_%d_%H_%M_%S').strftime('%Y-%m-%dT%H:%M:%S') # check for start and end params if 'start' in request.GET or 'end' in request.GET: if not query: query = '{}' query = json.loads(query) query[SUBMISSION_TIME] = {} try: if request.GET.get('start'): query[SUBMISSION_TIME]['$gte'] = format_date_for_mongo( request.GET['start'], datetime) if request.GET.get('end'): query[SUBMISSION_TIME]['$lte'] = format_date_for_mongo( request.GET['end'], datetime) except ValueError: return HttpResponseBadRequest( _("Dates must be in the format YY_MM_DD_hh_mm_ss")) else: query = json.dumps(query) try: export = generate_export( export_type, extension, username, id_string, None, query) audit_log( Actions.EXPORT_CREATED, request.user, owner, _("Created %(export_type)s export on '%(id_string)s'.") % { 'id_string': xform.id_string, 'export_type': export_type.upper() }, audit, request) except NoRecordsFoundError: return HttpResponseNotFound(_("No records found to export")) else: export = newset_export_for(xform, export_type) # log download as well audit_log( Actions.EXPORT_DOWNLOADED, request.user, owner, _("Downloaded %(export_type)s export on '%(id_string)s'.") % { 'id_string': xform.id_string, 'export_type': export_type.upper() }, audit, request) if not export.filename: # tends to happen when using newset_export_for. return HttpResponseNotFound("File does not exist!") # get extension from file_path, exporter could modify to # xlsx if it exceeds limits path, ext = os.path.splitext(export.filename) ext = ext[1:] if request.GET.get('raw'): id_string = None response = response_with_mimetype_and_name( Export.EXPORT_MIMES[ext], id_string, extension=ext, file_path=export.filepath) return response