def generate_external_export( export_type, username, id_string, export_id=None, token=None, filter_query=None, meta=None): xform = XForm.objects.get( user__username__iexact=username, id_string__exact=id_string) user = User.objects.get(username=username) server, name = _get_server_from_metadata(xform, meta, token) # dissect the url parsed_url = urlparse(server) token = parsed_url.path[5:] ser = parsed_url.scheme + '://' + parsed_url.netloc records = _get_records(Instance.objects.filter( xform__user=user, xform__id_string=id_string)) status_code = 0 if records and server: try: client = Client(ser) response = client.xls.create(token, json.dumps(records)) if hasattr(client.xls.conn, 'last_response'): status_code = client.xls.conn.last_response.status_code except Exception as e: raise J2XException( u"J2X client could not generate report. Server -> {0}," u" Error-> {1}".format(server, e) ) else: if not server: raise J2XException(u"External server not set") elif not records: raise J2XException( u"No record to export. Form -> {0}".format(id_string) ) # get or create export object if export_id: export = Export.objects.get(id=export_id) else: export = Export.objects.create(xform=xform, export_type=export_type) export.export_url = response if status_code == 201: export.internal_status = Export.SUCCESSFUL export.filename = name + '-' + response[5:] if name else response[5:] export.export_url = ser + response else: export.internal_status = Export.FAILED export.save() return export
def upload_template_for_external_export(server, file_obj): try: client = Client(server) response = client.template.create(template_file=file_obj) if hasattr(client.template.conn, 'last_response'): status_code = client.template.conn.last_response.status_code except Exception as e: response = str(e) status_code = 500 return str(status_code) + '|' + response
def upload_template_for_external_export(server, file_obj): """ Uploads an Excel template to the XLSReport server. Returns the status code with the server response. """ client = Client(server) response = client.template.create(template_file=file_obj) status_code = None if hasattr(client.template.conn, 'last_response'): status_code = client.template.conn.last_response.status_code return str(status_code) + '|' + response
def generate_external_export(export_type, username, id_string, export_id=None, options=None, xform=None): """ Generates external export using ONA data through an external service. param: export_type params: username: logged in username params: id_string: xform id_string params: export_id: ID of export object associated with the request param: options: additional parameters required for the lookup. data_id: instance id query: filter_query for custom queries meta: metadata associated with external export token: authentication key required by external service """ data_id = options.get("data_id") filter_query = options.get("query") meta = options.get("meta") token = options.get("token") if xform is None: xform = XForm.objects.get(user__username__iexact=username, id_string__iexact=id_string) user = User.objects.get(username=username) server, name = _get_server_from_metadata(xform, meta, token) # dissect the url parsed_url = urlparse(server) token = parsed_url.path[5:] ser = parsed_url.scheme + '://' + parsed_url.netloc # Get single submission data if data_id: inst = Instance.objects.filter(xform__user=user, xform__id_string=id_string, deleted_at=None, pk=data_id) instances = [inst[0].json if inst else {}] else: instances = query_data(xform, query=filter_query) records = _get_records(instances) status_code = 0 if records and server: try: client = Client(ser) response = client.xls.create(token, json.dumps(records)) if hasattr(client.xls.conn, 'last_response'): status_code = client.xls.conn.last_response.status_code except Exception as e: raise J2XException( u"J2X client could not generate report. Server -> {0}," u" Error-> {1}".format(server, e)) else: if not server: raise J2XException(u"External server not set") elif not records: raise J2XException( u"No record to export. Form -> {0}".format(id_string)) # get or create export object if export_id: export = Export.objects.get(id=export_id) else: export_options = get_export_options(options) export = Export.objects.create(xform=xform, export_type=export_type, options=export_options) export.export_url = response if status_code == 201: export.internal_status = Export.SUCCESSFUL export.filename = name + '-' + response[5:] if name else response[5:] export.export_url = ser + response else: export.internal_status = Export.FAILED export.save() return export