Пример #1
0
def create_update_terms(taxonomy,
                        taxonomy_data,
                        str_args: tuple = tuple(),
                        int_conversions: tuple = tuple(),
                        bool_args: tuple = tuple()):
    stack = [taxonomy]
    for term_dict in convert_data_to_dict(taxonomy_data, str_args=str_args,
                                          int_conversions=int_conversions, bool_args=bool_args):
        level = int(term_dict.pop('level'))
        try:
            slug = term_dict.pop('slug')
        except KeyError:
            slug = None
        while level < len(stack):
            stack.pop()
        if not slug:
            slug = slugify(term_dict["title"]["cs"])
        else:
            slug = slugify(slug)
        last_stack = stack[-1]
        if isinstance(last_stack, Taxonomy):
            identification = TermIdentification(taxonomy=taxonomy, slug=slug)
        else:
            identification = TermIdentification(parent=last_stack, slug=slug)
        term = current_flask_taxonomies.filter_term(identification).one_or_none()
        if term:
            current_flask_taxonomies.update_term(identification, extra_data=term_dict)
        else:
            term = current_flask_taxonomies.create_term(identification, extra_data=term_dict)
        stack.append(term)
    db.session.commit()
Пример #2
0
    def test_update_taxonomy_term(self, db, root_taxonomy, TaxonomyTerm):
        """Update TaxonomyTerm extra_data and name."""
        leaf = root_taxonomy.create_term(slug="leaf")

        current_flask_taxonomies.update_term(root_taxonomy, leaf,
                                             {'extra_data': {"description": "updated"}})

        retrieved_root = TaxonomyTerm.query.get(leaf.id)
        assert retrieved_root.extra_data == {"description": "updated"}

        root_taxonomy.check()
Пример #3
0
def patch_taxonomy_term(code=None,
                        slug=None,
                        prefer=None,
                        page=None,
                        size=None,
                        q=None):
    if q:
        json_abort(
            422, {
                'message':
                'Query not appropriate when creating or updating term',
                'reason': 'search-query-not-allowed'
            })
    taxonomy = current_flask_taxonomies.get_taxonomy(code, fail=False)
    if not taxonomy:
        json_abort(404, {})
    prefer = taxonomy.merge_select(prefer)

    if INCLUDE_DELETED in prefer:
        status_cond = sqlalchemy.sql.true()
    else:
        status_cond = TaxonomyTerm.status == TermStatusEnum.alive

    ti = TermIdentification(taxonomy=code, slug=slug)
    term = current_flask_taxonomies.filter_term(
        ti, status_cond=status_cond).one_or_none()

    if not term:
        abort(404)

    current_flask_taxonomies.permissions.taxonomy_term_update.enforce(
        request=request, taxonomy=taxonomy, term=term)

    current_flask_taxonomies.update_term(
        term,
        status_cond=status_cond,
        extra_data=request.json,
        patch=True,
        status=TermStatusEnum.alive  # make it alive if it  was deleted
    )
    current_flask_taxonomies.commit()

    return get_taxonomy_term(code=code,
                             slug=slug,
                             prefer=prefer,
                             page=page,
                             size=size)
Пример #4
0
def taxonomy_update_term(taxonomy, term, extra_data=None):
    """Update Term in Taxonomy."""
    changes = {}
    if extra_data:
        changes["extra_data"] = extra_data

    term = current_flask_taxonomies.update_term(taxonomy, term, changes)

    return jsonify(jsonify_taxonomy_term(term, taxonomy.code,
                                         term.tree_path), )
Пример #5
0
def _create_update_taxonomy_term_internal(code,
                                          slug,
                                          prefer,
                                          page,
                                          size,
                                          extra_data,
                                          if_none_match=False,
                                          if_match=False):
    try:
        taxonomy = current_flask_taxonomies.get_taxonomy(code)
        prefer = taxonomy.merge_select(prefer)

        if INCLUDE_DELETED in prefer:
            status_cond = sqlalchemy.sql.true()
        else:
            status_cond = TaxonomyTerm.status == TermStatusEnum.alive

        slug = '/'.join(slugify(x) for x in slug.split('/'))

        ti = TermIdentification(taxonomy=code, slug=slug)
        term = original_term = current_flask_taxonomies.filter_term(
            ti, status_cond=sqlalchemy.sql.true()).one_or_none()

        if term and INCLUDE_DELETED not in prefer:
            if term.status != TermStatusEnum.alive:
                term = None

        if if_none_match and term:
            json_abort(
                412, {
                    'message':
                    'The taxonomy already contains a term on this slug. ' +
                    'As If-None-Match: \'*\' has been requested, not modifying the term',
                    'reason':
                    'term-exists'
                })

        if if_match and not term:
            json_abort(
                412, {
                    'message':
                    'The taxonomy does not contain a term on this slug. ' +
                    'As If-Match: \'*\' has been requested, not creating a new term',
                    'reason':
                    'term-does-not-exist'
                })

        if term:
            current_flask_taxonomies.permissions.taxonomy_term_update.enforce(
                request=request, taxonomy=taxonomy, term=term)
            current_flask_taxonomies.update_term(term,
                                                 status_cond=status_cond,
                                                 extra_data=extra_data)
            status_code = 200
        else:
            if original_term:
                # there is a deleted term, so return a 409 Conflict
                json_abort(
                    409, {
                        'message':
                        'The taxonomy already contains a deleted term on this slug. '
                        'To reuse the term, repeat the operation with `del` in '
                        'representation:include.',
                        'reason':
                        'deleted-term-exists'
                    })

            current_flask_taxonomies.permissions.taxonomy_term_create.enforce(
                request=request, taxonomy=taxonomy, slug=slug)
            current_flask_taxonomies.create_term(ti, extra_data=extra_data)
            status_code = 201

        current_flask_taxonomies.commit()

        return get_taxonomy_term(code=code,
                                 slug=slug,
                                 prefer=prefer,
                                 page=page,
                                 size=size,
                                 status_code=status_code)

    except NoResultFound:
        json_abort(404, {})
    except:
        traceback.print_exc()
        raise
Пример #6
0
def test_taxonomy_term_update(app, db, taxonomy_tree, test_record):
    taxonomy = current_flask_taxonomies.get_taxonomy("test_taxonomy")
    terms = current_flask_taxonomies.list_taxonomy(taxonomy).all()
    old_record = Record.get_record(id_=test_record.id)
    assert old_record == {
        'pid': 1,
        'taxonomy': [{
            'is_ancestor': True,
            'level': 1,
            'links': {'self': 'http://127.0.0.1:5000/2.0/taxonomies/test_taxonomy/a'},
            'test': 'extra_data'
        },
            {
                'is_ancestor': True,
                'level': 2,
                'links': {
                    'parent': 'http://127.0.0.1:5000/2.0/taxonomies/test_taxonomy/a',
                    'self': 'http://127.0.0.1:5000/2.0/taxonomies/test_taxonomy/a/b'
                },
                'test': 'extra_data'
            },
            {
                'is_ancestor': False,
                'level': 3,
                'links': {
                    'parent': 'http://127.0.0.1:5000/2.0/taxonomies/test_taxonomy/a/b',
                    'self': 'http://127.0.0.1:5000/2.0/taxonomies/test_taxonomy/a/b/c'
                },
                'test': 'extra_data'
            }],
        'title': 'record 1'
    }
    term = terms[-1]
    current_flask_taxonomies.update_term(term, extra_data={"new_data": "changed extra data"})
    new_record = Record.get_record(id_=test_record.id)
    assert new_record == {
        'pid': 1,
        'taxonomy': [{
                         'is_ancestor': True,
                         'level': 1,
                         'links': {'self': 'http://127.0.0.1:5000/2.0/taxonomies/test_taxonomy/a'},
                         'test': 'extra_data'
                     },
                     {
                         'is_ancestor': True,
                         'level': 2,
                         'links': {
                             'parent': 'http://127.0.0.1:5000/2.0/taxonomies/test_taxonomy/a',
                             'self': 'http://127.0.0.1:5000/2.0/taxonomies/test_taxonomy/a/b'
                         },
                         'test': 'extra_data'
                     },
                     {
                         'is_ancestor': False,
                         'level': 3,
                         'links': {
                             'parent': 'http://127.0.0.1:5000/2.0/taxonomies/test_taxonomy/a/b',
                             'self': 'http://127.0.0.1:5000/2.0/taxonomies/test_taxonomy/a/b/c'
                         },
                         'new_data': 'changed extra data',
                         'test': 'extra_data'
                     }],
        'title': 'record 1'
    }
Пример #7
0
def test_redis(db, root_taxonomy, mkt):
    """Get terms  listassocitated with this taxonomy."""

    leaf = root_taxonomy.create_term(slug="leaf")
    nested = leaf.create_term(slug="nested")

    db.session.refresh(root_taxonomy)
    db.session.refresh(leaf)
    db.session.refresh(nested)

    jsonRes = JSONResolver(entry_point_group='invenio_records.jsonresolver')
    document = jsonRes.resolve("http://taxonomy-server.com/api/taxonomies/root/leaf")
    assert document == {
        'descendants_count': 1.0,
        'id': leaf.id,
        'links': {'parent': 'http://localhost/taxonomies/root/',
                  'parent_tree': 'http://localhost/taxonomies/root/?drilldown=True',
                  'self': 'http://localhost/taxonomies/root/leaf/',
                  'tree': 'http://localhost/taxonomies/root/leaf/?drilldown=True'},
        'path': '/leaf',
        'slug': 'leaf',
        'level': 1
    }

    # change the document directly in the database
    leaf.extra_data = {
        'title': 'a title'
    }
    db.session.add(leaf)
    db.session.commit()

    # the document must be the original one - coming from redis
    document = jsonRes.resolve("http://taxonomy-server.com/api/taxonomies/root/leaf")
    assert document == {
        'descendants_count': 1.0,
        'id': leaf.id,
        'links': {'parent': 'http://localhost/taxonomies/root/',
                  'parent_tree': 'http://localhost/taxonomies/root/?drilldown=True',
                  'self': 'http://localhost/taxonomies/root/leaf/',
                  'tree': 'http://localhost/taxonomies/root/leaf/?drilldown=True'},
        'path': '/leaf',
        'slug': 'leaf',
        'level': 1
    }

    # change the document via api
    current_flask_taxonomies.update_term(root_taxonomy, leaf, {
        'extra_data': {'title': 'a title'}
    })
    document = jsonRes.resolve("http://taxonomy-server.com/api/taxonomies/root/leaf")
    assert document == {
        'descendants_count': 1.0,
        'id': leaf.id,
        'title': 'a title',
        'links': {'parent': 'http://localhost/taxonomies/root/',
                  'parent_tree': 'http://localhost/taxonomies/root/?drilldown=True',
                  'self': 'http://localhost/taxonomies/root/leaf/',
                  'tree': 'http://localhost/taxonomies/root/leaf/?drilldown=True'},
        'path': '/leaf',
        'slug': 'leaf',
        'level': 1
    }