def organization_filters(): top_organizations = {} ancestors_relations = {} tree = organization_tree() for top_organization in tree: top_organization['count'] = 0 top_organizations[top_organization['name']] = top_organization ancestors_relations[top_organization['name']] = top_organization['name'] if 'children' in top_organization and len(top_organization['children']) > 0: children = all_descendants(top_organization['children']) for child_name in children: ancestors_relations[child_name] = top_organization['name'] for organization in ckan_helpers.get_facet_items_dict('organization'): top_parent_name = ancestors_relations[organization['name']] if top_parent_name in top_organizations: top_organizations[top_parent_name]['count'] += organization['count'] if ckan_helpers.get_request_param('organization') in top_organizations: top_organizations[ckan_helpers.get_request_param('organization')]['active'] = True top_organizations_with_results = [organization for organization in top_organizations.values() if organization['count'] > 0] sorted_organizations = sorted(top_organizations_with_results, key=lambda item: item['count'], reverse=True) limit = int(request.params.get('_organization_limit', g.facets_default_number)) c.search_facets_limits['organization'] = limit if limit is not None and limit > 0: return sorted_organizations[:limit] return sorted_organizations
def organization_filters(): top_organizations = {} ancestors_relations = {} tree = organization_tree() for top_organization in tree: top_organization['count'] = 0 top_organizations[top_organization['name']] = top_organization ancestors_relations[top_organization['name']] = top_organization['name'] if 'children' in top_organization and len(top_organization['children']) > 0: children = all_descendants(top_organization['children']) for child_name in children: ancestors_relations[child_name] = top_organization['name'] for organization in ckan_helpers.get_facet_items_dict('organization'): top_parent_name = ancestors_relations[organization['name']] if top_parent_name in top_organizations: top_organizations[top_parent_name]['count'] += organization['count'] if ckan_helpers.get_request_param('organization') in top_organizations: top_organizations[ckan_helpers.get_request_param('organization')]['active'] = True top_organizations_with_results = [organization for organization in top_organizations.values() if organization['count'] > 0] sorted_organizations = sorted(top_organizations_with_results, key=lambda item: item['count'], reverse=True) limit = int(request.params.get('_organization_limit', g.facets_default_number)) c.search_facets_limits['organization'] = limit if limit is not None and limit > 0: return sorted_organizations[:limit] return sorted_organizations
def organizations_basic_info(): def convert_organization_to_dict(organization, depth): current_organization = {} organization_id = organization.pop('id') current_organization['id'] = organization_id current_organization['name'] = organization.pop('name') current_organization['title'] = organization.pop('title') current_organization['depth'] = depth # si depth == 0, la organización no es hija de otra current_organization['own_package_count'] = organizations_that_have_packages.pop(organization_id, 0) own_available_package_count = ckan_organizations_info.pop(current_organization['name'], {}).get('count', 0) children_data_dict = generate_children_data(organization.pop('children'), depth) current_organization['children'] = children_data_dict['dict_children'] current_organization['total_package_count'] = children_data_dict['current_total_package_count'] \ + current_organization['own_package_count'] current_organization['available_package_count'] = children_data_dict['current_available_package_count'] + \ own_available_package_count current_organization['active'] = current_organization['name'] == organization_in_request current_organization['display'] = not organization_in_request or current_organization['active'] return current_organization def generate_children_data(group_tree_children, depth): dict_children = [] current_available_package_count = 0 current_total_package_count = 0 for child in group_tree_children: converted_child = convert_organization_to_dict(child, depth + 1) dict_children.append(converted_child) current_available_package_count += converted_child.get('available_package_count', 0) current_total_package_count += converted_child.get('total_package_count', 0) return {'dict_children': dict_children, 'current_available_package_count': current_available_package_count, 'current_total_package_count': current_total_package_count} # Traemos las organizaciones organizations = get_organizations_tree() ckan_organizations_info = {item['name']: item for item in ckan_helpers.get_facet_items_dict('organization')} # Realizamos una query para conseguir las organizaciones que tienen datasets, y la cantidad de éstos query = search.PackageSearchQuery() q = {'q': '+capacity:public', 'fl': 'groups', 'facet.field': ['groups', 'owner_org'], 'facet.limit': -1, 'rows': 1} query.run(q) organizations_that_have_packages = query.facets.get('owner_org') # Transformamos cada organización en un dict para facilitar su uso, y agregamos información requerida organizations_data = [] organization_in_request = ckan_helpers.get_request_param('organization') for organization in organizations: current_organization = convert_organization_to_dict(organization, 0) organizations_data.append(current_organization) return organizations_data
def changes_multiple(package_type=None): ''' Called when a user specifies a range of versions they want to look at changes between. Verifies that the range is valid and finds the set of activity diffs for the changes in the given version range, then re-renders changes.html with the list. ''' new_id = h.get_request_param(u'new_id') old_id = h.get_request_param(u'old_id') context = { u'model': model, u'session': model.Session, u'user': g.user, u'auth_user_obj': g.userobj } # check to ensure that the old activity is actually older than # the new activity old_activity = get_action(u'activity_show')(context, { u'id': old_id, u'include_data': False}) new_activity = get_action(u'activity_show')(context, { u'id': new_id, u'include_data': False}) old_timestamp = old_activity[u'timestamp'] new_timestamp = new_activity[u'timestamp'] t1 = datetime.strptime(old_timestamp, u'%Y-%m-%dT%H:%M:%S.%f') t2 = datetime.strptime(new_timestamp, u'%Y-%m-%dT%H:%M:%S.%f') time_diff = t2 - t1 # if the time difference is negative, just return the change that put us # at the more recent ID we were just looking at # TODO: do something better here - go back to the previous page, # display a warning that the user can't look at a sequence where # the newest item is older than the oldest one, etc if time_diff.total_seconds() < 0: return changes(h.get_request_param(u'current_new_id')) done = False current_id = new_id diff_list = [] while not done: try: activity_diff = get_action(u'activity_diff')( context, { u'id': current_id, u'object_type': u'package', u'diff_type': u'html'}) except NotFound as e: log.info( u'Activity not found: {} - {}'.format(str(e), current_id) ) return base.abort(404, _(u'Activity not found')) except NotAuthorized: return base.abort(403, _(u'Unauthorized to view activity data')) diff_list.append(activity_diff) if activity_diff['activities'][0]['id'] == old_id: done = True else: current_id = activity_diff['activities'][0]['id'] pkg_id = diff_list[0][u'activities'][1][u'data'][u'package'][u'id'] current_pkg_dict = get_action(u'package_show')(context, {u'id': pkg_id}) pkg_activity_list = get_action(u'package_activity_list')(context, { u'id': pkg_id, u'limit': 100}) return base.render( u'package/changes.html', { u'activity_diffs': diff_list, u'pkg_dict': current_pkg_dict, u'pkg_activity_list': pkg_activity_list, u'dataset_type': current_pkg_dict[u'type'], } )