Skip to content

Commit

Permalink
Merge branch 'after-resource' into custom_view
Browse files Browse the repository at this point in the history
  • Loading branch information
aiman committed Feb 12, 2025
2 parents 4d7633a + ea23ffb commit 0eb8250
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
17 changes: 16 additions & 1 deletion ckanext/afucn/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit
from ckan.lib.plugins import DefaultTranslation
from ckanext.afucn.subresource import create_subresource
from ckan.common import config

subresource = config.get('ckanext.afucn.subresource', False)

# --------------------------------------------------------------------
# Original Plugin Class
Expand All @@ -9,6 +13,9 @@ class AfucnPlugin(plugins.SingletonPlugin, DefaultTranslation):
plugins.implements(plugins.IConfigurer)
plugins.implements(plugins.IFacets)
plugins.implements(plugins.ITranslation)
plugins.implements(plugins.IResourceController, inherit=True)

# IConfigurer

# IConfigurer
def update_config(self, config_):
Expand Down Expand Up @@ -43,6 +50,12 @@ def organization_facets(self, facets_dict, organization_type, package_type):
facets_dict['res_format'] = toolkit._('Format')
facets_dict['license_id'] = toolkit._('License')
return facets_dict

# IResourceController
def after_resource_create(self, context, resource_dict):
if subresource:
create_subresource(context, resource_dict)
return

# --------------------------------------------------------------------
# New Resource View: Portal Map
Expand Down Expand Up @@ -119,4 +132,6 @@ def view_config(self, context, data_dict):
return {}

def order(self):
return 3
return 3


87 changes: 87 additions & 0 deletions ckanext/afucn/subresource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import ckan.plugins.toolkit as tk
from ckan.common import config
import os
import logging
from pathlib import Path
from openpyxl import load_workbook
import shutil
import tempfile


log = logging.getLogger(__name__)

try:
storage_path = config.get('ckan.storage_path')
except:
log.critical('''Please specify a ckan.storage_path in your config
for your uploads''')


def get_resource_path(resource_id):

resource_path = storage_path + "/resources/" + resource_id[0:3] + "/" + resource_id[3:6] + "/" + resource_id[6:]

return resource_path

def create_subresource(context, resource_dict):

package_data = {
'id': resource_dict['package_id'],
}

resources_list = tk.get_action("package_show")(context, package_data)['resources']

subresource_name = resource_dict['name'].rsplit('.', 1)[0] + '.geojson'
names_list = []

for i in resources_list:
names_list.append(i['name'])
if subresource_name not in names_list:

data_dict = {
'package_id': resource_dict['package_id'],
'name': subresource_name,
'url': subresource_name,
'url_type': 'upload',
}

resource_path = get_resource_path(resource_dict['id'])
resource_copy_path = resource_path + ".xlsx"

# Copy the file to the same location with .xls extension
shutil.copy(resource_path, resource_copy_path)

# Load the excel file from copied resource
wb = load_workbook(resource_copy_path)
ws = wb['Sheet1']

for idx, row in enumerate(ws.iter_rows()):
geojson_text = row[1].value

f = tempfile.NamedTemporaryFile(mode='w+t', delete=False)
f.write(geojson_text)
file_name = f.name
f.close()

# Create the map resource from the excel file and
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:]
Path(upload_path).mkdir(parents=True, exist_ok=True)
filepath = Path(os.path.join(upload_path, upload_filename))
shutil.copy(file_name, filepath)

# remove temp xls resource from resources directory
os.remove(resource_copy_path)
# remove temp subresource file
os.remove(file_name)

return resource_dict

else:
print("======================================================================")
print('subresource not created')
print("======================================================================")

return resource_dict

0 comments on commit 0eb8250

Please sign in to comment.