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 #import logging
from ckan.controllers.home import HomeController #from ckan.controllers.home import HomeController
from flask import Blueprint, render_template, g
import ckan.plugins as p import ckan.plugins as p
from ckan.common import OrderedDict, _, g, c from ckan.common import OrderedDict, _, g, c
import ckan.lib.search as search import ckan.lib.search as search
@ -9,63 +10,65 @@ import ckan.lib.maintain as maintain
import ckan.lib.base as base import ckan.lib.base as base
import ckan.lib.helpers as h import ckan.lib.helpers as h
class d4SHomeController(): #blueprint definition
d4science_home = Blueprint("d4science_home", __name__)
#Overriding controllers.HomeController.index method #@d4science_home.route("/catalog")
def index(self): @d4science_home.route("/")
try: def index():
# package search try:
context = {'model': model, 'session': model.Session,'user': c.user, 'auth_user_obj': c.userobj} # package search
context = {'model': model, 'session': model.Session,'user': g.user, 'auth_user_obj': g.userobj}
facets = OrderedDict() facets = OrderedDict()
default_facet_titles = { default_facet_titles = {
'organization': _('Organizations'), 'organization': _('Organizations'),
'groups': _('Groups'), 'groups': _('Groups'),
'tags': _('Tags'), 'tags': _('Tags'),
'res_format': _('Formats'), 'res_format': _('Formats'),
'license_id': _('Licenses'), 'license_id': _('Licenses'),
}
for facet in g.facets:
if facet in default_facet_titles:
facets[facet] = default_facet_titles[facet]
else:
facets[facet] = facet
# Facet titles
for plugin in p.PluginImplementations(p.IFacets):
facets = plugin.dataset_facets(facets, 'dataset')
c.facet_titles = facets
data_dict = {
'q': '*:*',
'facet.field': list(facets.keys()),
'rows': 4,
'start': 0,
'sort': 'views_recent desc',
'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']
#print "c.search_facets: " for facet in g.facets:
#print " ".join(c.search_facets) if facet in default_facet_titles:
facets[facet] = default_facet_titles[facet]
else:
facets[facet] = facet
except search.SearchError: # gestion filtri/facets
c.package_count = 0 for plugin in p.PluginImplementations(p.IFacets):
facets = plugin.dataset_facets(facets, 'dataset')
if c.userobj and not c.userobj.email: g.facet_titles = facets
url = h.url_for(controller='user', action='edit')
msg = _('Please <a href="%s">update your profile</a>'
' and add your email address. ') % url + \
_('%s uses your email address'
' if you need to reset your password.') \
% g.site_title
h.flash_notice(msg, allow_html=True)
return base.render('home/index.html', cache_force=True) data_dict = {
'q': '*:*',
'facet.field': list(facets.keys()),
'rows': 4,
'start': 0,
'sort': 'views_recent desc',
'fq': 'capacity:"public"'
}
query = logic.get_action('package_search')(context, data_dict)
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:
g.package_count = 0
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'
' if you need to reset your password.') \
% g.site_title
h.flash_notice(msg, allow_html=True)
return render_template('home/index.html', cache_force=True)

View File

@ -1,135 +1,118 @@
# encoding: utf-8 # encoding: utf-8
import re import re
import ckan.controllers.group as group
import ckan.plugins as plugins
import logging import logging
import datetime from flask import Blueprint, g, request, abort, render_template
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
import ckan.plugins as plugins 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__)
''' 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:
* templates and logic action/auth functions are sometimes customized
(switched using _replace_group_org)
* 'bulk_process' action only works for organizations
Nearly all the code for both is in the GroupController (for historical
reasons).
''' '''
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:
* templates and logic action/auth functions are sometimes customized
(switched using _replace_group_org)
* 'bulk_process' action only works for organizations
Nearly all the code for both is in the GroupController (for historical group_types = ['organization']
reasons).
'''
group_types = ['organization'] def _guess_group_type(expecting_name=False):
return 'organization'
def _guess_group_type(self, expecting_name=False): def _replace_group_org( string):
return 'organization' ''' substitute organization for group if this is an org'''
return re.sub('^group', 'organization', string)
def _replace_group_org(self, string): def _update_facet_titles(facets, group_type):
''' substitute organization for group if this is an org''' for plugin in plugins.PluginImplementations(plugins.IFacets):
return re.sub('^group', 'organization', string) facets = plugin.organization_facets(
facets, group_type, None)
def _update_facet_titles(self, facets, group_type): @organization_vre.route('/organization_vre')
for plugin in plugins.PluginImplementations(plugins.IFacets): def index():
facets = plugin.organization_facets( group_type = _guess_group_type()
facets, group_type, None) page = h.get_page_number(request.args) or 1
items_per_page = 21
context = {'model': model, 'session': model.Session,
'user': g.user, 'for_view': True,
'with_private': False}
def index(self): q = g.q = request.params.get('q', '')
group_type = self._guess_group_type() sort_by = g.sort_by_selected = request.args.get('sort')
page = h.get_page_number(request.params) or 1 try:
items_per_page = 21 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
context = {'model': model, 'session': model.Session, if g.userobj:
'user': c.user, 'for_view': True, context['user_id'] = g.userobj.id
'with_private': False} context['user_is_admin'] = g.userobj.sysadmin
q = c.q = request.params.get('q', '') data_dict_global_results = {
sort_by = c.sort_by_selected = request.params.get('sort') 'all_fields': False,
try: 'q': q,
self._check_access('site_read', context) 'sort': sort_by,
self._check_access('group_list', context) 'type': group_type or 'group',
except NotAuthorized: }
abort(403, _('Not authorized to see this page')) global_results = logic.get_action('group_list')(context,data_dict_global_results)
# pass user info to context as needed to view private datasets of data_dict_page_results = {
# orgs correctly 'all_fields': True,
if c.userobj: 'q': q,
context['user_id'] = c.userobj.id 'sort': sort_by,
context['user_is_admin'] = c.userobj.sysadmin 'type': group_type or 'group',
'limit': items_per_page,
'offset': items_per_page * (page - 1),
}
page_results = logic.get_action('group_list')(context, data_dict_page_results)
data_dict_global_results = { g.page = h.Page(
'all_fields': False, collection=global_results,
'q': q, page=page,
'sort': sort_by, url=h.pager_url,
'type': group_type or 'group', items_per_page=items_per_page,
} )
global_results = self._action('group_list')(context, g.page.items = page_results
data_dict_global_results) return render_template('organization_vre/index.html',
extra_vars={'group_type': group_type})
data_dict_page_results = {
'all_fields': True,
'q': q,
'sort': sort_by,
'type': group_type or 'group',
'limit': items_per_page,
'offset': items_per_page * (page - 1),
}
page_results = self._action('group_list')(context,
data_dict_page_results)
c.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',
extra_vars={'group_type': group_type})
def read(self, id, limit=20): @organization_vre.route('/organization_vre/<id>')
group_type = self._ensure_controller_matches_group_type( def read(id, limit=20):
id.split('@')[0]) #group_type = self._ensure_controller_matches_group_type(
# id.split('@')[0])
group_type = 'organization'
context = {'model': model, 'session': model.Session, context = {'model': model, 'session': model.Session,
'user': c.user, 'user': g.user,
'schema': self._db_to_form_schema(group_type=group_type), 'schema': logic.schema.group_form_schema(),
'for_view': True} 'for_view': True}
data_dict = {'id': id, 'type': group_type} data_dict = {'id': id, 'type': group_type}
# unicode format (decoded from utf8) # recupero eventuale query di ricerca
c.q = request.params.get('q', '') g.q = request.args.get('q', '')
try: try:
# Do not query for the group datasets when dictizing, as they will #i dataset non si includono nel risultato
# be ignored and get requested on the controller anyway data_dict['include_datasets'] = False
data_dict['include_datasets'] = False g.group_dict = logic.get_action('group_show')(context, data_dict)
c.group_dict = self._action('group_show')(context, data_dict) g.group = context['group']
c.group = context['group'] except (NotFound, NotAuthorized):
except (NotFound, NotAuthorized): abort(404, _('Group not found'))
abort(404, _('Group not found'))
#read(id, limit, group_type)
return render_template('organization_vre/read.html',
extra_vars={'group_type': group_type})
self._read(id, limit, group_type)
return base.render('organization_vre/read.html',
extra_vars={'group_type': group_type})

View File

@ -1,88 +1,73 @@
import logging import logging
import ckan.plugins as p 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.lib.search as search
import ckan.model as model import ckan.model as model
import ckan.logic as logic import ckan.logic as logic
import ckan.lib.maintain as maintain
import ckan.lib.base as base
import ckan.lib.helpers as h 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 urllib.parse import urlencode
#from pylons.i18n import get_lang
from flask import Blueprint, render_template, g, request
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):
try:
# package search
context = {'model': model, 'session': model.Session,'user': g.user, 'auth_user_obj': g.userobj}
facets = OrderedDict()
default_facet_titles = {
'organization': _('Organizations'),
'groups': _('Groups'),
'tags': _('Tags'),
'res_format': _('Formats'),
'license_id': _('Licenses'),
}
for facet in g.facets:
if facet in default_facet_titles:
facets[facet] = default_facet_titles[facet]
else:
facets[facet] = facet
# Facet titles
for plugin in p.PluginImplementations(p.IFacets):
facets = plugin.dataset_facets(facets, 'dataset')
g.facet_titles = facets
data_dict = {
'q': '*:*',
'facet.field': list(facets.keys()),
'rows': 4,
'start': 0,
'sort': 'views_recent desc',
'fq': 'capacity:"public"'
}
query = logic.get_action('package_search')(context, data_dict)
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:
g.package_count = 0
if g.userobj and not g.userobj.email:
#url = h.url_for(controller='user', action='edit') pylons
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'
' if you need to reset your password.') \
% g.site_title
h.flash_notice(msg, allow_html=True)
#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 = Blueprint('d4s_type', __name__)
@d4s_type_blueprint.route('/') @d4s_type_blueprint.route('/')
def index(): def index():
controller = d4STypeController() try:
return controller.index() # package search
context = {'model': model, 'session': model.Session,'user': g.user, 'auth_user_obj': g.userobj}
facets = OrderedDict()
default_facet_titles = {
'organization': _('Organizations'),
'groups': _('Groups'),
'tags': _('Tags'),
'res_format': _('Formats'),
'license_id': _('Licenses'),
}
for facet in g.facets:
if facet in default_facet_titles:
facets[facet] = default_facet_titles[facet]
else:
facets[facet] = facet
# Facet titles
for plugin in p.PluginImplementations(p.IFacets):
facets = plugin.dataset_facets(facets, 'dataset')
g.facet_titles = facets
data_dict = {
'q': '*:*',
'facet.field': list(facets.keys()),
'rows': 4,
'start': 0,
'sort': 'views_recent desc',
'fq': 'capacity:"public"'
}
query = logic.get_action('package_search')(context, data_dict)
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:
g.package_count = 0
if g.userobj and not g.userobj.email:
#url = h.url_for(controller='user', action='edit') pylons
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'
' if you need to reset your password.') \
% g.site_title
h.flash_notice(msg, allow_html=True)
#return base.render('type/index.html', cache_force=True) pylons
return render_template('type/index.html', cache_force=True)

View File

@ -14,10 +14,6 @@ namespaces_dir = None
NAMESPACES_DIR_NAME = "namespaces_for_catalogue" NAMESPACES_DIR_NAME = "namespaces_for_catalogue"
NAMESPACES_CACHE_FILENAME = "Namespaces_Catalogue_Categories.csv" NAMESPACES_CACHE_FILENAME = "Namespaces_Catalogue_Categories.csv"
# Created by Francesco Mangiacrapa
# francesco.mangiacrapa@isti.cnr.it
# ISTI-CNR Pisa (ITALY)
# D4S_Cache_Controller # D4S_Cache_Controller
class 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 import logging
log = logging.getLogger(__name__) 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") log.info("namespaces list read from IS is empty. Skipping caching update")
except Exception as e: except Exception as e:
print("Error occurred on reading namespaces from IS and refilling the cache!") log.error("Error occurred on reading namespaces from IS and refilling the cache!")
print(e) log.error(e)
# Created by Francesco Mangiacrapa
# francesco.mangiacrapa@isti.cnr.it
# ISTI-CNR Pisa (ITALY)
# D4S_IS_DiscoveryCatalogueNamespacesController is used to discovery namespaces for Catalogue Categories (implemented as a Singleton) # 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__) 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 # 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: 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 # @param: extras is the dictionary of extra fields for a certain item
@ -25,7 +20,7 @@ class D4S_Namespaces_Extra_Util():
dict_extras = None dict_extras = None
nms = namespaceid + ":" nms = namespaceid + ":"
#has_namespace_ref = None #has_namespace_ref = None
for key, value in extras: for key, value in extras.items():
k = key k = key
v = value v = value
# print "key: " + k # print "key: " + k
@ -53,7 +48,7 @@ class D4S_Namespaces_Extra_Util():
#break #break
#ADDING ALL EXTRAS WITHOUT NAMESPACE #ADDING ALL EXTRAS WITHOUT NAMESPACE
for key, value in extras: for key, value in extras.items():
k = key k = key
v = value v = value

View File

@ -13,11 +13,6 @@ NAMESPACE_ID_LABEL = '@id'
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
# Created by Francesco Mangiacrapa
# francesco.mangiacrapa@isti.cnr.it
# ISTI-CNR Pisa (ITALY)
def getResponseBody(uri): def getResponseBody(uri):
req = urllib.request.Request(uri) req = urllib.request.Request(uri)
try: try:
@ -43,6 +38,12 @@ def getResponseBody(uri):
class D4S_IS_DiscoveryCatalogueNamespaces(): class D4S_IS_DiscoveryCatalogueNamespaces():
def __init__(self, urlICProxy, resourceID, gcubeToken): 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.urlICProxy = urlICProxy
self.resourceID = resourceID self.resourceID = resourceID
self.gcubeToken = gcubeToken self.gcubeToken = gcubeToken
@ -53,10 +54,6 @@ class D4S_IS_DiscoveryCatalogueNamespaces():
namespace_list = [] namespace_list = []
try: try:
# print("proxy: "+self.urlICProxy)
# print("resourceID: " + self.resourceID)
# print("gcubeTokenParam: " + gcubeTokenParam)
# print("gcubeToken: " + self.gcubeToken)
uri = self.urlICProxy + "/" + self.resourceID + "?" + gcubeTokenParam + "=" + self.gcubeToken uri = self.urlICProxy + "/" + self.resourceID + "?" + gcubeTokenParam + "=" + self.gcubeToken
log.info("Contacting URL: %s" % uri) 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): def _package_extras_save(extra_dicts, obj, context):
''' It can save repeated extras as key-value ''' ''' It can save repeated extras as key-value '''
allow_partial_update = context.get("allow_partial_update", False) #allow_partial_update = context.get("allow_partial_update", False) potrebbe non servire
if extra_dicts is None and allow_partial_update: if extra_dicts is None: #and allow_partial_update:
return return
model = context["model"] model = context["model"]
session = context["session"] 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) #print "extra_dicts: "+str(extra_dicts)
extras_list = obj.extras_list extras_list = obj.extras_list
@ -59,118 +59,121 @@ def _package_extras_save(extra_dicts, obj, context):
if extra_dict['value'] is not None: if extra_dict['value'] is not None:
new_extras.setdefault(extra_dict["key"], []).append(extra_dict["value"]) 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) #print "new_extras: "+str(new_extras)
#new #aggiunta di nuove chiavi
for key in set(new_extras.keys()) - set(old_extras.keys()): for key in set(new_extras.keys()) - set(old_extras.keys()):
state = 'active' #state = 'active'
log.debug("adding key: "+unicode(key).encode('utf-8')) log.debug("adding key: " + str(key))
#print "adding key: "+str(key) #print "adding key: "+str(key)
extra_lst = new_extras[key] extra_lst = new_extras[key]
for extra in extra_lst: 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) session.add(extra)
extras_list.append(extra) extras_list.append(extra)
#deleted #gestione chiavi eliminate
for key in set(old_extras.keys()) - set(new_extras.keys()): 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) #print "deleting key: "+str(key)
extra_lst = extras[key] extra_lst = extras[key]
for extra in extra_lst: for extra in extra_lst:
state = 'deleted' #state = 'deleted'
extra.state = state extra.state = 'deleted'
extras_list.remove(extra) extras_list.remove(extra)
#changed #gestione chiavi aggiornate
for key in set(new_extras.keys()) & set(old_extras.keys()): for key in set(new_extras.keys()) & set(old_extras.keys()):
#for each value of new list #for each value of new list
for value in new_extras[key]: for value in new_extras[key]:
old_occur = old_extras[key].count(value) old_occur = old_extras[key].count(value)
new_occur = new_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) #print "value: "+str(value) + ", new_occur: "+str(new_occur) + ", old_occur: "+str(old_occur)
# it is an old value deleted or not # it is an old value deleted or not
if value in old_extras[key]: if value in old_extras[key]:
if old_occur == new_occur: if old_occur == new_occur:
#print "extra - occurrences of: "+str(value) +", are equal into both list" #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 #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) extra_values = get_package_for_value(extras[key], value)
#extras_list.append(extra) #extras_list.append(extra)
for extra in extra_values: for extra in extra_values:
state = 'active' #state = 'active'
extra.state = state extra.state = 'active'
session.add(extra) session.add(extra)
#print "extra updated: "+str(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: elif new_occur > old_occur:
#print "extra - a new occurrence of: "+str(value) +", is present into new list, adding it to old list" #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") log.debug("extra - a new occurrence of: "+ str(value) + ", is present into new list, adding it to old list")
state = 'active' #state = 'active'
extra = model.PackageExtra(state=state, key=key, value=value) extra = model.PackageExtra(state='active', key=key, value=value)
extra.state = state #extra.state = state
#extra.state = 'active' non dovrebbe servire
session.add(extra) session.add(extra)
extras_list.append(extra) extras_list.append(extra)
old_extras[key].append(value) 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]) #print "old extra values updated: "+str(old_extras[key])
else: else:
#remove all occurrences deleted - this code could be optimized, it is run several times but could be performed one shot #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 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" #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) extra_values = get_package_for_value(extras[key], value)
for idx, extra in enumerate(extra_values): for idx, extra in enumerate(extra_values):
if idx < countDelete: if idx < countDelete:
#print "extra - occurrence of: "+str(value) +", is not present into new list, removing it from old list" #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) #print "pkg extra deleting: "+str(extra.value)
state = 'deleted' #state = 'deleted'
extra.state = state extra.state = 'deleted'
else: else:
#print "pkg extra reactivating: "+str(extra.value) #print "pkg extra reactivating: "+str(extra.value)
log.debug("pkg extra reactivating: "+unicode(extra.value).encode('utf-8')) log.debug("pkg extra reactivating: "+ str(extra.value))
state = 'active' #state = 'active'
extra.state = state extra.state = 'active'
session.add(extra) session.add(extra)
else: else:
#print "extra new value: "+str(value) #print "extra new value: "+str(value)
log.debug("extra new value: "+unicode(value).encode('utf-8')) log.debug("extra new value: " + str(value))
state = 'active' #state = 'active'
extra = model.PackageExtra(state=state, key=key, value=value) extra = model.PackageExtra(state='active', key=key, value=value)
extra.state = state #extra.state = state
#extra.state = 'active'
session.add(extra) session.add(extra)
extras_list.append(extra) extras_list.append(extra)
#for each value of old list #chiavi vecchie non presenti
for value in old_extras[key]: for value in old_extras[key]:
#if value is not present in new list #if value is not present in new list
if value not in new_extras[key]: if value not in new_extras[key]:
extra_values = get_package_for_value(extras[key], value) extra_values = get_package_for_value(extras[key], value)
for extra in extra_values: for extra in extra_values:
#print "not present extra deleting: "+str(extra) #print "not present extra deleting: "+str(extra)
log.debug("not present extra deleting: "+unicode(extra).encode('utf-8')) log.debug("not present extra deleting: "+ str(extra))
state = 'deleted' #state = 'deleted'
extra.state = state extra.state = 'deleted'
def get_package_for_value(list_package, value): def get_package_for_value(list_package, value):
''' Returns a list of packages containing the value passed in input ''' 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
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 #OVERRIDING BASE SQL ALCHEMY ENGINE INSTANCE
@ -189,7 +192,7 @@ def _init_TrackingMiddleware(self, app, config):
sqlalchemy_overflow = config.get('sqlalchemy.max_overflow') sqlalchemy_overflow = config.get('sqlalchemy.max_overflow')
if sqlalchemy_overflow is None: if sqlalchemy_overflow is None:
sqlalchemy_overflow = 10; sqlalchemy_overflow = 10
log.debug('sqlalchemy_overflow read: '+str(sqlalchemy_overflow)) log.debug('sqlalchemy_overflow read: '+str(sqlalchemy_overflow))
@ -244,7 +247,7 @@ class D4SciencePlugin(plugins.SingletonPlugin):
def package_types(self): def package_types(self):
# Aggiunta del tipo di dato personalizzato deliverable # Aggiunta del tipo di dato personalizzato deliverable
return ['deliverable_type'] return []
def is_fallback(self): def is_fallback(self):
# Indica che questo plugin può essere usato come fallback se un tipo specifico non è specificato # Indica che questo plugin può essere usato come fallback se un tipo specifico non è specificato