diff --git a/ckanext/bulkupload/actions.py b/ckanext/bulkupload/actions.py new file mode 100644 index 0000000..f01cf0f --- /dev/null +++ b/ckanext/bulkupload/actions.py @@ -0,0 +1,6 @@ +import ckan.plugins.toolkit as tk + +def call_add_resouce_api(*args, **kwargs): + + print("============================1==========================") + diff --git a/ckanext/bulkupload/auth.py b/ckanext/bulkupload/auth.py new file mode 100644 index 0000000..da33712 --- /dev/null +++ b/ckanext/bulkupload/auth.py @@ -0,0 +1,2 @@ +def bulk_resource_upload(context, data_dict): + return {'success': False} diff --git a/ckanext/bulkupload/i18n/ckanext-bulkupload.pot b/ckanext/bulkupload/i18n/ckanext-bulkupload.pot new file mode 100644 index 0000000..53719ec --- /dev/null +++ b/ckanext/bulkupload/i18n/ckanext-bulkupload.pot @@ -0,0 +1,64 @@ +# Translations template for ckanext-bulkupload. +# Copyright (C) 2024 ORGANIZATION +# This file is distributed under the same license as the ckanext-bulkupload +# project. +# FIRST AUTHOR , 2024. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: ckanext-bulkupload 0.0.1\n" +"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" +"POT-Creation-Date: 2024-04-12 09:28+0200\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=utf-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Generated-By: Babel 2.7.0\n" + +#: ckanext/bulkupload/templates/package/activity_bulk.html:3 +#: ckanext/bulkupload/templates/package/activity_bulk.html:8 +msgid "Activity Stream" +msgstr "" + +#: ckanext/bulkupload/templates/package/new_resource_not_draft.html:3 +#: ckanext/bulkupload/templates/package/new_resource_not_draft.html:4 +#: ckanext/bulkupload/templates/package/new_resource_not_draft_bulk.html:4 +#: ckanext/bulkupload/templates/package/new_resource_not_draft_bulk.html:5 +msgid "Add resource" +msgstr "" + +#: ckanext/bulkupload/templates/package/new_resource_not_draft.html:8 +msgid "Add New Resource" +msgstr "" + +#: ckanext/bulkupload/templates/package/new_resource_not_draft.html:16 +#: ckanext/bulkupload/templates/package/new_resource_not_draft_bulk.html:11 +msgid "New resource" +msgstr "" + +#: ckanext/bulkupload/templates/package/new_resource_not_draft.html:17 +#: ckanext/bulkupload/templates/package/new_resource_not_draft_bulk.html:12 +msgid "Bulk Upload" +msgstr "" + +#: ckanext/bulkupload/templates/package/new_resource_not_draft_bulk.html:23 +msgid "Description" +msgstr "" + +#: ckanext/bulkupload/templates/package/new_resource_not_draft_bulk.html:26 +msgid "Upload" +msgstr "" + +#: ckanext/bulkupload/templates/package/snippets/bulk_sucess.html:1 +msgid "Bulk upload finished" +msgstr "" + +#: ckanext/bulkupload/templates/package/snippets/bulk_sucess.html:3 +msgid "" +"Notice: Reloading this page will send the same request again and repeat the " +"bulk upload" +msgstr "" + diff --git a/ckanext/bulkupload/i18n/es/LC_MESSAGES/ckanext-bulkupload.mo b/ckanext/bulkupload/i18n/es/LC_MESSAGES/ckanext-bulkupload.mo new file mode 100644 index 0000000..5a6c6f5 Binary files /dev/null and b/ckanext/bulkupload/i18n/es/LC_MESSAGES/ckanext-bulkupload.mo differ diff --git a/ckanext/bulkupload/i18n/es/LC_MESSAGES/ckanext-bulkupload.po b/ckanext/bulkupload/i18n/es/LC_MESSAGES/ckanext-bulkupload.po new file mode 100644 index 0000000..3e76a06 --- /dev/null +++ b/ckanext/bulkupload/i18n/es/LC_MESSAGES/ckanext-bulkupload.po @@ -0,0 +1,67 @@ +# Spanish translations for ckanext-bulkupload. +# Copyright (C) 2024 ORGANIZATION +# This file is distributed under the same license as the ckanext-bulkupload +# project. +# FIRST AUTHOR , 2024. +# +msgid "" +msgstr "" +"Project-Id-Version: ckanext-bulkupload 0.0.1\n" +"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" +"POT-Creation-Date: 2024-04-12 09:28+0200\n" +"PO-Revision-Date: 2024-04-12 09:28+0200\n" +"Last-Translator: FULL NAME \n" +"Language: es\n" +"Language-Team: es \n" +"Plural-Forms: nplurals=2; plural=(n != 1)\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=utf-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Generated-By: Babel 2.7.0\n" + +#: ckanext/bulkupload/templates/package/activity_bulk.html:3 +#: ckanext/bulkupload/templates/package/activity_bulk.html:8 +msgid "Activity Stream" +msgstr "" + +#: ckanext/bulkupload/templates/package/new_resource_not_draft.html:3 +#: ckanext/bulkupload/templates/package/new_resource_not_draft.html:4 +#: ckanext/bulkupload/templates/package/new_resource_not_draft_bulk.html:4 +#: ckanext/bulkupload/templates/package/new_resource_not_draft_bulk.html:5 +msgid "Add resource" +msgstr "" + +#: ckanext/bulkupload/templates/package/new_resource_not_draft.html:8 +msgid "Add New Resource" +msgstr "" + +#: ckanext/bulkupload/templates/package/new_resource_not_draft.html:16 +#: ckanext/bulkupload/templates/package/new_resource_not_draft_bulk.html:11 +msgid "New resource" +msgstr "" + +#: ckanext/bulkupload/templates/package/new_resource_not_draft.html:17 +#: ckanext/bulkupload/templates/package/new_resource_not_draft_bulk.html:12 +msgid "Bulk Upload" +msgstr "Carga Masiva" + +#: ckanext/bulkupload/templates/package/new_resource_not_draft_bulk.html:23 +msgid "Description" +msgstr "" + +#: ckanext/bulkupload/templates/package/new_resource_not_draft_bulk.html:26 +msgid "Upload" +msgstr "" + +#: ckanext/bulkupload/templates/package/snippets/bulk_sucess.html:1 +msgid "Bulk upload finished" +msgstr "Carga masiva finalizada" + +#: ckanext/bulkupload/templates/package/snippets/bulk_sucess.html:3 +msgid "" +"Notice: Reloading this page will send the same request again and repeat " +"the bulk upload" +msgstr "" +"Aviso: al recargar esta página se enviará la misma solicitud nuevamente y se repetirá " +"la carga masiva" + diff --git a/ckanext/bulkupload/plugin.py b/ckanext/bulkupload/plugin.py index d437fe8..370325a 100644 --- a/ckanext/bulkupload/plugin.py +++ b/ckanext/bulkupload/plugin.py @@ -1,11 +1,17 @@ import ckan.plugins as plugins import ckan.plugins.toolkit as toolkit +import ckanext.bulkupload.actions as actions +import ckanext.bulkupload.auth as auth from ckanext.bulkupload.views import bulkupload +from ckan.lib.plugins import DefaultTranslation -class BulkuploadPlugin(plugins.SingletonPlugin): +class BulkuploadPlugin(plugins.SingletonPlugin, DefaultTranslation): plugins.implements(plugins.IConfigurer) plugins.implements(plugins.IBlueprint) + plugins.implements(plugins.IActions) + plugins.implements(plugins.IAuthFunctions) + plugins.implements(plugins.ITranslation) # IConfigurer @@ -13,7 +19,23 @@ def update_config(self, config_): toolkit.add_template_directory(config_, 'templates') toolkit.add_public_directory(config_, 'public') toolkit.add_resource('fanstatic', 'bulkupload') - + + # IActions + + def get_actions(self): + + return { + "call_add_resouce_api": actions.call_add_resouce_api, + + } + + # IAuthFunctions + + def get_auth_functions(self): + return { + "bulk_resource_upload": auth.bulk_resource_upload, + } + # IBlueprint def get_blueprint(self): diff --git a/ckanext/bulkupload/templates/package/activity_bulk.html b/ckanext/bulkupload/templates/package/activity_bulk.html new file mode 100644 index 0000000..8d11fad --- /dev/null +++ b/ckanext/bulkupload/templates/package/activity_bulk.html @@ -0,0 +1,12 @@ +{% extends "package/read_base.html" %} + +{% block subtitle %}{{ _('Activity Stream') }} {{ g.template_title_delimiter }} {{ super() }}{% endblock %} + +{% block primary_content_inner %} +

+ {% block page_heading %} + {{ _('Activity Stream') }} + {% endblock %} +

+ {% snippet 'package/snippets/bulk_sucess.html', activity_stream=activity_stream, id=id, object_type='package' %} +{% endblock %} diff --git a/ckanext/bulkupload/templates/package/new_resource_not_draft.html b/ckanext/bulkupload/templates/package/new_resource_not_draft.html index 1caf0c4..bc3658e 100644 --- a/ckanext/bulkupload/templates/package/new_resource_not_draft.html +++ b/ckanext/bulkupload/templates/package/new_resource_not_draft.html @@ -14,7 +14,7 @@ {% block content_primary_nav %}
  • {{ _('New resource') }}
  • -
  • {% link_for _('Bulk Upload'), named_route='bulkupload.bulk_upload', pkg_dict=pkg_dict, pkg_name=pkg_name, icon='plus-square' %}
  • +
  • {% link_for _('Bulk Upload'), named_route='bulkupload.bulk_resource_upload', pkg_name=pkg_name, icon='plus-square' %}
  • {% endblock %} {% block secondary_content %} diff --git a/ckanext/bulkupload/templates/package/new_resource_not_draft_bulk.html b/ckanext/bulkupload/templates/package/new_resource_not_draft_bulk.html index bd5a741..e7a462e 100644 --- a/ckanext/bulkupload/templates/package/new_resource_not_draft_bulk.html +++ b/ckanext/bulkupload/templates/package/new_resource_not_draft_bulk.html @@ -1,15 +1,29 @@ {% extends "package/resource_edit_base.html" %} +{% import 'macros/form.html' as form %} {% block subtitle %}{{ _('Add resource') }} {{ g.template_title_delimiter }} {{ h.dataset_display_name(pkg) }}{% endblock %} {% block form_title %}{{ _('Add resource') }}{% endblock %} + {% block content_primary_nav %} -
  • {{ _('New resource') }}
  • -
  • {% link_for _('Bulk Upload'), named_route='bulkupload.bulk_upload', pkg_dict=pkg_dict, pkg_name=pkg_name, icon='plus-square' %}
  • +
  • {% link_for _('New resource'), named_route=pkg_dict.type ~ '_resource.new', id=pkg_dict.name, icon='fa fa-pencil-square-o' %}
  • +
  • {% link_for _('Bulk Upload'), named_route='bulkupload.bulk_resource_upload', pkg_dict=pkg_dict, pkg_name=pkg_name, icon='plus-square' %}
  • {% endblock %} {% block secondary_content %} {% snippet 'package/snippets/resource_help.html' %} {% endblock %} + +{% block form %} +
    + + + {{ form.textarea('desc', id='field-description', label=_('Description'), value='') }} + +
    + +
    +
    +{% endblock %} \ No newline at end of file diff --git a/ckanext/bulkupload/templates/package/snippets/bulk_sucess.html b/ckanext/bulkupload/templates/package/snippets/bulk_sucess.html new file mode 100644 index 0000000..aad3e48 --- /dev/null +++ b/ckanext/bulkupload/templates/package/snippets/bulk_sucess.html @@ -0,0 +1,3 @@ +

    {{ _("Bulk upload finished") }}

    +
    +

    {{ _("Notice: Reloading this page will send the same request again and repeat the bulk upload") }}

    \ No newline at end of file diff --git a/ckanext/bulkupload/templates/package/snippets/test.html b/ckanext/bulkupload/templates/package/snippets/test.html deleted file mode 100644 index 5634956..0000000 --- a/ckanext/bulkupload/templates/package/snippets/test.html +++ /dev/null @@ -1,7 +0,0 @@ -{% import 'macros/form.html' as form %} - -{% set data = data or {} %} -{% set errors = errors or {} %} - - -

    Bulk Upload

    diff --git a/ckanext/bulkupload/templates/package/test.html b/ckanext/bulkupload/templates/package/test.html deleted file mode 100644 index 7719e4b..0000000 --- a/ckanext/bulkupload/templates/package/test.html +++ /dev/null @@ -1 +0,0 @@ -

    Bulk Upload

    diff --git a/ckanext/bulkupload/templates/package/upload_bulk_sucess.html b/ckanext/bulkupload/templates/package/upload_bulk_sucess.html deleted file mode 100644 index bd5a741..0000000 --- a/ckanext/bulkupload/templates/package/upload_bulk_sucess.html +++ /dev/null @@ -1,15 +0,0 @@ -{% extends "package/resource_edit_base.html" %} - -{% block subtitle %}{{ _('Add resource') }} {{ g.template_title_delimiter }} {{ h.dataset_display_name(pkg) }}{% endblock %} -{% block form_title %}{{ _('Add resource') }}{% endblock %} - - - -{% block content_primary_nav %} -
  • {{ _('New resource') }}
  • -
  • {% link_for _('Bulk Upload'), named_route='bulkupload.bulk_upload', pkg_dict=pkg_dict, pkg_name=pkg_name, icon='plus-square' %}
  • -{% endblock %} - -{% block secondary_content %} - {% snippet 'package/snippets/resource_help.html' %} -{% endblock %} diff --git a/ckanext/bulkupload/views.py b/ckanext/bulkupload/views.py index 456e1b4..bb566e8 100644 --- a/ckanext/bulkupload/views.py +++ b/ckanext/bulkupload/views.py @@ -1,26 +1,44 @@ from flask import Blueprint -from flask.views import MethodView - - +from flask import request +from ckan.common import config import ckan.model as model import ckan.logic as logic import ckan.plugins.toolkit as tk import ckan.lib.base as base +import logging +import os +from pathlib import Path + +from ckan.common import g +from ckan.logic.action import get +import flask -from ckan.common import g, request -from ckan.logic.action import get, update +log = logging.getLogger(__name__) + +get_action = logic.get_action bulkupload = Blueprint("bulkupload", __name__) +try: + storage_path = config.get('ckan.storage_path') +except: + log.critical('''Please specify a ckan.storage_path in your config + for your uploads''') -class BulkResourceUpload(MethodView): +def bulk_resource_upload(pkg_name): - def get(self, pkg_name): + if flask.request.method == 'GET': context = { "model": model, "session": model.Session, "user": g.user, + "auth_user_obj": g.userobj, } + try: + tk.check_access("bulk_resource_upload", context) + except: + return tk.abort(403) + pkg_name_dict = { 'id': pkg_name, } @@ -32,11 +50,47 @@ def get(self, pkg_name): 'pkg_dict': pkg_dict, } ) - - def post(self, pkg_name): - return tk.render("package/upload_bulk_sucess.html") + elif flask.request.method == 'POST': + context = { + "model": model, + "session": model.Session, + "user": g.user, + "auth_user_obj": g.userobj, + } + try: + tk.check_access("bulk_resource_upload", context) + except: + return tk.abort(403) + + pkg_name_dict = { + 'id': pkg_name, + } + pkg_dict = get.package_show(context, pkg_name_dict) + uploaded_files = flask.request.files.getlist("file[]") + for f in uploaded_files: + + data_dict = { + 'package_id': pkg_name, + 'name': f.filename, + 'url': f.filename, + 'url_type': 'upload', + } + + x = tk.get_action("resource_create")(context, data_dict) + upload_path = storage_path + '/resources/' + x['id'][0:3] + "/" + x['id'][3:6] + upload_filename = x['id'][6:] + filepath = Path(os.path.join(upload_path, upload_filename)) + filepath.parent.mkdir(parents=True, exist_ok=True) + f.save(os.path.join(upload_path, upload_filename)) + + extra_vars= { + 'pkg_dict': pkg_dict + } + return base.render( + 'package/activity_bulk.html', extra_vars + ) bulkupload.add_url_rule("/dataset//resource/new/bulkupload", - view_func=BulkResourceUpload.as_view(str("bulk_upload")), + view_func=bulk_resource_upload, methods=("GET", "POST")) diff --git a/setup.py b/setup.py index a5f0edf..38443f1 100644 --- a/setup.py +++ b/setup.py @@ -45,7 +45,7 @@ # Specify the Python versions you support here. In particular, ensure # that you indicate whether you support Python 2, Python 3 or both. - 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3.8', ],