Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bulk resource upload function view #1

Merged
merged 11 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ckanext/bulkupload/actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import ckan.plugins.toolkit as tk

def call_add_resouce_api(*args, **kwargs):

print("============================1==========================")

2 changes: 2 additions & 0 deletions ckanext/bulkupload/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def bulk_resource_upload(context, data_dict):
return {'success': False}
64 changes: 64 additions & 0 deletions ckanext/bulkupload/i18n/ckanext-bulkupload.pot
Original file line number Diff line number Diff line change
@@ -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 <EMAIL@ADDRESS>, 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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\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 ""

Binary file not shown.
67 changes: 67 additions & 0 deletions ckanext/bulkupload/i18n/es/LC_MESSAGES/ckanext-bulkupload.po
Original file line number Diff line number Diff line change
@@ -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 <EMAIL@ADDRESS>, 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 <EMAIL@ADDRESS>\n"
"Language: es\n"
"Language-Team: es <[email protected]>\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"

26 changes: 24 additions & 2 deletions ckanext/bulkupload/plugin.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
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

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):
Expand Down
12 changes: 12 additions & 0 deletions ckanext/bulkupload/templates/package/activity_bulk.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends "package/read_base.html" %}

{% block subtitle %}{{ _('Activity Stream') }} {{ g.template_title_delimiter }} {{ super() }}{% endblock %}

{% block primary_content_inner %}
<h1 class="hide-heading">
{% block page_heading %}
{{ _('Activity Stream') }}
{% endblock %}
</h1>
{% snippet 'package/snippets/bulk_sucess.html', activity_stream=activity_stream, id=id, object_type='package' %}
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

{% block content_primary_nav %}
<li class="active"><a href="#"><i class="fa fa-pencil-square-o"></i> {{ _('New resource') }}</a></li>
<li class="">{% link_for _('Bulk Upload'), named_route='bulkupload.bulk_upload', pkg_dict=pkg_dict, pkg_name=pkg_name, icon='plus-square' %}</li>
<li class="">{% link_for _('Bulk Upload'), named_route='bulkupload.bulk_resource_upload', pkg_name=pkg_name, icon='plus-square' %}</li>
{% endblock %}

{% block secondary_content %}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 %}
<li class=""><a href="#"><i class="fa fa-pencil-square-o"></i> {{ _('New resource') }}</a></li>
<li class="active">{% link_for _('Bulk Upload'), named_route='bulkupload.bulk_upload', pkg_dict=pkg_dict, pkg_name=pkg_name, icon='plus-square' %}</li>
<li class="">{% link_for _('New resource'), named_route=pkg_dict.type ~ '_resource.new', id=pkg_dict.name, icon='fa fa-pencil-square-o' %}</li>
<li class="active">{% link_for _('Bulk Upload'), named_route='bulkupload.bulk_resource_upload', pkg_dict=pkg_dict, pkg_name=pkg_name, icon='plus-square' %}</li>
{% endblock %}

{% block secondary_content %}
{% snippet 'package/snippets/resource_help.html' %}
{% endblock %}

{% block form %}
<form id="bulk_upload_form" method="post" enctype="multipart/form-data">

<input syle="margin-bottom:20px;" class ='btn btn-default' type="file" name="file[]" multiple>
{{ form.textarea('desc', id='field-description', label=_('Description'), value='') }}

<div style="display:flex; gap:10px">
<button type='submit', class ='btn btn-primary', icon='plus-square'>{{_('Upload')}}</button>
</div>
</form>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h3>{{ _("Bulk upload finished") }}</h3>
</br>
<p><small>{{ _("Notice: Reloading this page will send the same request again and repeat the bulk upload") }}</small></p>
7 changes: 0 additions & 7 deletions ckanext/bulkupload/templates/package/snippets/test.html

This file was deleted.

1 change: 0 additions & 1 deletion ckanext/bulkupload/templates/package/test.html

This file was deleted.

15 changes: 0 additions & 15 deletions ckanext/bulkupload/templates/package/upload_bulk_sucess.html

This file was deleted.

76 changes: 65 additions & 11 deletions ckanext/bulkupload/views.py
Original file line number Diff line number Diff line change
@@ -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,
}
Expand All @@ -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/<pkg_name>/resource/new/bulkupload",
view_func=BulkResourceUpload.as_view(str("bulk_upload")),
view_func=bulk_resource_upload,
methods=("GET", "POST"))
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],


Expand Down
Loading