def test_move_taxonomy_prohibited(self, db, root_taxonomy, mkt):
        t1 = root_taxonomy.create_term(slug="1")
        t11 = t1.create_term(slug="11")
        t12 = t1.create_term(slug="12")

        with pytest.raises(TaxonomyError,
                           match='Can not move term inside, before or after the same term'):
            current_flask_taxonomies.move_term(root_taxonomy, term_path=t1.tree_path,
                                               target_path=root_taxonomy.code + t1.tree_path,
                                               destination_order=MovePosition.INSIDE)
Exemple #2
0
def test_move_term(app, db, taxonomy_tree):
    taxonomy = current_flask_taxonomies.get_taxonomy("test_taxonomy")
    terms = current_flask_taxonomies.list_taxonomy(taxonomy).all()
    print(terms)
    ti = TermIdentification(term=terms[2])
    current_flask_taxonomies.move_term(ti,
                                       new_parent=terms[0],
                                       remove_after_delete=False)
    db.session.commit()
    terms = current_flask_taxonomies.list_taxonomy(taxonomy).all()
    print(terms)
    def test_move_taxonomy_term_after(self, db, root_taxonomy, mkt):
        t1 = root_taxonomy.create_term(slug="leaf1")
        t2 = root_taxonomy.create_term(slug="leaf2")
        t3 = root_taxonomy.create_term(slug="leaf3")

        root_taxonomy.check()
        assert list(root_taxonomy.dump()) == mkt('leaf1', 'leaf2', 'leaf3')

        current_flask_taxonomies.move_term(root_taxonomy, term_path=t2.tree_path,
                                           target_path=root_taxonomy.code + t1.tree_path,
                                           destination_order=MovePosition.AFTER)
        root_taxonomy.check()
        assert list(root_taxonomy.dump()) == mkt('leaf1', 'leaf2', 'leaf3')
Exemple #4
0
def taxonomy_move_term(taxonomy,
                       term_path='',
                       destination='',
                       destination_order=''):
    """Create a Term inside a Taxonomy tree."""
    target_path = None
    if not destination:
        abort(400, "No destination given.")

    try:
        target_path = url_to_path(destination)
    except ValueError:
        abort(400, 'Invalid target URL passed.')

    try:
        term, target_taxonomy, target_term = current_flask_taxonomies.move_term(
            taxonomy, term_path, target_path, destination_order)
        moved = jsonify_taxonomy_term(term, target_taxonomy.code,
                                      term.tree_path)
        response = jsonify(moved)
        response.headers['Location'] = moved['links']['self']
        return response
    except AttributeError as e:
        abort(400, str(e))
    except NoResultFound:
        abort(400, "Target path not found.")
    def test_move_taxonomy_term_inside(self, db, root_taxonomy, mkt):
        t1 = root_taxonomy.create_term(slug="leaf1")
        t2 = root_taxonomy.create_term(slug="leaf2")

        root_taxonomy.check()
        assert list(root_taxonomy.dump()) == mkt('leaf1', 'leaf2')

        current_flask_taxonomies.move_term(root_taxonomy, term_path=t1.tree_path,
                                           destination_order=MovePosition.INSIDE)
        root_taxonomy.check()
        assert list(root_taxonomy.dump()) == mkt('leaf2', 'leaf1')

        current_flask_taxonomies.move_term(root_taxonomy, term_path=t2.tree_path,
                                           destination_order=MovePosition.INSIDE)
        root_taxonomy.check()
        assert list(root_taxonomy.dump()) == mkt('leaf1', 'leaf2')
Exemple #6
0
def test_taxonomy_term_moved(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)
    old_taxonomy = old_record["taxonomy"]
    assert old_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'
        }]
    ti = TermIdentification(term=terms[2])
    current_flask_taxonomies.move_term(ti, new_parent=terms[0], remove_after_delete=False)
    db.session.commit()
    new_record = Record.get_record(id_=test_record.id)
    new_taxonomy = new_record["taxonomy"]
    new_terms = current_flask_taxonomies.list_taxonomy(taxonomy).all()
    assert new_terms[-1].parent_id == 1
    def test_move_taxonomy_term_with_children_inside(self, db, root_taxonomy, mkt):
        t1 = root_taxonomy.create_term(slug="leaf1")
        t1.create_term(slug="leaf11")
        t1.create_term(slug="leaf12")
        t2 = root_taxonomy.create_term(slug="leaf2")
        t2.create_term(slug="leaf21")
        t2.create_term(slug="leaf22")

        root_taxonomy.check()
        print(mkt(
            ('leaf1',
             'leaf11', 'leaf12'),
            ('leaf2',
             'leaf21', 'leaf22')))
        assert list(root_taxonomy.dump()) == mkt(
            ('leaf1',
             'leaf11', 'leaf12'),
            ('leaf2',
             'leaf21', 'leaf22'))

        current_flask_taxonomies.move_term(root_taxonomy, term_path=t1.tree_path,
                                           destination_order=MovePosition.INSIDE)
        root_taxonomy.check()
        assert list(root_taxonomy.dump()) == mkt(
            ('leaf2',
             'leaf21', 'leaf22'),
            ('leaf1',
             'leaf11', 'leaf12'),
        )
        current_flask_taxonomies.move_term(root_taxonomy, term_path=t2.tree_path,
                                           destination_order=MovePosition.INSIDE)
        root_taxonomy.check()
        assert list(root_taxonomy.dump()) == mkt(
            ('leaf1',
             'leaf11', 'leaf12'),
            ('leaf2',
             'leaf21', 'leaf22'))
Exemple #8
0
def taxonomy_move_term(code=None,
                       slug=None,
                       prefer=None,
                       page=None,
                       size=None,
                       destination='',
                       rename='',
                       q=None):
    """Move term into a new parent or rename it."""
    if q:
        json_abort(
            422, {
                'message': 'Query not appropriate when moving term',
                'reason': 'search-query-not-allowed'
            })

    try:
        taxonomy = current_flask_taxonomies.get_taxonomy(code)
        ti = TermIdentification(taxonomy=taxonomy, slug=slug)
        term = current_flask_taxonomies.filter_term(ti).one()

        current_flask_taxonomies.permissions.taxonomy_term_move.enforce(
            request=request,
            taxonomy=taxonomy,
            term=term,
            destination=destination,
            rename=rename)
    except NoResultFound as e:
        return json_abort(404, {})

    if destination:
        if destination.startswith('http'):
            destination_path = urlparse(destination).path
            url_prefix = current_app.config['FLASK_TAXONOMIES_URL_PREFIX']
            if not destination_path.startswith(url_prefix):
                abort(
                    400, 'Destination not part of this server as it '
                    'does not start with config.FLASK_TAXONOMIES_URL_PREFIX')
            destination_path = destination_path[len(url_prefix):]
            destination_path = destination_path.split('/', maxsplit=1)
            if len(destination_path) > 1:
                destination_taxonomy, destination_slug = destination_path
            else:
                destination_taxonomy = destination_path[0]
                destination_slug = None
        else:
            destination_taxonomy = code
            destination_slug = destination
            if destination_slug.startswith('/'):
                destination_slug = destination_slug[1:]
        if not current_flask_taxonomies.filter_term(
                TermIdentification(taxonomy=code, slug=slug)).count():
            abort(404, 'Term %s/%s does not exist' % (code, slug))

        try:
            old_term, new_term = current_flask_taxonomies.move_term(
                TermIdentification(taxonomy=code, slug=slug),
                new_parent=TermIdentification(taxonomy=destination_taxonomy,
                                              slug=destination_slug)
                if destination_slug else '',
                remove_after_delete=False
            )  # do not remove the original node from the database,
            # just mark it as deleted
        except TaxonomyTermBusyError as e:
            return json_abort(412, {'message': str(e), 'reason': 'term-busy'})
    elif rename:
        new_slug = slug
        if new_slug.endswith('/'):
            new_slug = new_slug[:-1]
        if '/' in new_slug:
            new_slug = new_slug.rsplit('/')[0]
            new_slug = new_slug + '/' + rename
        else:
            new_slug = rename
        try:
            old_term, new_term = current_flask_taxonomies.rename_term(
                TermIdentification(taxonomy=code, slug=slug),
                new_slug=new_slug,
                remove_after_delete=False
            )  # do not remove the original node from the database, just mark it as deleted
        except TaxonomyTermBusyError as e:
            return json_abort(412, {'message': str(e), 'reason': 'term-busy'})
        destination_taxonomy = code
    else:
        abort(400, 'Pass either `destination` or `rename` parameters ')
        return  # just to make pycharm happy

    current_flask_taxonomies.commit()

    return get_taxonomy_term(code=destination_taxonomy,
                             slug=new_term.slug,
                             prefer=prefer,
                             page=page,
                             size=size)