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_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 to_xlsform(self): '''Generate an XLS format XLSForm copy of this form.''' file_path= self.xls.name default_storage= get_storage_class()() 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: return None
def _xls_file_io(self): """ Pulls the xls file from remote storage this should be used sparingly """ file_path = self.xls.name default_storage = get_storage_class()() if file_path != '' and default_storage.exists(file_path): with default_storage.open(file_path) as ff: if file_path.endswith('.csv'): return convert_csv_to_xls(ff.read()) else: return BytesIO(ff.read())