Exemplo n.º 1
0
def inventory_organization_show(context, data_dict):
    model = context['model']
    group_extra = model.Session.query(model.GroupExtra) \
                       .filter_by(key='inventory_organization_id') \
                       .filter_by(value=data_dict['inventory_organization_id']) \
                       .first()

    if not group_extra:
        raise ObjectNotFound('Group extra was not found')

    organization = model.Group.get(group_extra.group_id)

    if not organization:
        raise ObjectNotFound('Organization was not found')

    return {'title': organization.title}
Exemplo n.º 2
0
def organization_by_inventory_id(context, data_dict):
    model = context['model']
    id = get_or_bust(data_dict, 'id')

    group_extra = model.meta.Session.query(model.GroupExtra) \
                       .filter_by(key='inventory_organization_id') \
                       .filter_by(value=id).first()
    if group_extra is None:
        raise ObjectNotFound('No GroupExtra with specificied inventory id')

    organization = model.meta.Session.query(model.Group) \
                        .filter_by(id=group_extra.group_id).first()
    if organization is None:
        raise ObjectNotFound('No organization with specificied inventory id')

    return model_dictize.group_dictize(organization, context)
Exemplo n.º 3
0
def inventory_entry_list(context, data_dict):
    '''Return a list of inventory entries.

    :param name: organization name
    :type name: string

    :rtype: list of dictionaries
    '''
    # TODO @palcu: define this
    # check_access('inventory_manage', context, data_dict)

    model = context['model']
    name = get_or_bust(data_dict, 'name')
    organization = model.Group.get(name)
    if not organization:
        raise ObjectNotFound('Organization was not found')

    entries = [
        table_dictize(entry, context)
        for entry in organization.inventory_entries
    ]

    for entry in entries:
        entry['next_deadline_timestamp'] = None
        if entry['last_added_dataset_timestamp']:
            last_added = _datestamp_to_datetime(
                entry['last_added_dataset_timestamp'])
            delta = timedelta(days=entry['recurring_interval'])
            entry['next_deadline_timestamp'] = last_added + delta
    return entries
Exemplo n.º 4
0
def get_cube(context, data_dict):
    """
    Return a dict representation of a cube, given a cubeId, if it exists.

    :param cubeId: ID of the cube to retrieve. (i.e. 1310001)
    :type cubeId: str

    :return: requested cube
    :rtype: dict

    :raises: ValidationError, ObjectObjectNotFound
    """
    cube_id = _get_or_bust(data_dict, 'cubeId')
    lc = ckanapi.LocalCKAN(context=context)
    result = lc.action.package_search(
        q=('type:cube AND '
           'product_id_new:{cube_id}').format(cube_id=cube_id),
        rows=1)

    if not result['count']:
        raise ObjectNotFound('Cube not found')
    elif result['count'] > 1:
        raise ValidationError('More than one cube with given cubeid found')
    else:
        return result['results'][-1]
Exemplo n.º 5
0
def create_access_request(context, data_dict):
    if not context.get('ignore_auth'):
        check_access('create_access_request', context, data_dict)

    pkg_id_or_name, reason, user = get_or_bust(data_dict,
                                               ['id', 'reason', 'user'])
    user_org = data_dict.get('user_org')

    # check if the package with such id exists to use it's ID
    pkg = Package.get(pkg_id_or_name)

    if not pkg:
        raise ObjectNotFound()

    req = AccessRequest.create_or_get_request(user_id=user,
                                              package_id=pkg.id,
                                              reason=reason,
                                              org_id=pkg.owner_org)

    # send email notifications to org custodians
    if config.get('spc.access_request.send_admin_notification'):
        notify_org_members(pkg.owner_org, {
            'pkg': pkg,
            'user': user,
            'reason': reason,
            'user_org': user_org
        })

    return req.as_dict()
Exemplo n.º 6
0
def scheming_organization_schema_show(context, data_dict):
    '''
    Return the scheming schema for a given organization type

    :param type: the organization type
    :param expanded: True to expand presets (default)
    '''
    t = get_or_bust(data_dict, 'type')
    expanded = data_dict.get('expanded', True)
    s = scheming_get_organization_schema(t, expanded)
    if s is None:
        raise ObjectNotFound()
    return s
Exemplo n.º 7
0
def inventory_entry_csv_single(context, data_dict):
    model = context['model']
    name = get_or_bust(data_dict, 'name')
    organization = model.Group.get(name)
    if not organization:
        raise ObjectNotFound('Organization was not found')

    entries = [
        table_dictize(entry, context)
        for entry in organization.inventory_entries
    ]

    inventory_entries = model.Session.query(InventoryEntry).join(model.Group)
    return [(entry.title, entry.recurring_interval,
             entry.last_added_dataset_timestamp)
            for entry in inventory_entries]
Exemplo n.º 8
0
def activate_user(context, data_dict):
    check_access('sysadmin', context, {})

    model = context['model']
    id = get_or_bust(data_dict, 'id')

    user_obj = model.User.get(id)
    if not user_obj:
        raise ObjectNotFound('User was not found')

    user_obj.activate()
    user_obj.save()
    try:
        send_reset_link(user_obj)
    except Exception, e:
        h.flash_error(_('Could not send reset link: %s') % unicode(e))
Exemplo n.º 9
0
def update_cube(context, data_dict):
    cube_id = _get_or_bust(data_dict, 'cubeId')
    cube_data = _get_or_bust(data_dict, 'cubeData')

    lc = ckanapi.LocalCKAN(context=context)

    result = lc.action.package_search(
        q=('type:cube AND product_id_new:{cube_id}'.format(cube_id=cube_id)),
        rows=1)

    if not result['count']:
        raise ObjectNotFound('Cube not found')
    elif result['count'] > 1:
        raise ValidationError('More than one cube with given cubeid found')

    cube = result['results'][0]
    cube.update(cube_data)

    return lc.action.package_update(**cube)
Exemplo n.º 10
0
def get_cube_list_by_subject(context, data_dict):
    """
    Return a dict with all Cube Ids and French/English titles based on a
    provided SubjectCode.

    :param subjectCode: two-digit subject code (i.e. 13)
    :type subjectCode: str

    :return: registered cubes for the SubjectCode and their
             French/English titles
    :rtype: list of dicts

    :raises: ValidationError, ObjectObjectNotFound
    """
    subject_code = _get_or_bust(data_dict, 'subjectCode')

    if len(subject_code) != 2:
        raise ValidationError('invalid subjectcode')

    lc = ckanapi.LocalCKAN(context=context)
    result = lc.action.package_search(
        q=('dataset_type:cube AND '
           '(subject_codes:{code} OR '
           'subject_codes:{code}*)').format(code=subject_code),
        rows=1000)

    count = result['count']
    if not count:
        raise ObjectNotFound(
            'Found no cubes with subject code {subject_code}'.format(
                subject_code=subject_code))
    else:
        return [{
            u'title': r['title'],
            u'cube_id': r['product_id_new']
        } for r in result['results']]
Exemplo n.º 11
0
def get_subject(context, data_dict):
    """
    :param: subjectCode: Subject Code (i.e. 13)
    :type: str

    :return: English, French and code values for given subjectCode
    :rtype: dict
    """
    lc = ckanapi.LocalCKAN(context=context)
    subject_code = get_or_bust(data_dict, 'subjectCode')

    response = lc.action.package_search(
        q='dataset_type:subject AND subject_code:{value}'.format(
            value=subject_code
        )
    )

    if not response['count']:
        raise ObjectNotFound(
            'No subject found with subject code {subject_code}'.format(
                subject_code=subject_code
            )
        )
    elif response['count'] > 1:
        raise ValidationError((
            'Duplicate SubjectCodes have been entered '
            'in CKAN {subject_code}'
        ).format(
            subject_code
        ))
    else:
        r = response['results'][0]
        return {
            'title': r['title'],
            'subject_code': r['subject_code']
        }
Exemplo n.º 12
0
def _get_user_obj(username):
    user = User.get(username)
    if not user:
        raise ObjectNotFound()
    return user