use blueprint instead of controllers

This commit is contained in:
Alessio Fabrizio 2024-10-11 16:00:20 +02:00
parent e37a17838d
commit ec62dc0c76
9 changed files with 287 additions and 335 deletions

View File

@ -1,5 +1,6 @@
import logging
from ckan.controllers.home import HomeController
#import logging
#from ckan.controllers.home import HomeController
from flask import Blueprint, render_template, g
import ckan.plugins as p
from ckan.common import OrderedDict, _, g, c
import ckan.lib.search as search
@ -9,13 +10,15 @@ import ckan.lib.maintain as maintain
import ckan.lib.base as base
import ckan.lib.helpers as h
class d4SHomeController():
#blueprint definition
d4science_home = Blueprint("d4science_home", __name__)
#Overriding controllers.HomeController.index method
def index(self):
#@d4science_home.route("/catalog")
@d4science_home.route("/")
def index():
try:
# package search
context = {'model': model, 'session': model.Session,'user': c.user, 'auth_user_obj': c.userobj}
context = {'model': model, 'session': model.Session,'user': g.user, 'auth_user_obj': g.userobj}
facets = OrderedDict()
@ -33,11 +36,11 @@ class d4SHomeController():
else:
facets[facet] = facet
# Facet titles
# gestion filtri/facets
for plugin in p.PluginImplementations(p.IFacets):
facets = plugin.dataset_facets(facets, 'dataset')
c.facet_titles = facets
g.facet_titles = facets
data_dict = {
'q': '*:*',
@ -48,18 +51,18 @@ class d4SHomeController():
'fq': 'capacity:"public"'
}
query = logic.get_action('package_search')(context, data_dict)
c.search_facets = query['search_facets']
c.package_count = query['count']
c.datasets = query['results']
g.search_facets = query['search_facets']
g.package_count = query['count']
g.datasets = query['results']
#print "c.search_facets: "
#print " ".join(c.search_facets)
except search.SearchError:
c.package_count = 0
g.package_count = 0
if c.userobj and not c.userobj.email:
url = h.url_for(controller='user', action='edit')
if g.userobj and not g.userobj.email:
url = h.url_for('user.edit')
msg = _('Please <a href="%s">update your profile</a>'
' and add your email address. ') % url + \
_('%s uses your email address'
@ -67,5 +70,5 @@ class d4SHomeController():
% g.site_title
h.flash_notice(msg, allow_html=True)
return base.render('home/index.html', cache_force=True)
return render_template('home/index.html', cache_force=True)

View File

@ -1,32 +1,16 @@
# encoding: utf-8
import re
import ckan.controllers.group as group
import ckan.plugins as plugins
import logging
import datetime
from urllib.parse import urlencode
from pylons.i18n import get_lang
import ckan.lib.base as base
import ckan.lib.helpers as h
import ckan.lib.maintain as maintain
import ckan.lib.navl.dictization_functions as dict_fns
import ckan.logic as logic
import ckan.lib.search as search
import ckan.model as model
import ckan.authz as authz
import ckan.lib.plugins
from flask import Blueprint, g, request, abort, render_template
import ckan.plugins as plugins
from ckan.common import OrderedDict, c, g, request, _
import ckan.logic as logic
import ckan.model as model
import ckan.lib.helpers as h
import ckan.lib.search as search
from ckan.common import OrderedDict, _, NotAuthorized, NotFound
organization_vre = Blueprint("organization_vre", __name__)
'''
Created by Francesco Mangiacrapa, see: #8964
'''
class OrganizationVREController(group.GroupController):
''' The organization controller is for Organizations, which are implemented
as Groups with is_organization=True and group_type='organization'. It works
the same as the group controller apart from:
@ -40,41 +24,41 @@ class OrganizationVREController(group.GroupController):
group_types = ['organization']
def _guess_group_type(self, expecting_name=False):
def _guess_group_type(expecting_name=False):
return 'organization'
def _replace_group_org(self, string):
def _replace_group_org( string):
''' substitute organization for group if this is an org'''
return re.sub('^group', 'organization', string)
def _update_facet_titles(self, facets, group_type):
def _update_facet_titles(facets, group_type):
for plugin in plugins.PluginImplementations(plugins.IFacets):
facets = plugin.organization_facets(
facets, group_type, None)
def index(self):
group_type = self._guess_group_type()
page = h.get_page_number(request.params) or 1
@organization_vre.route('/organization_vre')
def index():
group_type = _guess_group_type()
page = h.get_page_number(request.args) or 1
items_per_page = 21
context = {'model': model, 'session': model.Session,
'user': c.user, 'for_view': True,
'user': g.user, 'for_view': True,
'with_private': False}
q = c.q = request.params.get('q', '')
sort_by = c.sort_by_selected = request.params.get('sort')
q = g.q = request.params.get('q', '')
sort_by = g.sort_by_selected = request.args.get('sort')
try:
self._check_access('site_read', context)
self._check_access('group_list', context)
logic.check_access('site_read', context)
logic.check_access('group_list', context)
except NotAuthorized:
abort(403, _('Not authorized to see this page'))
# pass user info to context as needed to view private datasets of
# orgs correctly
if c.userobj:
context['user_id'] = c.userobj.id
context['user_is_admin'] = c.userobj.sysadmin
if g.userobj:
context['user_id'] = g.userobj.id
context['user_is_admin'] = g.userobj.sysadmin
data_dict_global_results = {
'all_fields': False,
@ -82,8 +66,7 @@ class OrganizationVREController(group.GroupController):
'sort': sort_by,
'type': group_type or 'group',
}
global_results = self._action('group_list')(context,
data_dict_global_results)
global_results = logic.get_action('group_list')(context,data_dict_global_results)
data_dict_page_results = {
'all_fields': True,
@ -93,43 +76,43 @@ class OrganizationVREController(group.GroupController):
'limit': items_per_page,
'offset': items_per_page * (page - 1),
}
page_results = self._action('group_list')(context,
data_dict_page_results)
page_results = logic.get_action('group_list')(context, data_dict_page_results)
c.page = h.Page(
g.page = h.Page(
collection=global_results,
page=page,
url=h.pager_url,
items_per_page=items_per_page,
)
c.page.items = page_results
return base.render('organization_vre/index.html',
g.page.items = page_results
return render_template('organization_vre/index.html',
extra_vars={'group_type': group_type})
def read(self, id, limit=20):
group_type = self._ensure_controller_matches_group_type(
id.split('@')[0])
@organization_vre.route('/organization_vre/<id>')
def read(id, limit=20):
#group_type = self._ensure_controller_matches_group_type(
# id.split('@')[0])
group_type = 'organization'
context = {'model': model, 'session': model.Session,
'user': c.user,
'schema': self._db_to_form_schema(group_type=group_type),
'user': g.user,
'schema': logic.schema.group_form_schema(),
'for_view': True}
data_dict = {'id': id, 'type': group_type}
# unicode format (decoded from utf8)
c.q = request.params.get('q', '')
# recupero eventuale query di ricerca
g.q = request.args.get('q', '')
try:
# Do not query for the group datasets when dictizing, as they will
# be ignored and get requested on the controller anyway
#i dataset non si includono nel risultato
data_dict['include_datasets'] = False
c.group_dict = self._action('group_show')(context, data_dict)
c.group = context['group']
g.group_dict = logic.get_action('group_show')(context, data_dict)
g.group = context['group']
except (NotFound, NotAuthorized):
abort(404, _('Group not found'))
self._read(id, limit, group_type)
return base.render('organization_vre/read.html',
#read(id, limit, group_type)
return render_template('organization_vre/read.html',
extra_vars={'group_type': group_type})

View File

@ -1,27 +1,18 @@
import logging
import ckan.plugins as p
from ckan.common import OrderedDict, _, g, c
from ckan.common import OrderedDict, _
import ckan.lib.search as search
import ckan.model as model
import ckan.logic as logic
import ckan.lib.maintain as maintain
import ckan.lib.base as base
import ckan.lib.helpers as h
from flask import Blueprint, render_template, request, g
from ckan.lib.search import SearchError
from urllib.parse import urlencode
#from pylons.i18n import get_lang
from flask import Blueprint, render_template, g, request
d4s_type_blueprint = Blueprint('d4s_type', __name__)
import ckan.lib.base as base
import ckan.lib.navl.dictization_functions as dict_fns
import ckan.authz as authz
class d4STypeController(base.BaseController):
#Overriding controllers.HomeController.index method
def index(self):
@d4s_type_blueprint.route('/')
def index():
try:
# package search
context = {'model': model, 'session': model.Session,'user': g.user, 'auth_user_obj': g.userobj}
@ -80,9 +71,3 @@ class d4STypeController(base.BaseController):
#return base.render('type/index.html', cache_force=True) pylons
return render_template('type/index.html', cache_force=True)
d4s_type_blueprint = Blueprint('d4s_type', __name__)
@d4s_type_blueprint.route('/')
def index():
controller = d4STypeController()
return controller.index()

View File

@ -14,10 +14,6 @@ namespaces_dir = None
NAMESPACES_DIR_NAME = "namespaces_for_catalogue"
NAMESPACES_CACHE_FILENAME = "Namespaces_Catalogue_Categories.csv"
# Created by Francesco Mangiacrapa
# francesco.mangiacrapa@isti.cnr.it
# ISTI-CNR Pisa (ITALY)
# D4S_Cache_Controller
class D4S_Cache_Controller():

View File

@ -1,8 +1,3 @@
# Created by Francesco Mangiacrapa
# francesco.mangiacrapa@isti.cnr.it
# ISTI-CNR Pisa (ITALY)
## questo file va bene anche in p3 ##
import logging
log = logging.getLogger(__name__)

View File

@ -44,13 +44,8 @@ def reload_namespaces_from_IS(urlICProxy, resourceID, gcubeToken):
log.info("namespaces list read from IS is empty. Skipping caching update")
except Exception as e:
print("Error occurred on reading namespaces from IS and refilling the cache!")
print(e)
# Created by Francesco Mangiacrapa
# francesco.mangiacrapa@isti.cnr.it
# ISTI-CNR Pisa (ITALY)
log.error("Error occurred on reading namespaces from IS and refilling the cache!")
log.error(e)
# D4S_IS_DiscoveryCatalogueNamespacesController is used to discovery namespaces for Catalogue Categories (implemented as a Singleton)

View File

@ -7,11 +7,6 @@ NOCATEOGORY = 'nocategory'
log = logging.getLogger(__name__)
# Created by Francesco Mangiacrapa
# francesco.mangiacrapa@isti.cnr.it
# ISTI-CNR Pisa (ITALY)
# D4S_Namespaces_Extra_Util is used to get the extra fields indexed for D4Science namespaces
# @param: namespace_dict is the namespace dict of D4Science namespaces (defined in the Generic Resource: "Namespaces Catalogue Categories")
# @param: extras is the dictionary of extra fields for a certain item
@ -25,7 +20,7 @@ class D4S_Namespaces_Extra_Util():
dict_extras = None
nms = namespaceid + ":"
#has_namespace_ref = None
for key, value in extras:
for key, value in extras.items():
k = key
v = value
# print "key: " + k
@ -53,7 +48,7 @@ class D4S_Namespaces_Extra_Util():
#break
#ADDING ALL EXTRAS WITHOUT NAMESPACE
for key, value in extras:
for key, value in extras.items():
k = key
v = value

View File

@ -13,11 +13,6 @@ NAMESPACE_ID_LABEL = '@id'
log = logging.getLogger(__name__)
# Created by Francesco Mangiacrapa
# francesco.mangiacrapa@isti.cnr.it
# ISTI-CNR Pisa (ITALY)
def getResponseBody(uri):
req = urllib.request.Request(uri)
try:
@ -43,6 +38,12 @@ def getResponseBody(uri):
class D4S_IS_DiscoveryCatalogueNamespaces():
def __init__(self, urlICProxy, resourceID, gcubeToken):
if not isinstance(urlICProxy, str):
raise ValueError("urlICProxy must be a string")
if not isinstance(resourceID, str):
raise ValueError("resourceID must be a string")
if not isinstance(gcubeToken, str):
raise ValueError("gcubeToken must be a string")
self.urlICProxy = urlICProxy
self.resourceID = resourceID
self.gcubeToken = gcubeToken
@ -53,10 +54,6 @@ class D4S_IS_DiscoveryCatalogueNamespaces():
namespace_list = []
try:
# print("proxy: "+self.urlICProxy)
# print("resourceID: " + self.resourceID)
# print("gcubeTokenParam: " + gcubeTokenParam)
# print("gcubeToken: " + self.gcubeToken)
uri = self.urlICProxy + "/" + self.resourceID + "?" + gcubeTokenParam + "=" + self.gcubeToken
log.info("Contacting URL: %s" % uri)

View File

@ -26,14 +26,14 @@ def remove_check_replicated_custom_key(schema):
def _package_extras_save(extra_dicts, obj, context):
''' It can save repeated extras as key-value '''
allow_partial_update = context.get("allow_partial_update", False)
if extra_dicts is None and allow_partial_update:
#allow_partial_update = context.get("allow_partial_update", False) potrebbe non servire
if extra_dicts is None: #and allow_partial_update:
return
model = context["model"]
session = context["session"]
log.debug("extra_dicts: "+unicode(str(extra_dicts)).encode('utf-8'))
log.debug("extra_dicts: "+ str(extra_dicts))
#print "extra_dicts: "+str(extra_dicts)
extras_list = obj.extras_list
@ -59,118 +59,121 @@ def _package_extras_save(extra_dicts, obj, context):
if extra_dict['value'] is not None:
new_extras.setdefault(extra_dict["key"], []).append(extra_dict["value"])
log.debug("new_extras: "+unicode(str(new_extras)).encode('utf-8'))
log.debug("new_extras: "+ str(new_extras))
#print "new_extras: "+str(new_extras)
#new
#aggiunta di nuove chiavi
for key in set(new_extras.keys()) - set(old_extras.keys()):
state = 'active'
log.debug("adding key: "+unicode(key).encode('utf-8'))
#state = 'active'
log.debug("adding key: " + str(key))
#print "adding key: "+str(key)
extra_lst = new_extras[key]
for extra in extra_lst:
extra = model.PackageExtra(state=state, key=key, value=extra)
extra = model.PackageExtra(state='active', key=key, value=extra)
session.add(extra)
extras_list.append(extra)
#deleted
#gestione chiavi eliminate
for key in set(old_extras.keys()) - set(new_extras.keys()):
log.debug("deleting key: "+unicode(key).encode('utf-8'))
log.debug("deleting key: "+ str(key))
#print "deleting key: "+str(key)
extra_lst = extras[key]
for extra in extra_lst:
state = 'deleted'
extra.state = state
#state = 'deleted'
extra.state = 'deleted'
extras_list.remove(extra)
#changed
#gestione chiavi aggiornate
for key in set(new_extras.keys()) & set(old_extras.keys()):
#for each value of new list
for value in new_extras[key]:
old_occur = old_extras[key].count(value)
new_occur = new_extras[key].count(value)
log.debug("value: "+unicode(value).encode('utf-8') + ", new_occur: "+unicode(new_occur).encode('utf-8')+ ", old_occur: "+unicode(old_occur).encode('utf-8'))
log.debug("value: " + str(value) + ", new_occur: "+ str(new_occur)+ ", old_occur: "+ str(old_occur))
#print "value: "+str(value) + ", new_occur: "+str(new_occur) + ", old_occur: "+str(old_occur)
# it is an old value deleted or not
if value in old_extras[key]:
if old_occur == new_occur:
#print "extra - occurrences of: "+str(value) +", are equal into both list"
log.debug("extra - occurrences of: "+unicode(value).encode('utf-8') +", are equal into both list")
log.debug("extra - occurrences of: "+ str(value) +", are equal into both list")
#there is a little bug, this code return always the first element, so I'm fixing with #FIX-STATUS
extra_values = get_package_for_value(extras[key], value)
#extras_list.append(extra)
for extra in extra_values:
state = 'active'
extra.state = state
#state = 'active'
extra.state = 'active'
session.add(extra)
#print "extra updated: "+str(extra)
log.debug("extra updated: "+unicode(extra).encode('utf-8'))
log.debug("extra updated: "+ str(extra))
elif new_occur > old_occur:
#print "extra - a new occurrence of: "+str(value) +", is present into new list, adding it to old list"
log.debug("extra - a new occurrence of: "+unicode(value).encode('utf-8') +", is present into new list, adding it to old list")
state = 'active'
extra = model.PackageExtra(state=state, key=key, value=value)
extra.state = state
log.debug("extra - a new occurrence of: "+ str(value) + ", is present into new list, adding it to old list")
#state = 'active'
extra = model.PackageExtra(state='active', key=key, value=value)
#extra.state = state
#extra.state = 'active' non dovrebbe servire
session.add(extra)
extras_list.append(extra)
old_extras[key].append(value)
log.debug("old extra values updated: "+unicode(old_extras[key]).encode('utf-8'))
log.debug("old extra values updated: "+ str(old_extras[key]))
#print "old extra values updated: "+str(old_extras[key])
else:
#remove all occurrences deleted - this code could be optimized, it is run several times but could be performed one shot
countDelete = old_occur-new_occur
log.debug("extra - occurrence of: "+unicode(value).encode('utf-8')+", is not present into new list, removing "+unicode(countDelete).encode('utf-8')+" occurrence/s from old list")
log.debug("extra - occurrence of: "+ str(value).encode('utf-8') + ", is not present into new list, removing "+ str(countDelete).encode('utf-8')+" occurrence/s from old list")
#print "extra - occurrence of: "+str(value) +", is not present into new list, removing "+str(countDelete)+" occurrence/s from old list"
extra_values = get_package_for_value(extras[key], value)
for idx, extra in enumerate(extra_values):
if idx < countDelete:
#print "extra - occurrence of: "+str(value) +", is not present into new list, removing it from old list"
log.debug("pkg extra deleting: "+unicode(extra.value).encode('utf-8'))
log.debug("pkg extra deleting: "+ str(extra.value))
#print "pkg extra deleting: "+str(extra.value)
state = 'deleted'
extra.state = state
#state = 'deleted'
extra.state = 'deleted'
else:
#print "pkg extra reactivating: "+str(extra.value)
log.debug("pkg extra reactivating: "+unicode(extra.value).encode('utf-8'))
state = 'active'
extra.state = state
log.debug("pkg extra reactivating: "+ str(extra.value))
#state = 'active'
extra.state = 'active'
session.add(extra)
else:
#print "extra new value: "+str(value)
log.debug("extra new value: "+unicode(value).encode('utf-8'))
state = 'active'
extra = model.PackageExtra(state=state, key=key, value=value)
extra.state = state
log.debug("extra new value: " + str(value))
#state = 'active'
extra = model.PackageExtra(state='active', key=key, value=value)
#extra.state = state
#extra.state = 'active'
session.add(extra)
extras_list.append(extra)
#for each value of old list
#chiavi vecchie non presenti
for value in old_extras[key]:
#if value is not present in new list
if value not in new_extras[key]:
extra_values = get_package_for_value(extras[key], value)
for extra in extra_values:
#print "not present extra deleting: "+str(extra)
log.debug("not present extra deleting: "+unicode(extra).encode('utf-8'))
state = 'deleted'
extra.state = state
log.debug("not present extra deleting: "+ str(extra))
#state = 'deleted'
extra.state = 'deleted'
def get_package_for_value(list_package, value):
''' Returns a list of packages containing the value passed in input
'''
lst = []
for x in list_package:
if x.value == value:
lst.append(x)
else:
return lst
''' Returns a list of packages containing the value passed in input'''
return lst
return [x for x in list_package if x.value == value]
#lst = []
#for x in list_package:
# if x.value == value:
# lst.append(x)
# else:
# return lst
#
#return lst
#OVERRIDING BASE SQL ALCHEMY ENGINE INSTANCE
@ -189,7 +192,7 @@ def _init_TrackingMiddleware(self, app, config):
sqlalchemy_overflow = config.get('sqlalchemy.max_overflow')
if sqlalchemy_overflow is None:
sqlalchemy_overflow = 10;
sqlalchemy_overflow = 10
log.debug('sqlalchemy_overflow read: '+str(sqlalchemy_overflow))
@ -244,7 +247,7 @@ class D4SciencePlugin(plugins.SingletonPlugin):
def package_types(self):
# Aggiunta del tipo di dato personalizzato deliverable
return ['deliverable_type']
return []
def is_fallback(self):
# Indica che questo plugin può essere usato come fallback se un tipo specifico non è specificato