def respond_ckan(ty): # First check if there is a package (dataset) in CKAN tables = {"task": model.Task, "task_run": model.TaskRun} msg_1 = gettext("Data exported to ") msg = msg_1 + "%s ..." % current_app.config['CKAN_URL'] ckan = Ckan(url=current_app.config['CKAN_URL'], api_key=current_user.ckan_api) app_url = url_for('.details', short_name=app.short_name, _external=True) try: package, e = ckan.package_exists(name=app.short_name) if e: raise e if package: # Update the package owner = User.query.get(app.owner_id) package = ckan.package_update(app=app, user=owner, url=app_url, resources=package['resources']) ckan.package = package resource_found = False # print len(package['resources']) for r in package['resources']: if r['name'] == ty: ckan.datastore_delete(name=ty, resource_id=r['id']) ckan.datastore_create(name=ty, resource_id=r['id']) ckan.datastore_upsert(name=ty, records=gen_json(tables[ty]), resource_id=r['id']) resource_found = True break if not resource_found: create_ckan_datastore(ckan, ty, package['id']) else: owner = User.query.get(app.owner_id) package = ckan.package_create(app=app, user=owner, url=app_url) create_ckan_datastore(ckan, ty, package['id']) #new_resource = ckan.resource_create(name=ty, # package_id=package['id']) #ckan.datastore_create(name=ty, # resource_id=new_resource['result']['id']) #ckan.datastore_upsert(name=ty, # records=gen_json(tables[ty]), # resource_id=new_resource['result']['id']) flash(msg, 'success') return respond() except requests.exceptions.ConnectionError: msg = "CKAN server seems to be down, try again layer or contact the CKAN admins" current_app.logger.error(msg) flash(msg, 'danger') except Exception as inst: if len(inst.args) == 3: t, msg, status_code = inst.args msg = ("Error: %s with status code: %s" % (t, status_code)) else: # pragma: no cover msg = ("Error: %s" % inst.args[0]) current_app.logger.error(msg) flash(msg, 'danger') finally: return respond()
def respond_ckan(ty): # First check if there is a package (dataset) in CKAN tables = {"task": model.Task, "task_run": model.TaskRun} msg_1 = lazy_gettext("Data exported to ") msg = msg_1 + "%s ..." % current_app.config['CKAN_URL'] ckan = Ckan(url=current_app.config['CKAN_URL'], api_key=current_user.ckan_api) app_url = url_for('.details', short_name=app.short_name, _external=True) try: package = ckan.package_exists(name=app.short_name) if package: # Update the package ckan.package_update(app=app, user=app.owner, url=app_url) if len(package['resources']) == 0: resources = create_ckan_datastores(ckan) ckan.datastore_upsert(name=ty, records=gen_json(tables[ty]), resource_id=resources[ty]['id']) flash(msg, 'success') return render_template('/applications/export.html', title=title, app=app) else: ckan.datastore_delete(name=ty) ckan.datastore_create(name=ty) ckan.datastore_upsert(name=ty, records=gen_json(tables[ty])) flash(msg, 'success') return render_template('/applications/export.html', title=title, app=app) else: ckan.package_create(app=app, user=app.owner, url=app_url, tags=current_app.config['BRAND']) resources = create_ckan_datastores(ckan) ckan.datastore_upsert(name=ty, records=gen_json(tables[ty]), resource_id=resources[ty]['id']) flash(msg, 'success') return render_template('/applications/export.html', title=title, app=app) except Exception as inst: print inst if len(inst.args) == 3: type, msg, status_code = inst.args msg = ("Error: %s with status code: %s" % (type, status_code)) else: msg = ("Error: %s" % inst.args[0]) flash(msg, 'danger') return render_template('/applications/export.html', title=title, app=app)
def respond_ckan(ty): # First check if there is a package (dataset) in CKAN tables = {"task": model.Task, "task_run": model.TaskRun} msg_1 = lazy_gettext("Data exported to ") msg = msg_1 + "%s ..." % current_app.config['CKAN_URL'] ckan = Ckan(url=current_app.config['CKAN_URL'], api_key=current_user.ckan_api) app_url = url_for('.details', short_name=app.short_name, _external=True) try: package, e = ckan.package_exists(name=app.short_name) if e: raise e if package: # Update the package ckan.package_update(app=app, user=app.owner, url=app_url) if len(package['resources']) == 0: resources = create_ckan_datastores(ckan) ckan.datastore_upsert(name=ty, records=gen_json(tables[ty]), resource_id=resources[ty]['id']) else: ckan.datastore_delete(name=ty) ckan.datastore_create(name=ty) ckan.datastore_upsert(name=ty, records=gen_json(tables[ty])) else: ckan.package_create(app=app, user=app.owner, url=app_url, tags=current_app.config['BRAND']) resources = create_ckan_datastores(ckan) ckan.datastore_upsert(name=ty, records=gen_json(tables[ty]), resource_id=resources[ty]['id']) flash(msg, 'success') return respond() except requests.exceptions.ConnectionError: msg = "CKAN server seems to be down, try again layer or contact the CKAN admins" current_app.logger.error(msg) flash(msg, 'danger') except Exception as inst: if len(inst.args) == 3: type, msg, status_code = inst.args msg = ("Error: %s with status code: %s" % (type, status_code)) else: msg = ("Error: %s" % inst.args[0]) current_app.logger.error(msg) flash(msg, 'danger') finally: return respond()