Skip to content

Commit

Permalink
Merge pull request #197 from JVickery-TBS/feature/delete-datastore-table
Browse files Browse the repository at this point in the history
Delete Datastore Table Button
  • Loading branch information
ThrawnCA authored Feb 1, 2024
2 parents 58be9be + a1b73f1 commit 959dfd2
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
22 changes: 22 additions & 0 deletions ckanext/xloader/templates/xloader/confirm_datastore_delete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% extends "page.html" %}

{% block subtitle %}{{ _("Confirm Delete") }}{% endblock %}

{% block maintag %}<div class="row" role="main">{% endblock %}

{% block main_content %}
<section class="module col-md-6 col-md-offset-3">
<div class="module-content">
{% block form %}
<p>{{ _('Are you sure you want to delete the DataStore and Data Dictionary?') }}</p>
<p class="form-actions">
<form action="{{ h.url_for('xloader.delete_datastore_table', id=package_id, resource_id=resource_id) }}" method="post">
{{ h.csrf_input() if 'csrf_input' in h }}
<button class="btn btn-danger" type="submit" name="cancel" >{{ _('Cancel') }}</button>
<button class="btn btn-primary" type="submit" name="delete" >{{ _('Confirm Delete') }}</button>
</form>
</p>
{% endblock %}
</div>
</section>
{% endblock %}
22 changes: 20 additions & 2 deletions ckanext/xloader/templates/xloader/resource_data.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,36 @@

{% block primary_content_inner %}

{% set action = h.url_for('xloader.resource_data', id=pkg.name, resource_id=res.id) %}
{% set show_table = true %}

{% block upload_ds_button %}
<form method="post" action="{{ action }}" class="datapusher-form">
{% set action = h.url_for('xloader.resource_data', id=pkg.name, resource_id=res.id) %}
<form method="post" action="{{ action }}" class="datapusher-form mb-3 mr-3 d-inline-block">
{{ h.csrf_input() if 'csrf_input' in h }}
<button class="btn btn-primary" name="save" type="submit">
<i class="fa fa-cloud-upload"></i> {{ _('Upload to DataStore') }}
</button>
</form>
{% endblock %}

<div class="clear-fix mx-1 d-inline-block"></div>

{% block delete_ds_button %}
{% if res.datastore_active %}
{% set delete_action = h.url_for('xloader.delete_datastore_table', id=pkg.id, resource_id=res.id) %}
<form method="post" action="{{ delete_action }}" class="mb-3 d-inline-block">
{{ h.csrf_input() if 'csrf_input' in h }}
<a href="{{ h.url_for('xloader.delete_datastore_table', id=pkg.id, resource_id=res.id) }}"
class="btn btn-danger"
type="submit"
data-module="confirm-action"
data-module-with-data=true
data-module-content="{{ _('Are you sure you want to delete the DataStore and Data Dictionary?') }}"
>{% block delete_datastore_button_text %}<i class="fa fa-trash-alt"></i> {{ _('Delete from DataStore') }}{% endblock %}</a>
</form>
{% endif %}
{% endblock %}

{% if status.error and status.error.message %}
{% set show_table = false %}
<div class="alert alert-error">
Expand Down
42 changes: 42 additions & 0 deletions ckanext/xloader/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from flask import Blueprint, request

from ckan.plugins.toolkit import _, h, g, render, request, abort, NotAuthorized, get_action, ObjectNotFound

import ckanext.xloader.utils as utils


Expand All @@ -21,3 +23,43 @@ def resource_data(id, resource_id):
except ValueError:
rows = None
return utils.resource_data(id, resource_id, rows)


@xloader.route("/dataset/<id>/delete-datastore/<resource_id>", methods=("GET", "POST"))
def delete_datastore_table(id, resource_id):
if u'cancel' in request.form:
return h.redirect_to(u'xloader.resource_data', id=id, resource_id=resource_id)

context = {"user": g.user}

try:
res_dict = get_action('resource_show')(context, {"id": resource_id})
if res_dict.get('package_id') != id:
raise ObjectNotFound
except ObjectNotFound:
return abort(404, _(u'Resource not found'))

if request.method == 'POST':
try:
get_action('datastore_delete')(context, {
"resource_id": resource_id,
"force": True})
except NotAuthorized:
return abort(403, _(u'Unauthorized to delete resource %s') % resource_id)

h.flash_notice(_(u'DataStore and Data Dictionary deleted for resource %s') % resource_id)

return h.redirect_to(
'xloader.resource_data',
id=id,
resource_id=resource_id
)
else:
g.resource_id = resource_id
g.package_id = id

extra_vars = {
u"resource_id": resource_id,
u"package_id": id
}
return render(u'xloader/confirm_datastore_delete.html', extra_vars)

0 comments on commit 959dfd2

Please sign in to comment.