Пример #1
0
    def callable_method(key, data, errors, context):
        if isinstance(data[key], basestring):
            tags = [tag.strip() for tag in data[key].split(',') if tag.strip()]
        else:
            tags = data[key]

        current_index = max(
            [int(k[1])
             for k in data.keys() if len(k) == 3 and k[0] == 'tags'] + [-1])

        model = context['model']
        v = model.Vocabulary.get(vocab)

        if not v:
            _create_vocabulary(vocab)
            v = model.Vocabulary.get(vocab)

        context['vocabulary'] = v

        for num, tag in zip(count(current_index + 1), tags):
            data[('tags', num, 'name')] = tag
            data[('tags', num, 'vocabulary_id')] = v.id

        for tag in tags:
            tag_length_validator(tag, context)
            tag_name_validator(tag, context)
            _create_tag(tag, context)
Пример #2
0
    def callable_method(key, data, errors, context):
        if isinstance(data[key], basestring):
            tags = [tag.strip() for tag in data[key].split(',') if tag.strip()]
        else:
            tags = data[key]

        current_index = max([int(k[1]) for k in data.keys() if len(k) == 3 and k[0] == 'tags'] + [-1])

        model = context['model']
        v = model.Vocabulary.get(vocab)

        if not v:
            _create_vocabulary(vocab)
            v = model.Vocabulary.get(vocab)

        context['vocabulary'] = v

        for num, tag in zip(count(current_index + 1), tags):
            data[('tags', num, 'name')] = tag
            data[('tags', num, 'vocabulary_id')] = v.id

        for tag in tags:
            tag_length_validator(tag, context)
            tag_name_validator(tag, context)
            _create_tag(tag, context)
Пример #3
0
    def callable(key, data, errors, context):
        new_tags = data.get(key)
        if not new_tags:
            return
        if isinstance(new_tags, basestring):
            new_tags = [new_tags]

        # get current number of tags
        n = 0
        for k in data.keys():
            if k[0] == 'tags':
                n = max(n, k[1] + 1)

        v = model.Vocabulary.get(vocab)
        if not v:
            raise Invalid(_('Tag vocabulary "%s" does not exist') % vocab)
        context['vocabulary'] = v

        for tag in new_tags:
            tag_length_validator(tag, context)
            tag_name_validator(tag, context)
            tag_in_vocabulary_validator(tag, context)

        for num, tag in enumerate(new_tags):
            data[('tags', num+n, 'name')] = tag
            data[('tags', num+n, 'vocabulary_id')] = v.id
Пример #4
0
def add_to_vocab(context, tags, vocab):
    try:
        v = get_action('vocabulary_show')(context, {'id': vocab})
    except ObjectNotFound:
        v = plugin.create_vocabulary(vocab)

    context['vocabulary'] = model.Vocabulary.get(v.get('id'))

    for tag in tags:
        validators.tag_length_validator(tag, context)
        validators.tag_name_validator(tag, context)

        try:
            validators.tag_in_vocabulary_validator(tag, context)
        except Invalid:
            plugin.create_tag_to_vocabulary(tag, vocab)
Пример #5
0
def add_to_vocab(context, tags, vocab):
    try:
        v = get_action('vocabulary_show')(context, {'id': vocab})
    except ObjectNotFound:
        v = plugin.create_vocabulary(vocab)

    if isinstance(tags, basestring):
        tags = [tags]

    for tag in tags:
        validators.tag_length_validator(tag, context)
        validators.tag_name_validator(tag, context)

        try:
            validators.tag_in_vocabulary_validator(tag, context)
        except Invalid:
            plugin.create_tag_to_vocabulary(tag, vocab)
Пример #6
0
def keyword_string_convert(key, data, errors, context):
    '''
    Takes a list of tags that is a comma-separated string (in data[key])
    and parses tag names. These are added to the data dict, enumerated. They
    are also validated.
    '''
    if isinstance(data[key], basestring):
        tags = [tag.strip() for tag in data[key].split(',') if tag.strip()]
    else:
        tags = data[key]

    current_index = max(
        [int(k[1])
         for k in data.keys() if len(k) == 3 and k[0] == 'keywords'] + [-1])

    for num, tag in zip(itertools.count(current_index + 1), tags):
        data[('keywords', num, 'name')] = tag

    for tag in tags:
        val.tag_length_validator(tag, context)
        val.tag_name_validator(tag, context)
Пример #7
0
def add_to_vocab(context, tags, vocab):

    defer = context.get('defer', False)
    try:
        v = get_action('vocabulary_show')(context, {'id': vocab})
    except ObjectNotFound:
        log.info("creating vocab")
        v = plugin.create_vocabulary(vocab, defer)

    context['vocabulary'] = model.Vocabulary.get(v.get('id'))
    if isinstance(tags, basestring):
        tags = [tags]

    for tag in tags:
        validators.tag_length_validator(tag, context)
        validators.tag_name_validator(tag, context)

        try:
            validators.tag_in_vocabulary_validator(tag, context)
        except Invalid:
            plugin.create_tag_to_vocabulary(tag, vocab, defer)
Пример #8
0
def keyword_string_convert(key, data, errors, context):
    '''
    Takes a list of tags that is a comma-separated string (in data[key])
    and parses tag names. These are added to the data dict, enumerated. They
    are also validated.
    '''
    if isinstance(data[key], basestring):
        tags = [tag.strip() \
                for tag in data[key].split(',') \
                if tag.strip()]
    else:
        tags = data[key]

    current_index = max([int(k[1]) for k in data.keys() if len(k) == 3 and k[0] == 'keywords'] + [-1])

    for num, tag in zip(itertools.count(current_index + 1), tags):
        data[('keywords', num, 'name')] = tag

    for tag in tags:
        val.tag_length_validator(tag, context)
        val.tag_name_validator(tag, context)
Пример #9
0
def add_to_vocab(context, tags, vocab):

    defer = context.get('defer', False)
    try:
        v = get_action('vocabulary_show')(context, {'id': vocab})
    except ObjectNotFound:
        log.info("creating vocab")
        v = plugin.create_vocabulary(vocab, defer)

    context['vocabulary'] = model.Vocabulary.get(v.get('id'))
    if isinstance(tags, basestring):
        tags = [tags]

    for tag in tags:
        validators.tag_length_validator(tag, context)
        validators.tag_name_validator(tag, context)

        try:
            validators.tag_in_vocabulary_validator(tag, context)
        except Invalid:
            plugin.create_tag_to_vocabulary(tag, vocab, defer)
Пример #10
0
                .format(previous_object.guid))
            previous_object.current = False
            previous_object.add()

        # Flag this object as the current one
        harvest_object.current = True
        harvest_object.add()

        if status == 'new':
            try:
                package_dict = self._get_package_dict(harvest_object)

                # validate tags
                for tag in package_dict['tags']:
                    try:
                        validator_response = v.tag_name_validator(
                            tag['name'], context)
                    except Exception, e:
                        log.exception('Error processing tags: %r', e)
                        self._save_object_error('%r' % e.error, harvest_object,
                                                'Import')
                        return None

                # Save reference to the package on the object
                harvest_object.package_id = package_dict['id']

                # Defer constraints and flush so the dataset can be indexed with
                # the harvest object id (on the after_show hook from the harvester
                # plugin)
                model.Session.execute(
                    'SET CONSTRAINTS harvest_object_package_id_fkey DEFERRED')
                model.Session.flush()