def test_group_archived_requests_valid(self): id2 = 2 id3 = 3 id55 = 55 requests = [{ 'package_id': id55, 'title': 'test5', 'maintainers': 'm', 'requests_archived': 'r', 'shared': '', 'requests': 'r' }, { 'package_id': id3, 'title': 'test3', 'maintainers': '', 'requests_archived': '', 'shared': '', 'requests': '' }, { 'package_id': id2, 'title': 'test2', 'maintainers': 't', 'requests_archived': 'r', 'shared': 't', 'requests': 'r' }] res = h.group_archived_requests_by_dataset(requests) assert_equals(res[0]['package_id'], id2) assert_equals(res[1]['package_id'], id3) assert_equals(res[2]['package_id'], id55)
def my_requested_data(self, id): '''Handles creating template for 'My Requested Data' page in the user's dashboard. :param id: The user's id. :type id: string :returns: template ''' try: requests = _get_action('requestdata_request_list_for_current_user', {}) except NotAuthorized: abort(403, _('Not authorized to see this page.')) c.is_myself = id == c.user if not c.is_myself: abort(403, _('Not authorized to see this page.')) order_by = request.query_string requests_new = [] requests_open = [] requests_archive = [] reverse = True order = '' if order_by is not '': if 'shared' in order_by: order = 'shared' elif 'requests' in order_by: order = 'requests' elif 'asc' in order_by: reverse = False order = 'title' elif 'desc' in order_by: reverse = True order = 'title' for item in requests: package = _get_action('package_show', {'id': item['package_id']}) count = _get_action('requestdata_request_data_counters_get', {'package_id': item['package_id']}) item['title'] = package['title'] item['shared'] = count.shared item['requests'] = count.requests requests = sorted(requests, key=lambda x: x[order], reverse=reverse) #TODO simplify these for item in requests: package = _get_action('package_show', {'id': item['package_id']}) package_maintainers_ids = package['maintainer'].split(',') item['title'] = package['title'] maintainers = [] for i in package_maintainers_ids: user = _get_action('user_show', {'id': i}) payload = {'id': i, 'fullname': user['fullname']} maintainers.append(payload) item['maintainers'] = maintainers if item['state'] == 'new': requests_new.append(item) elif item['state'] == 'open': requests_open.append(item) elif item['state'] == 'archive': requests_archive.append(item) grouped_requests_archive = helpers.group_archived_requests_by_dataset( requests_archive) extra_vars = { 'requests_new': requests_new, 'requests_open': requests_open, 'requests_archive': grouped_requests_archive } context = _get_context() user_obj = context['auth_user_obj'] user_id = user_obj.id data_dict = {'user_id': user_id} _get_action('requestdata_notification_change', data_dict) data_dict = {'id': id, 'include_num_followers': True} self._setup_template_variables(_get_context(), data_dict) return toolkit.render('requestdata/my_requested_data.html', extra_vars)
if maint in maintainer_ids: maintainer_found = True else: if maint in package_maintainer_ids: maintainer_found = True if not maintainer_found: if r['state'] == 'new': org['requests_new'].remove(r) elif r['state'] == 'open': org['requests_open'].remove(r) elif r['state'] == 'archive': org['requests_archive'].remove(r) org['requests_archive'] = \ helpers.group_archived_requests_by_dataset( org['requests_archive']) q_org = [x for x in q_organizations if x.get('org') == org['name']] if q_org: q_org = q_org[0] order = q_org.get('order') reverse = q_org.get('reverse') current_order_name = q_org.get('current_order_name') else: order = 'last_request_created_at' reverse = True current_order_name = 'Most Recent' org['current_order_name'] = current_order_name
def requests_data(self): ''' Handles creating template for 'Requested Data' page in the admin dashboard. :returns: template ''' try: requests = _get_action('requestdata_request_list_for_sysadmin', {}) except NotAuthorized: abort(403, _('Not authorized to see this page.')) organizations = [] tmp_orgs = [] filtered_maintainers = [] filtered_organizations = [] organizations_for_filters = {} reverse = True q_organization = '' request_params = request.params.dict_of_lists() for item in request_params: if item == 'filter_by_maintainers': for x in request_params[item]: params = x.split('|') org = params[0].split(':')[1] maintainers = params[1].split(':')[1].split(',') maintainers_ids = [] if maintainers[0] != '*all*': for i in maintainers: user = _get_action('user_show', {'id': i}) maintainers_ids.append(user['id']) data = {'org': org, 'maintainers': maintainers_ids} filtered_maintainers.append(data) elif item == 'filter_by_organizations': filtered_organizations = request_params[item][0].split(',') elif item == 'order_by': for x in request_params[item]: params = x.split('|') q_organization = params[1].split(':')[1] order = params[0] if 'asc' in order: reverse = False order = 'title' elif 'desc' in order: reverse = True order = 'title' for x in requests: package = _get_action('package_show', {'id': x['package_id']}) count = _get_action( 'requestdata_request_data_counters_get', {'package_id': x['package_id']}) x['title'] = package['title'] x['shared'] = count.shared x['requests'] = count.requests data_dict = {'id': package['owner_org']} current_org = _get_action('organization_show', data_dict) x['name'] = current_org['name'] # Group requests by organization for item in requests: package = _get_action('package_show', {'id': item['package_id']}) package_maintainer_ids = package['maintainer'].split(',') data_dict = {'id': package['owner_org']} org = _get_action('organization_show', data_dict) item['title'] = package['title'] if org['id'] in organizations_for_filters: organizations_for_filters[org['id']]['requests'] += 1 else: organizations_for_filters[org['id']] = { 'name': org['name'], 'title': org['title'], 'requests': 1 } if len(filtered_organizations ) > 0 and org['name'] not in filtered_organizations: continue maintainers = [] for id in package_maintainer_ids: user = _get_action('user_show', {'id': id}) username = user['name'] name = user['fullname'] payload = {id: id, 'fullname': user['fullname']} maintainers.append(payload) if not name: name = username item['maintainers'] = maintainers counters = _get_action( 'requestdata_request_data_counters_get_by_org', {'org_id': org['id']}) if org['id'] not in tmp_orgs: data = { 'title': org['title'], 'name': org['name'], 'id': org['id'], 'requests_new': [], 'requests_open': [], 'requests_archive': [], 'maintainers': [], 'counters': counters } if item['state'] == 'new': data['requests_new'].append(item) elif item['state'] == 'open': data['requests_open'].append(item) elif item['state'] == 'archive': data['requests_archive'].append(item) payload = {'id': id, 'name': name, 'username': username} data['maintainers'].append(payload) organizations.append(data) else: current_org = next(item for item in organizations if item['id'] == org['id']) payload = {'id': id, 'name': name, 'username': username} current_org['maintainers'].append(payload) if item['state'] == 'new': current_org['requests_new'].append(item) elif item['state'] == 'open': current_org['requests_open'].append(item) elif item['state'] == 'archive': current_org['requests_archive'].append(item) tmp_orgs.append(org['id']) for org in organizations: copy_of_maintainers = org['maintainers'] org['maintainers'] = dict( (item['id'], item) for item in org['maintainers']).values() # Count how many requests each maintainer has for main in org['maintainers']: c = Counter(item for dct in copy_of_maintainers for item in dct.items()) main['count'] = c[('id', main['id'])] # Sort maintainers by number of requests org['maintainers'] = sorted(org['maintainers'], key=lambda k: k['count'], reverse=True) total_organizations = org['requests_new'] + org[ 'requests_open'] + org['requests_archive'] for i, r in enumerate(total_organizations): maintainer_found = False package = _get_action('package_show', {'id': r['package_id']}) package_maintainer_ids = package['maintainer'].split(',') data_dict = {'id': package['owner_org']} organ = _get_action('organization_show', data_dict) # Check if current request is part of a filtered maintainer for x in filtered_maintainers: if x['org'] == organ['name']: for maint in x['maintainers']: if maint in package_maintainer_ids: maintainer_found = True if not maintainer_found: if r['state'] == 'new': org['requests_new'].remove(r) elif r['state'] == 'open': org['requests_open'].remove(r) elif r['state'] == 'archive': org['requests_archive'].remove(r) org['requests_archive'] = helpers.group_archived_requests_by_dataset( org['requests_archive']) if org['name'] == q_organization: org['requests_archive'] = sorted(org['requests_archive'], key=lambda x: x[order], reverse=reverse) organizations_for_filters = sorted( organizations_for_filters.iteritems(), key=lambda (x, y): y['requests'], reverse=True) total_requests_counters = _get_action( 'requestdata_request_data_counters_get_all', {}) extra_vars = { 'organizations': organizations, 'organizations_for_filters': organizations_for_filters, 'total_requests_counters': total_requests_counters } return toolkit.render('admin/all_requests_data.html', extra_vars)
def requested_data(self, id): '''Handles creating template for 'Requested Data' page in the organization's dashboard. :param id: The organization's id. :type id: string :returns: template ''' try: requests = _get_action('requestdata_request_list_for_organization', {'org_id': id}) except NotAuthorized: abort(403, _('Not authorized to see this page.')) group_type = self._ensure_controller_matches_group_type(id) context = {'model': model, 'session': model.Session, 'user': c.user} c.group_dict = self._get_group_dict(id) group_type = c.group_dict['type'] request_params = request.params.dict_of_lists() filtered_maintainers = [] reverse = True order = '' q_organization = '' for item in request_params: if item == 'filter_by_maintainers': for x in request_params[item]: params = x.split('|') org = params[0].split(':')[1] maintainers = params[1].split(':')[1].split(',') maintainers_ids = [] if maintainers[0] != '*all*': for i in maintainers: user = _get_action('user_show', {'id': i}) maintainers_ids.append(user['id']) data = {'org': org, 'maintainers': maintainers_ids} filtered_maintainers.append(data) elif item == 'order_by': params = request_params[item][0].split('|') q_organization = params[1].split(':')[1] order = params[0] if 'asc' in order: reverse = False order = 'title' elif 'desc' in order: reverse = True order = 'title' for x in requests: package = _get_action('package_show', {'id': x['package_id']}) count = _get_action( 'requestdata_request_data_counters_get', {'package_id': x['package_id']}) x['title'] = package['title'] x['shared'] = count.shared x['requests'] = count.requests data_dict = {'id': package['owner_org']} current_org = _get_action('organization_show', data_dict) x['name'] = current_org['name'] maintainers = [] for item in requests: package = _get_action('package_show', {'id': item['package_id']}) package_maintainer_ids = package['maintainer'].split(',') item['title'] = package['title'] for maint_id in package_maintainer_ids: user = _get_action('user_show', {'id': maint_id}) username = user['name'] name = user['fullname'] if not name: name = username payload = {'id': maint_id, 'name': name, 'username': username} maintainers.append(payload) copy_of_maintainers = maintainers maintainers = dict((item['id'], item) for item in maintainers).values() organ = _get_action('organization_show', {'id': id}) # Count how many requests each maintainer has for main in maintainers: count = Counter(item for dct in copy_of_maintainers for item in dct.items()) main['count'] = count[('id', main['id'])] # Sort maintainers by number of requests maintainers = sorted(maintainers, key=lambda k: k['count'], reverse=True) for i, r in enumerate(requests[:]): maintainer_found = False package = _get_action('package_show', {'id': r['package_id']}) package_maintainer_ids = package['maintainer'].split(',') data_dict = {'id': package['owner_org']} organ = _get_action('organization_show', data_dict) # Check if current request is part of a filtered maintainer for x in filtered_maintainers: if x['org'] == organ['name']: for maint in x['maintainers']: if maint in package_maintainer_ids: maintainer_found = True if not maintainer_found: requests.remove(r) requests_new = [] requests_open = [] requests_archive = [] for item in requests: if item['state'] == 'new': requests_new.append(item) elif item['state'] == 'open': requests_open.append(item) elif item['state'] == 'archive': requests_archive.append(item) grouped_requests_archive = helpers.group_archived_requests_by_dataset( requests_archive) if organ['name'] == q_organization: grouped_requests_archive = sorted(grouped_requests_archive, key=lambda x: x[order], reverse=reverse) extra_vars = { 'requests_new': requests_new, 'requests_open': requests_open, 'requests_archive': grouped_requests_archive, 'maintainers': maintainers, 'org_name': organ['name'] } self._setup_template_variables(context, {'id': id}, group_type=group_type) return render('requestdata/organization_requested_data.html', extra_vars)
def requests_data(self): ''' Handles creating template for 'Requested Data' page in the admin dashboard. :returns: template ''' try: requests = _get_action('requestdata_request_list_for_sysadmin', {}) except NotAuthorized: abort(403, _('Not authorized to see this page.')) organizations = [] tmp_orgs = [] filtered_maintainers = [] filtered_organizations = [] organizations_for_filters = {} reverse = True q_organizations = [] request_params = request.params.dict_of_lists() order = 'last_request_created_at' for item in request_params: if item == 'filter_by_maintainers': for x in request_params[item]: params = x.split('|') org = params[0].split(':')[1] maintainers = params[1].split(':')[1].split(',') maintainers_ids = [] if maintainers[0] != '*all*': for i in maintainers: try: user = _get_action('user_show', {'id': i}) maintainers_ids.append(user['id']) except NotFound: pass data = {'org': org, 'maintainers': maintainers_ids} filtered_maintainers.append(data) elif item == 'filter_by_organizations': filtered_organizations = request_params[item][0].split(',') elif item == 'order_by': for x in request_params[item]: params = x.split('|') q_organization = params[1].split(':')[1] order = params[0] if 'asc' in order: reverse = False order = 'title' current_order_name = 'Alphabetical (A-Z)' elif 'desc' in order: reverse = True order = 'title' current_order_name = 'Alphabetical (Z-A)' elif 'most_recent' in order: reverse = True order = 'last_request_created_at' current_order_name = 'Most Recent' elif 'shared' in order: current_order_name = 'Sharing Rate' elif 'requests' in order: current_order_name = 'Requests Rate' data = { 'org': q_organization, 'order': order, 'reverse': reverse, 'current_order_name': current_order_name } q_organizations.append(data) for x in requests: package =\ _get_action('package_show', {'id': x['package_id']}) count = \ _get_action('requestdata_request_data_counters_get', {'package_id': x['package_id']}) if count: x['shared'] = count.shared x['requests'] = count.requests x['title'] = package['title'] data_dict = {'id': package['owner_org']} current_org = _get_action('organization_show', data_dict) x['name'] = current_org['name'] # Group requests by organization for item in requests: package = \ _get_action('package_show', {'id': item['package_id']}) package_maintainer_ids = package['maintainer'].split(',') data_dict = {'id': package['owner_org']} org = _get_action('organization_show', data_dict) item['title'] = package['title'] if org['id'] in organizations_for_filters: organizations_for_filters[org['id']]['requests'] += 1 else: organizations_for_filters[org['id']] = { 'name': org['name'], 'title': org['title'], 'requests': 1 } if len(filtered_organizations) > 0\ and org['name'] not in filtered_organizations: continue maintainers = [] name = '' username = '' for id in package_maintainer_ids: try: user = _get_action('user_show', {'id': id}) username = user['name'] name = user['fullname'] payload = { 'id': id, 'name': name, 'username': username, 'fullname': name } maintainers.append(payload) if not name: name = username except NotFound: pass item['maintainers'] = maintainers counters = \ _get_action('requestdata_request_data_counters_get_by_org', {'org_id': org['id']}) if org['id'] not in tmp_orgs: data = { 'title': org['title'], 'name': org['name'], 'id': org['id'], 'requests_new': [], 'requests_open': [], 'requests_archive': [], 'maintainers': [], 'counters': counters } if item['state'] == 'new': data['requests_new'].append(item) elif item['state'] == 'open': data['requests_open'].append(item) elif item['state'] == 'archive': data['requests_archive'].append(item) payload = {'id': id, 'name': name, 'username': username} data['maintainers'].append(payload) organizations.append(data) else: current_org = \ next(item for item in organizations if item['id'] == org['id']) payload = {'id': id, 'name': name, 'username': username} current_org['maintainers'].append(payload) if item['state'] == 'new': current_org['requests_new'].append(item) elif item['state'] == 'open': current_org['requests_open'].append(item) elif item['state'] == 'archive': current_org['requests_archive'].append(item) tmp_orgs.append(org['id']) for org in organizations: copy_of_maintainers = org['maintainers'] org['maintainers'] = \ dict((item['id'], item) for item in org['maintainers']).values() # Count how many requests each maintainer has for main in org['maintainers']: c = Counter(item for dct in copy_of_maintainers for item in dct.items()) main['count'] = c[('id', main['id'])] # Sort maintainers by number of requests org['maintainers'] = \ sorted(org['maintainers'], key=lambda k: k['count'], reverse=True) total_organizations = \ org['requests_new'] + \ org['requests_open'] +\ org['requests_archive'] for i, r in enumerate(total_organizations): maintainer_found = False package = _get_action('package_show', {'id': r['package_id']}) package_maintainer_ids = package['maintainer'].split(',') is_hdx = requestdata_helper.is_hdx_portal() if is_hdx: # Quick fix for hdx portal maintainer_ids = [] for maintainer_name in package_maintainer_ids: try: main_ids =\ _get_action('user_show', {'id': maintainer_name}) maintainer_ids.append(main_ids['id']) except NotFound: pass data_dict = {'id': package['owner_org']} organ = _get_action('organization_show', data_dict) # Check if current request is part of a filtered maintainer for x in filtered_maintainers: if x['org'] == organ['name']: for maint in x['maintainers']: if is_hdx: if maint in maintainer_ids: maintainer_found = True else: if maint in package_maintainer_ids: maintainer_found = True if not maintainer_found: if r['state'] == 'new': org['requests_new'].remove(r) elif r['state'] == 'open': org['requests_open'].remove(r) elif r['state'] == 'archive': org['requests_archive'].remove(r) org['requests_archive'] = \ helpers.group_archived_requests_by_dataset( org['requests_archive']) q_org = [x for x in q_organizations if x.get('org') == org['name']] if q_org: q_org = q_org[0] order = q_org.get('order') reverse = q_org.get('reverse') current_order_name = q_org.get('current_order_name') else: order = 'last_request_created_at' reverse = True current_order_name = 'Most Recent' org['current_order_name'] = current_order_name if order == 'last_request_created_at': for dataset in org['requests_archive']: created_at = \ dataset.get('requests_archived')[0].get('created_at') data = {'last_request_created_at': created_at} dataset.update(data) org['requests_archive'] = \ sorted(org['requests_archive'], key=lambda x: x[order], reverse=reverse) organizations_for_filters = \ sorted(organizations_for_filters.iteritems(), key=lambda (x, y): y['requests'], reverse=True) total_requests_counters =\ _get_action('requestdata_request_data_counters_get_all', {}) extra_vars = { 'organizations': organizations, 'organizations_for_filters': organizations_for_filters, 'total_requests_counters': total_requests_counters } return toolkit.render('admin/all_requests_data.html', extra_vars)
class UserController(BaseController): def my_requested_data(self, id): '''Handles creating template for 'My Requested Data' page in the user's dashboard. :param id: The user's id. :type id: string :returns: template ''' try: requests = _get_action('requestdata_request_list_for_current_user', {}) except NotAuthorized: abort(403, _('Not authorized to see this page.')) c.is_myself = id == c.user if not c.is_myself: abort(403, _('Not authorized to see this page.')) order_by = request.query_string requests_new = [] requests_open = [] requests_archive = [] reverse = True order = 'last_request_created_at' current_order_name = 'Most Recent' if order_by is not '': if 'shared' in order_by: order = 'shared' current_order_name = 'Sharing Rate' elif 'requests' in order_by: order = 'requests' current_order_name = 'Requests Rate' elif 'asc' in order_by: reverse = False order = 'title' current_order_name = 'Alphabetical (A-Z)' elif 'desc' in order_by: reverse = True order = 'title' current_order_name = 'Alphabetical (Z-A)' elif 'most_recent' in order_by: reverse = True order = 'last_request_created_at' for item in requests: package =\ _get_action('package_show', {'id': item['package_id']}) count = _get_action('requestdata_request_data_counters_get', {'package_id': item['package_id']}) item['title'] = package['title'] item['shared'] = count.shared item['requests'] = count.requests for item in requests: try: package =\ _get_action('package_show', {'id': item['package_id']}) package_maintainers_ids = package['maintainer'].split(',') item['title'] = package['title'] except NotFound, e: # package was not found, possibly deleted continue maintainers = [] for i in package_maintainers_ids: try: user = _get_action('user_show', {'id': i}) payload = {'id': i, 'fullname': user['fullname']} maintainers.append(payload) except NotFound: pass item['maintainers'] = maintainers if item['state'] == 'new': requests_new.append(item) elif item['state'] == 'open': requests_open.append(item) elif item['state'] == 'archive': requests_archive.append(item) requests_archive = \ helpers.group_archived_requests_by_dataset(requests_archive) if order == 'last_request_created_at': for dataset in requests_archive: created_at = \ dataset.get('requests_archived')[0].get('created_at') data = {'last_request_created_at': created_at} dataset.update(data) if order: requests_archive = \ sorted(requests_archive, key=lambda x: x[order], reverse=reverse) extra_vars = { 'requests_new': requests_new, 'requests_open': requests_open, 'requests_archive': requests_archive, 'current_order_name': current_order_name } context = _get_context() user_obj = context['auth_user_obj'] user_id = user_obj.id data_dict = {'user_id': user_id} _get_action('requestdata_notification_change', data_dict) data_dict = {'id': id, 'include_num_followers': True} self._setup_template_variables(_get_context(), data_dict) return toolkit.render('requestdata/my_requested_data.html', extra_vars)