Skip to content

Commit

Permalink
Preparing package create
Browse files Browse the repository at this point in the history
  • Loading branch information
avdata99 committed Nov 22, 2024
1 parent e134082 commit 1540537
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 13 deletions.
56 changes: 44 additions & 12 deletions ckanext/superset/blueprints/superset.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import logging
from flask import Blueprint
from ckan.plugins.toolkit import config, render
from ckan import model
from ckan.common import current_user, request
from ckan.plugins import toolkit as tk

from ckanext.superset.config import get_config
from ckanext.superset.decorators import require_sysadmin_user
from ckanext.superset.data.main import SupersetCKAN
from ckanext.superset.utils import slug


log = logging.getLogger(__name__)
superset_bp = Blueprint('superset_blueprint', __name__, url_prefix='/apache-superset')


@superset_bp.route('/')
@superset_bp.route('/', methods=['GET'])
@require_sysadmin_user
def index():
# Datos informativos
superset_url = config.get('ckanext.superset.instance.url')
superset_url = tk.config.get('ckanext.superset.instance.url')
cfg = get_config()
sc = SupersetCKAN(**cfg)
sc.load_datasets()
Expand All @@ -30,19 +35,46 @@ def index():
'databases_count': databases_count,
'databases': databases,
}
return render('superset/index.html', extra_vars)
return tk.render('superset/index.html', extra_vars)


@superset_bp.route('/create-dataset/<string:superset_dataset_id>')
@require_sysadmin_user
def create_dataset(superset_dataset_id):
""" Create a new CKAN dataset from a Superset dataset """
# Check if the dataset exists in CKAN
cfg = get_config()
sc = SupersetCKAN(**cfg)
superset_dataset = sc.get_dataset(superset_dataset_id)

extra_vars = {
'superset_dataset': superset_dataset,
}
return render('superset/create-dataset.html', extra_vars)
if request.method == 'GET':
# Check if the dataset exists in CKAN
cfg = get_config()
sc = SupersetCKAN(**cfg)
superset_dataset = sc.get_dataset(superset_dataset_id)

extra_vars = {
'superset_dataset': superset_dataset,
}
return tk.render('superset/create-dataset.html', extra_vars)

if request.method == 'POST':
# Create the dataset
ckan_dataset_title = request.form.get('ckan_dataset_title')
# generate a slug name
ckan_dataset_name = slug(ckan_dataset_title)
# ensure the name is unique
c = 2
while pkg := model.Package.filter(name=ckan_dataset_name).first():
log.warning(f'Package name {ckan_dataset_name} already exists for package {pkg.id}')
ckan_dataset_name = f'{ckan_dataset_name}-{superset_dataset_id}-{c}'
c += 1

# Create the dataset
action = tk.get_action("package_create")
context = {'user': current_user.name}
data = {
'name': ckan_dataset_name,
'title': ckan_dataset_title,
'notes': request.form.get('ckan_dataset_notes'),
'owner_org': request.form.get('ckan_dataset_owner_org'),
'private': request.form.get('ckan_dataset_private'),
'superset_dataset_id': superset_dataset_id,
}
pkg = action(context, data)
20 changes: 19 additions & 1 deletion ckanext/superset/templates/superset/create-dataset.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,25 @@
<section class="module">
<div class="module-content">
<h2>Create CKAN dataset from Superset dataset</h2>


<form action="{{ h.url_for('superset_blueprint.create_dataset', superset_dataset_id=superset_dataset.id) }}" method="post">

{{ h.csrf_input() }}

<div class="form-group">
<label for="ckan_dataset_title">Dataset title</label>
<input
type="text" class="form-control"
id="ckan_dataset_title" name="ckan_dataset_title" required
value="{{ superset_dataset.table_name }}"
>
</div>



<button type="submit" class="btn btn-primary">Create CKAN dataset</button>

</form>
</div>

</section>
Expand Down
11 changes: 11 additions & 0 deletions ckanext/superset/templates/superset/index.html
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
{% extends "page.html" %}

{% block primary_content %}

{% block superset_index_admin_top %}{% endblock %}

<section class="module">
<div class="module-content">
{% block superset_index_admin_module_1 %}
<h2>Superset Dashboard</h2>
<p>URL de la instancia de Superset: <a href="{{ superset_url }}" target="_blank">{{ superset_url }}</a></p>
<p>Datasets: {{ datasets_count }}</p>
<p>Bases de Datos: {{ databases_count }}</p>
{% endblock %}
</div>

{% block superset_index_admin_module_2 %}{% endblock %}

<div class="module-content">
<h3>Datasets</h3>
<table class="table table-striped table-bordered">
<thead>
<tr>
{% block superset_index_admin_table_headers %}
<th>{{ _('CKAN') }}</th>
<th>{{ _('Schema') }}</th>
<th>{{ _('Nombre de tabla') }}</th>
<th>{{ _('SQL') }}</th>
<th>{{ _('Actualizado') }}</th>
<th>{{ _('Base de datos') }}</th>
{% endblock %}
</tr>
</thead>
<tbody>
{% for dataset in datasets %}
<tr>
{% block superset_index_admin_table_row %}
<td>
{#
Create CKAN dataset action or if exists, link to the dataset
Expand All @@ -47,6 +57,7 @@ <h3>Datasets</h3>
</td>
<td>{{ dataset.changed_on_delta_humanized }}</td>
<td>{{ dataset.database.database_name }} ({{ dataset.database.id }})</td>
{% endblock %}
</tr>
{% endfor %}
</tbody>
Expand Down
10 changes: 10 additions & 0 deletions ckanext/superset/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import re
import unicodedata


def slug(value):
value = str(value)
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
value = re.sub(r'[^\w\s-]', '', value).strip().lower()
value = re.sub(r'[-\s]+', '-', value)
return value

0 comments on commit 1540537

Please sign in to comment.