Exemple #1
0
            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)
    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 = 'last_request_created_at'
        q_organization = ''
        current_order_name = 'Most Recent'

        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 == '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'
                    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'
                elif 'shared' in order:
                    current_order_name = 'Sharing Rate'
                elif 'requests' in order:
                    current_order_name = 'Requests Rate'

                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']
            package_maintainers = []

            for maint_id in package_maintainer_ids:
                try:
                    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,
                        'fullname': name
                    }
                    maintainers.append(payload)
                    package_maintainers.append(payload)
                except NotFound:
                    pass
            item['maintainers'] = package_maintainers

        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(',')
            is_hdx = helpers.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:
                        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 order == 'last_request_created_at':
            for dataset in grouped_requests_archive:
                created_at =\
                    dataset.get('requests_archived')[0].get('created_at')
                data = {'last_request_created_at': created_at}
                dataset.update(data)

        if organ['name'] == q_organization:
            grouped_requests_archive = sorted(grouped_requests_archive,
                                              key=lambda x: x[order],
                                              reverse=reverse)

        counters =\
            _get_action('requestdata_request_data_counters_get_by_org',
                        {'org_id': organ['id']})

        extra_vars = {
            'requests_new': requests_new,
            'requests_open': requests_open,
            'requests_archive': grouped_requests_archive,
            'maintainers': maintainers,
            'org_name': organ['name'],
            'current_order_name': current_order_name,
            'counters': counters
        }

        self._setup_template_variables(context, {'id': id},
                                       group_type=group_type)

        return render('requestdata/organization_requested_data.html',
                      extra_vars)
Exemple #3
0
    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)
def request_create(context, data_dict):
    '''Create new request data.

    :param sender_name: The name of the sender who request data.
    :type sender_name: string

    :param organization: The sender's organization.
    :type organization: string

    :param email_address: The sender's email_address.
    :type email_address: string

    :param message_content: The content of the message.
    :type message_content: string

    :param package_id: The id of the package the data belongs to.
    :type package_id: string

    :returns: the newly created request data
    :rtype: dictionary

    '''

    check_access('requestdata_request_create', context, data_dict)

    data, errors = df.validate(data_dict, schema.request_create_schema(),
                               context)

    if errors:
        raise toolkit.ValidationError(errors)

    sender_name = data.get('sender_name')
    organization = data.get('organization')
    email_address = data.get('email_address')
    message_content = data.get('message_content')
    package_id = data.get('package_id')

    package = toolkit.get_action('package_show')(context, {'id': package_id})

    sender_user_id = User.get(context['user']).id

    maintainers = package['maintainer'].split(',')

    data = {
        'sender_name': sender_name,
        'sender_user_id': sender_user_id,
        'organization': organization,
        'email_address': email_address,
        'message_content': message_content,
        'package_id': package_id
    }

    requestdata = ckanextRequestdata(**data)
    requestdata.save()
    maintainers_list = []
    is_hdx = helpers.is_hdx_portal()

    for id in maintainers:
        try:
            if is_hdx:
                main_ids = toolkit.get_action('user_show')(context, {'id': id})
                user = User.get(main_ids['id'])
            else:
                user = User.get(id)
            data = ckanextMaintainers()
            data.maintainer_id = user.id
            data.request_data_id = requestdata.id
            data.email = user.email
            maintainers_list.append(data)
        except NotFound:
            pass

    out = ckanextMaintainers.insert_all(maintainers_list, requestdata.id)

    return out