Example #1
0
File: get.py Project: pingali/ckan
def tag_list(context, data_dict):
    '''Returns a list of tags'''

    model = context['model']
    user = context['user']

    all_fields = data_dict.get('all_fields', None)

    check_access('tag_list', context, data_dict)

    q = data_dict.get('q', '')
    if q:
        limit = data_dict.get('limit', 25)
        offset = data_dict.get('offset', 0)
        return_objects = data_dict.get('return_objects', True)

        query = query_for(model.Tag)
        query.run(query=q,
                  limit=limit,
                  offset=offset,
                  return_objects=return_objects,
                  username=user)
        tags = query.results
    else:
        tags = model.Session.query(model.Tag).all()

    tag_list = []
    if all_fields:
        for tag in tags:
            result_dict = tag_dictize(tag, context)
            tag_list.append(result_dict)
    else:
        tag_list = [tag.name for tag in tags]

    return tag_list
Example #2
0
File: get.py Project: jasonzou/ckan
def tag_list(context, data_dict):
    """Returns a list of tags"""

    model = context["model"]
    user = context["user"]

    all_fields = data_dict.get("all_fields", None)

    check_access("tag_list", context, data_dict)

    q = data_dict.get("q", "")
    if q:
        limit = data_dict.get("limit", 25)
        offset = data_dict.get("offset", 0)
        return_objects = data_dict.get("return_objects", True)

        query = query_for(model.Tag)
        query.run(query=q, limit=limit, offset=offset, return_objects=return_objects, username=user)
        tags = query.results
    else:
        tags = model.Session.query(model.Tag).all()

    tag_list = []
    if all_fields:
        for tag in tags:
            result_dict = tag_dictize(tag, context)
            tag_list.append(result_dict)
    else:
        tag_list = [tag.name for tag in tags]

    return tag_list
Example #3
0
File: get.py Project: pingali/ckan
def tag_show(context, data_dict):
    '''Shows tag details'''

    model = context['model']
    api = context.get('api_version') or '1'
    id = data_dict['id']

    tag = model.Tag.get(id)
    context['tag'] = tag

    if tag is None:
        raise NotFound

    check_access('tag_show', context, data_dict)

    tag_dict = tag_dictize(tag, context)

    extended_packages = []
    for package in tag_dict['packages']:
        pkg = model.Package.get(package['id'])
        extended_packages.append(package_dictize(pkg, context))

    tag_dict['packages'] = extended_packages

    return tag_dict
Example #4
0
def tag_list(context, data_dict):
    '''Returns a list of tags'''

    model = context['model']
    user = context['user']

    all_fields = data_dict.get('all_fields',None)

    check_access('tag_list',context, data_dict)

    q = data_dict.get('q','')
    if q:
        limit = data_dict.get('limit',25)
        offset = data_dict.get('offset',0)
        return_objects = data_dict.get('return_objects',True)

        query = query_for(model.Tag)
        query.run(query=q,
                  limit=limit,
                  offset=offset,
                  return_objects=return_objects,
                  username=user)
        tags = query.results
    else:
        tags = model.Session.query(model.Tag).all()

    tag_list = []
    if all_fields:
        for tag in tags:
            result_dict = tag_dictize(tag, context)
            tag_list.append(result_dict)
    else:
        tag_list = [tag.name for tag in tags]

    return tag_list
Example #5
0
def tag_show(context, data_dict):
    '''Shows tag details'''

    model = context['model']
    api = context.get('api_version') or '1'
    id = data_dict['id']

    tag = model.Tag.get(id)
    context['tag'] = tag

    if tag is None:
        raise NotFound

    check_access('tag_show',context, data_dict)

    tag_dict = tag_dictize(tag,context)

    extended_packages = []
    for package in tag_dict['packages']:
        pkg = model.Package.get(package['id'])
        extended_packages.append(package_dictize(pkg,context))

    tag_dict['packages'] = extended_packages

    return tag_dict
Example #6
0
File: get.py Project: jasonzou/ckan
def tag_show(context, data_dict):
    """Shows tag details"""

    model = context["model"]
    api = context.get("api_version") or "1"
    id = data_dict["id"]

    tag = model.Tag.get(id)
    context["tag"] = tag

    if tag is None:
        raise NotFound

    check_access("tag_show", context, data_dict)

    tag_dict = tag_dictize(tag, context)

    extended_packages = []
    for package in tag_dict["packages"]:
        pkg = model.Package.get(package["id"])
        extended_packages.append(package_dictize(pkg, context))

    tag_dict["packages"] = extended_packages

    return tag_dict
Example #7
0
    def test_tag_dictize_including_datasets(self):
        """By default a dictized tag should include the tag's datasets."""
        # Make a dataset in order to have a tag created.
        factories.Dataset(tags=[dict(name="test_tag")])
        tag = model.Tag.get("test_tag")

        tag_dict = model_dictize.tag_dictize(tag, context={"model": model})

        assert len(tag_dict["packages"]) == 1
Example #8
0
    def test_tag_dictize_including_datasets(self):
        """By default a dictized tag should include the tag's datasets."""
        # Make a dataset in order to have a tag created.
        factories.Dataset(tags=[dict(name="test_tag")])
        tag = model.Tag.get("test_tag")

        tag_dict = model_dictize.tag_dictize(tag, context={"model": model})

        assert len(tag_dict["packages"]) == 1
    def test_tag_dictize_not_including_datasets(self):
        """include_datasets=False should exclude datasets from tag dicts."""
        # Make a dataset in order to have a tag created.
        factories.Dataset(tags=[dict(name="test_tag")])
        tag = model.Tag.get("test_tag")

        tag_dict = model_dictize.tag_dictize(tag, context={"model": model},
                                             include_datasets=False)

        assert not tag_dict.get("packages")
Example #10
0
def tag_create(context, tag_dict):
    """Create a new tag and return a dictionary representation of it."""

    model = context["model"]

    check_access("tag_create", context, tag_dict)

    schema = context.get("schema") or ckan.logic.schema.default_create_tag_schema()
    data, errors = validate(tag_dict, schema, context)
    if errors:
        raise ValidationError(errors)

    tag = model_save.tag_dict_save(tag_dict, context)

    if not context.get("defer_commit"):
        model.repo.commit()

    log.debug("Created tag '%s' " % tag)
    return model_dictize.tag_dictize(tag, context)
Example #11
0
File: create.py Project: zydio/ckan
def tag_create(context, tag_dict):
    '''Create a new tag and return a dictionary representation of it.'''

    model = context['model']

    check_access('tag_create', context, tag_dict)

    schema = context.get('schema') or default_create_tag_schema()
    data, errors = validate(tag_dict, schema, context)
    if errors:
        raise ValidationError(errors)

    tag = tag_dict_save(tag_dict, context)

    if not context.get('defer_commit'):
        model.repo.commit()

    log.debug("Created tag '%s' " % tag)
    return tag_dictize(tag, context)
Example #12
0
def tag_create(context, tag_dict):
    '''Create a new tag and return a dictionary representation of it.'''

    model = context['model']

    check_access('tag_create', context, tag_dict)

    schema = context.get(
        'schema') or ckan.logic.schema.default_create_tag_schema()
    data, errors = validate(tag_dict, schema, context)
    if errors:
        raise ValidationError(errors)

    tag = model_save.tag_dict_save(tag_dict, context)

    if not context.get('defer_commit'):
        model.repo.commit()

    log.debug("Created tag '%s' " % tag)
    return model_dictize.tag_dictize(tag, context)
Example #13
0
def tag_create(context, data_dict):
    '''Create a new vocabulary tag.

    You must be a sysadmin to create vocabulary tags.

    You can only use this function to create tags that belong to a vocabulary,
    not to create free tags. (To create a new free tag simply add the tag to
    a package, e.g. using the
    :py:func:`~ckan.logic.action.update.package_update` function.)

    :param name: the name for the new tag, a string between 2 and 100
        characters long containing only alphanumeric characters and ``-``,
        ``_`` and ``.``, e.g. ``'Jazz'``
    :type name: string
    :param vocabulary_id: the name or id of the vocabulary that the new tag
        should be added to, e.g. ``'Genre'``
    :type vocabulary_id: string

    :returns: the newly-created tag
    :rtype: dictionary

    '''
    model = context['model']

    _check_access('tag_create', context, data_dict)

    schema = context.get('schema') or \
        ckan.logic.schema.default_create_tag_schema()
    data, errors = _validate(data_dict, schema, context)
    if errors:
        raise ValidationError(errors)

    tag = model_save.tag_dict_save(data_dict, context)

    if not context.get('defer_commit'):
        model.repo.commit()

    log.debug("Created tag '%s' " % tag)
    return model_dictize.tag_dictize(tag, context)
Example #14
0
def tag_create(context, data_dict):
    '''Create a new vocabulary tag.

    You must be a sysadmin to create vocabulary tags.

    You can only use this function to create tags that belong to a vocabulary,
    not to create free tags. (To create a new free tag simply add the tag to
    a package, e.g. using the
    :py:func:`~ckan.logic.action.update.package_update` function.)

    :param name: the name for the new tag, a string between 2 and 100
        characters long containing only alphanumeric characters and ``-``,
        ``_`` and ``.``, e.g. ``'Jazz'``
    :type name: string
    :param vocabulary_id: the name or id of the vocabulary that the new tag
        should be added to, e.g. ``'Genre'``
    :type vocabulary_id: string

    :returns: the newly-created tag
    :rtype: dictionary

    '''
    model = context['model']

    _check_access('tag_create', context, data_dict)

    schema = context.get('schema') or \
        ckan.logic.schema.default_create_tag_schema()
    data, errors = _validate(data_dict, schema, context)
    if errors:
        raise ValidationError(errors)

    tag = model_save.tag_dict_save(data_dict, context)

    if not context.get('defer_commit'):
        model.repo.commit()

    log.debug("Created tag '%s' " % tag)
    return model_dictize.tag_dictize(tag, context)
 def _tag_show(self, context, data_dict):
     '''Return the details of a tag and all its datasets.
 
     :param id: the name or id of the tag
     :type id: string
 
     :param package_details: if False details are not returned
     :type package_details: boolean
 
     :returns: the details of the tag, including a list of all of the tag's
         datasets and their details (if needed)
     :rtype: dictionary
 
     '''
     model = context['model']
     id = _get_or_bust(data_dict, 'id')
 
     tag = model.Tag.get(id)
     context['tag'] = tag
 
     if tag is None:
         raise NotFound
 
     _check_access('tag_show',context, data_dict)
 
     tag_dict = model_dictize.tag_dictize(tag,context)
 
     if data_dict.has_key('package_details') and data_dict['package_details'] == False:
         pass
     else:
         extended_packages = []
         for package in tag_dict['packages']:
             pkg = model.Package.get(package['id'])
             extended_packages.append(model_dictize.package_dictize(pkg,context))
     
         tag_dict['packages'] = extended_packages
 
     return tag_dict
Example #16
0
def tag_list(context, data_dict):
    '''Return a list of tag dictionaries.

    If a query is provided in the data_dict with key 'query' or 'q', then only
    tags whose names match the given query will be returned. Otherwise, all
    tags will be returned.

    By default only free tags (tags that don't belong to a vocabulary) are
    returned. If a 'vocabulary_id' is provided in the data_dict then tags
    belonging to the given vocabulary (id or name) will be returned instead.

    '''
    model = context['model']

    vocab_id_or_name = data_dict.get('vocabulary_id')
    query = data_dict.get('query') or data_dict.get('q')
    if query:
        query = query.strip()
    all_fields = data_dict.get('all_fields', None)

    check_access('tag_list', context, data_dict)

    if query:
        tags = _tag_search(context, data_dict)
    else:
        tags = model.Tag.all(vocab_id_or_name)

    tag_list = []
    if tags:
        if all_fields:
            for tag in tags:
                result_dict = model_dictize.tag_dictize(tag, context)
                tag_list.append(result_dict)
        else:
            tag_list.extend([tag.name for tag in tags])

    return tag_list
Example #17
0
def tag_list(context, data_dict):
    '''Return a list of tag dictionaries.

    If a query is provided in the data_dict with key 'query' or 'q', then only
    tags whose names match the given query will be returned. Otherwise, all
    tags will be returned.

    By default only free tags (tags that don't belong to a vocabulary) are
    returned. If a 'vocabulary_id' is provided in the data_dict then tags
    belonging to the given vocabulary (id or name) will be returned instead.

    '''
    model = context['model']

    vocab_id_or_name = data_dict.get('vocabulary_id')
    query = data_dict.get('query') or data_dict.get('q')
    if query:
        query = query.strip()
    all_fields = data_dict.get('all_fields', None)

    check_access('tag_list', context, data_dict)

    if query:
        tags = _tag_search(context, data_dict)
    else:
        tags = model.Tag.all(vocab_id_or_name)

    tag_list = []
    if tags:
        if all_fields:
            for tag in tags:
                result_dict = model_dictize.tag_dictize(tag, context)
                tag_list.append(result_dict)
        else:
            tag_list.extend([tag.name for tag in tags])

    return tag_list