2012-12-12 12:54:50 +01:00
|
|
|
|
|
|
|
from ckan import logic
|
|
|
|
from ckan import model
|
|
|
|
import ckan.lib.helpers as h
|
2013-01-09 12:07:44 +01:00
|
|
|
import ckan.plugins as p
|
2012-12-12 12:54:50 +01:00
|
|
|
|
2013-01-09 12:07:44 +01:00
|
|
|
from ckanext.harvest.model import UPDATE_FREQUENCIES
|
2012-12-19 18:27:05 +01:00
|
|
|
from ckanext.harvest.plugin import DATASET_TYPE_NAME
|
2013-02-25 19:07:34 +01:00
|
|
|
from ckanext.harvest.interfaces import IHarvester
|
2013-01-09 12:07:44 +01:00
|
|
|
|
2018-10-26 13:38:09 +02:00
|
|
|
|
|
|
|
c = p.toolkit.c
|
2018-10-25 15:39:57 +02:00
|
|
|
request = p.toolkit.request
|
|
|
|
|
|
|
|
|
2018-10-26 13:38:09 +02:00
|
|
|
def get_harvest_source(source_id=None):
|
|
|
|
|
|
|
|
context = {'model': model, 'session': model.Session}
|
|
|
|
if source_id:
|
|
|
|
return p.toolkit.get_action('harvest_source_show')(context, {'id': source_id})
|
|
|
|
elif hasattr(c, 'pkg_dict'):
|
|
|
|
return c.pkg_dict
|
|
|
|
elif hasattr(c, 'pkg'):
|
|
|
|
return p.toolkit.get_action('harvest_source_show')(context, {'id': c.pkg.id})
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2012-12-12 12:54:50 +01:00
|
|
|
def package_list_for_source(source_id):
|
|
|
|
'''
|
|
|
|
Creates a dataset list with the ones belonging to a particular harvest
|
|
|
|
source.
|
|
|
|
|
|
|
|
It calls the package_list snippet and the pager.
|
|
|
|
'''
|
|
|
|
limit = 20
|
|
|
|
page = int(request.params.get('page', 1))
|
2018-08-30 16:33:55 +02:00
|
|
|
fq = '+harvest_source_id:"{0}"'.format(source_id)
|
2012-12-12 12:54:50 +01:00
|
|
|
search_dict = {
|
|
|
|
'fq' : fq,
|
2013-06-26 12:14:38 +02:00
|
|
|
'rows': limit,
|
2012-12-12 12:54:50 +01:00
|
|
|
'sort': 'metadata_modified desc',
|
|
|
|
'start': (page - 1) * limit,
|
|
|
|
}
|
|
|
|
|
|
|
|
context = {'model': model, 'session': model.Session}
|
2018-10-26 13:38:09 +02:00
|
|
|
harvest_source = get_harvest_source(source_id)
|
|
|
|
owner_org = harvest_source.get('owner_org', '')
|
2013-03-21 12:54:02 +01:00
|
|
|
if owner_org:
|
|
|
|
user_member_of_orgs = [org['id'] for org
|
2013-03-18 17:29:29 +01:00
|
|
|
in h.organizations_available('read')]
|
2018-10-26 13:38:09 +02:00
|
|
|
if (harvest_source and owner_org in user_member_of_orgs):
|
2013-03-21 12:54:02 +01:00
|
|
|
context['ignore_capacity_check'] = True
|
2013-03-18 17:29:29 +01:00
|
|
|
|
2012-12-12 12:54:50 +01:00
|
|
|
query = logic.get_action('package_search')(context, search_dict)
|
|
|
|
|
2012-12-19 18:27:05 +01:00
|
|
|
base_url = h.url_for('{0}_read'.format(DATASET_TYPE_NAME), id=source_id)
|
|
|
|
def pager_url(q=None, page=None):
|
|
|
|
url = base_url
|
|
|
|
if page:
|
|
|
|
url += '?page={0}'.format(page)
|
|
|
|
return url
|
|
|
|
|
2012-12-12 12:54:50 +01:00
|
|
|
pager = h.Page(
|
|
|
|
collection=query['results'],
|
|
|
|
page=page,
|
2012-12-19 18:27:05 +01:00
|
|
|
url=pager_url,
|
2012-12-12 12:54:50 +01:00
|
|
|
item_count=query['count'],
|
|
|
|
items_per_page=limit
|
|
|
|
)
|
|
|
|
pager.items = query['results']
|
|
|
|
|
2013-02-08 13:15:14 +01:00
|
|
|
if query['results']:
|
|
|
|
out = h.snippet('snippets/package_list.html', packages=query['results'])
|
|
|
|
out += pager.pager()
|
|
|
|
else:
|
|
|
|
out = h.snippet('snippets/package_list_empty.html')
|
2012-12-12 12:54:50 +01:00
|
|
|
|
|
|
|
return out
|
2013-01-09 12:07:44 +01:00
|
|
|
|
2017-10-31 15:56:38 +01:00
|
|
|
def package_count_for_source(source_id):
|
|
|
|
'''
|
|
|
|
Returns the current package count for datasets associated with the given
|
|
|
|
source id
|
|
|
|
'''
|
2018-08-30 16:33:55 +02:00
|
|
|
fq = '+harvest_source_id:"{0}"'.format(source_id)
|
2017-10-31 15:56:38 +01:00
|
|
|
search_dict = {'fq': fq}
|
|
|
|
context = {'model': model, 'session': model.Session}
|
|
|
|
result = logic.get_action('package_search')(context, search_dict)
|
|
|
|
return result.get('count', 0)
|
|
|
|
|
2013-01-09 12:07:44 +01:00
|
|
|
def harvesters_info():
|
|
|
|
context = {'model': model, 'user': p.toolkit.c.user or p.toolkit.c.author}
|
|
|
|
return logic.get_action('harvesters_info_show')(context,{})
|
|
|
|
|
|
|
|
def harvester_types():
|
|
|
|
harvesters = harvesters_info()
|
|
|
|
return [{'text': p.toolkit._(h['title']), 'value': h['name']}
|
|
|
|
for h in harvesters]
|
|
|
|
|
|
|
|
def harvest_frequencies():
|
|
|
|
|
|
|
|
return [{'text': p.toolkit._(f.title()), 'value': f}
|
|
|
|
for f in UPDATE_FREQUENCIES]
|
2013-01-24 19:21:05 +01:00
|
|
|
|
|
|
|
def link_for_harvest_object(id=None, guid=None, text=None):
|
|
|
|
|
|
|
|
if not id and not guid:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if guid:
|
|
|
|
context = {'model': model, 'user': p.toolkit.c.user or p.toolkit.c.author}
|
|
|
|
obj =logic.get_action('harvest_object_show')(context, {'id': guid, 'attr': 'guid'})
|
|
|
|
id = obj.id
|
|
|
|
|
|
|
|
url = h.url_for('harvest_object_show', id=id)
|
|
|
|
text = text or guid or id
|
|
|
|
link = '<a href="{url}">{text}</a>'.format(url=url, text=text)
|
|
|
|
|
|
|
|
return p.toolkit.literal(link)
|
2013-02-25 19:07:34 +01:00
|
|
|
|
|
|
|
def harvest_source_extra_fields():
|
|
|
|
fields = {}
|
|
|
|
for harvester in p.PluginImplementations(IHarvester):
|
|
|
|
if not hasattr(harvester, 'extra_schema'):
|
|
|
|
continue
|
|
|
|
fields[harvester.info()['name']] = harvester.extra_schema().keys()
|
|
|
|
return fields
|
|
|
|
|
2018-10-25 12:19:43 +02:00
|
|
|
|
|
|
|
def bootstrap_version():
|
|
|
|
if p.toolkit.check_ckan_version(max_version='2.7.99'):
|
|
|
|
return 'bs2'
|
|
|
|
else:
|
|
|
|
return (
|
|
|
|
'bs2' if
|
|
|
|
p.toolkit.config.get('ckan.base_public_folder') == 'public-bs2'
|
|
|
|
else 'bs3')
|