def organisation_resources(organisation_name, include_sub_organisations=False, date_formatter=None): ''' Returns a dictionary detailing resources for each dataset in the organisation specified. headings: ['Publisher title', 'Publisher name', 'Dataset title', 'Dataset name', 'Resource index', 'Description', 'URL', 'Format', 'Date created'] i.e.: {'publisher_name': 'cabinet-office', 'publisher_title:': 'Cabinet Office', 'schema': {'Publisher title': 'publisher_id', 'Publisher name': 'publisher_name', ...}, 'rows': [ row_dict, row_dict, ... ] } ''' sql = """ select package.id as package_id, package.title as package_title, package.name as package_name, resource.id as resource_id, resource.url as resource_url, resource.format as resource_format, resource.description as resource_description, resource.position as resource_position, resource.created as resource_created, "group".id as publisher_id, "group".name as publisher_name, "group".title as publisher_title from resource left join resource_group on resource.resource_group_id = resource_group.id left join package on resource_group.package_id = package.id left join member on member.table_id = package.id left join "group" on member.group_id = "group".id where package.state='active' and resource.state='active' and resource_group.state='active' and "group".state='active' and %(org_filter)s order by "group".name, package.name, resource.position """ org = model.Group.by_name(organisation_name) if not org: abort(404, 'Publisher not found') organisation_title = org.title sql_org_filter, sql_params = sql_to_filter_by_organisation( org, include_sub_organisations=include_sub_organisations) raw_rows = model.Session.execute(sql % sql_org_filter, sql_params) schema = OrderedDict(( ('Publisher title', 'publisher_title'), ('Publisher name', 'publisher_name'), ('Dataset title', 'package_title'), ('Dataset name', 'package_name'), ('Resource index', 'resource_position'), ('Resource ID', 'resource_id'), ('Description', 'resource_description'), ('URL', 'resource_url'), ('Format', 'resource_format'), ('Date created', 'resource_created'), )) rows = [] for raw_row in raw_rows: #row = [getattr(raw_row, key) for key in schema.values()] row = OrderedDict([(key, getattr(raw_row, key)) for key in schema.values()]) if date_formatter: for col in ('resource_created', ): if row[col]: row[col] = date_formatter(row[col]) rows.append(row) return { 'publisher_name': org.name, 'publisher_title': org.title, 'schema': schema, 'rows': rows, }
package_data['openness_score'] = row.task_status_value package_data['openness_score_reason'] = package_data[ 'reason'] # deprecated package_data['last_updated'] = row.task_status_last_updated data[row.package_name] = package_data # Sort the results by openness_score asc so we can see the worst # results first data = OrderedDict( sorted(data.iteritems(), key=lambda x: x[1]['openness_score'])) return { 'publisher_name': organisation_name, 'publisher_title': organisation_title, 'data': data.values() } def feedback_report(publisher, include_sub_publishers=False, include_published=False, use_cache=False): """ For the publisher provided (and optionally for sub-publishers) this function will generate a report on the feedback for that publisher. """ import collections import datetime import ckan.lib.helpers as helpers from ckanext.dgu.lib.publisher import go_down_tree
package_data['reason'] = 'Could not display reason due to a system error' package_data['openness_score'] = row.task_status_value package_data['openness_score_reason'] = package_data['reason'] # deprecated package_data['last_updated'] = row.task_status_last_updated data[row.package_name] = package_data # Sort the results by openness_score asc so we can see the worst # results first data = OrderedDict(sorted(data.iteritems(), key=lambda x: x[1]['openness_score'])) return {'publisher_name': organisation_name, 'publisher_title': organisation_title, 'data': data.values()} def feedback_report(publisher, include_sub_publishers=False, include_published=False, use_cache=False): """ For the publisher provided (and optionally for sub-publishers) this function will generate a report on the feedback for that publisher. """ import collections import datetime import ckan.lib.helpers as helpers from ckanext.dgu.lib.publisher import go_down_tree from ckanext.dgu.model.feedback import Feedback from operator import itemgetter from sqlalchemy.util import OrderedDict
def organisation_resources(organisation_name, include_sub_organisations=False, date_formatter=None): ''' Returns a dictionary detailing resources for each dataset in the organisation specified. headings: ['Publisher title', 'Publisher name', 'Dataset title', 'Dataset name', 'Resource index', 'Description', 'URL', 'Format', 'Date created'] i.e.: {'publisher_name': 'cabinet-office', 'publisher_title:': 'Cabinet Office', 'schema': {'Publisher title': 'publisher_id', 'Publisher name': 'publisher_name', ...}, 'rows': [ row_dict, row_dict, ... ] } ''' sql = """ select package.id as package_id, package.title as package_title, package.name as package_name, resource.id as resource_id, resource.url as resource_url, resource.format as resource_format, resource.description as resource_description, resource.position as resource_position, resource.created as resource_created, "group".id as publisher_id, "group".name as publisher_name, "group".title as publisher_title from resource left join resource_group on resource.resource_group_id = resource_group.id left join package on resource_group.package_id = package.id left join member on member.table_id = package.id left join "group" on member.group_id = "group".id where package.state='active' and resource.state='active' and resource_group.state='active' and "group".state='active' and %(org_filter)s order by "group".name, package.name, resource.position """ org = model.Group.by_name(organisation_name) if not org: abort(404, 'Publisher not found') organisation_title = org.title sql_org_filter, sql_params = sql_to_filter_by_organisation( org, include_sub_organisations=include_sub_organisations) raw_rows = model.Session.execute(sql % sql_org_filter, sql_params) schema = OrderedDict((('Publisher title', 'publisher_title'), ('Publisher name', 'publisher_name'), ('Dataset title', 'package_title'), ('Dataset name', 'package_name'), ('Resource index', 'resource_position'), ('Resource ID', 'resource_id'), ('Description', 'resource_description'), ('URL', 'resource_url'), ('Format', 'resource_format'), ('Date created', 'resource_created'), )) rows = [] for raw_row in raw_rows: #row = [getattr(raw_row, key) for key in schema.values()] row = OrderedDict([(key, getattr(raw_row, key)) for key in schema.values()]) if date_formatter: for col in ('resource_created',): if row[col]: row[col] = date_formatter(row[col]) rows.append(row) return {'publisher_name': org.name, 'publisher_title': org.title, 'schema': schema, 'rows': rows, }
package_data.update(json.loads(row.task_status_error)) except ValueError, e: log.error('QA status "error" should have been in JSON format, but found: "%s" %s', task_status_error, e) package_data["reason"] = "Could not display reason due to a system error" package_data["openness_score"] = row.task_status_value package_data["openness_score_reason"] = package_data["reason"] # deprecated package_data["last_updated"] = row.task_status_last_updated data[row.package_name] = package_data # Sort the results by openness_score asc so we can see the worst # results first data = OrderedDict(sorted(data.iteritems(), key=lambda x: x[1]["openness_score"])) return {"publisher_name": organisation_name, "publisher_title": organisation_title, "data": data.values()} def feedback_report(publisher, include_sub_publishers=False, include_published=False, use_cache=False): """ For the publisher provided (and optionally for sub-publishers) this function will generate a report on the feedback for that publisher. """ import collections import datetime import ckan.lib.helpers as helpers from ckanext.dgu.lib.publisher import go_down_tree from ckanext.dgu.model.feedback import Feedback from operator import itemgetter from sqlalchemy.util import OrderedDict