Пример #1
0
    def move_term(cls, taxonomy, term_path='', target_path=None, destination_order=''):
        """Moves a taxonomy term to a different path.
        :param taxonomy: source taxonomy instance
        :param term_path: current path of a term
        :param target_path: desired path of a term
        :param destination_order: order in a destination tree
        :raise AttributeError
        :raise NoResultFound
        :return Tuple(Taxonomy, Taxonomy, Optional[Taxonomy]): target taxonomy and moved term
        """
        taxonomy.lock()
        term = taxonomy.find_term(term_path)
        if not term:
            raise AttributeError('Invalid Term path given.')

        before_taxonomy_term_moved.send(
            term, taxonomy=taxonomy,
            target_path=target_path, order=destination_order)

        if not target_path:
            target_path = f'{taxonomy.code}/'

        target_taxonomy, target_term = Taxonomy.find_taxonomy_and_term(target_path)

        term.move(target_term, position=MovePosition(destination_order or 'inside'))
        db.session.commit()

        after_taxonomy_term_moved.send(term, taxonomy=taxonomy, term=term)
        db.session.refresh(term)
        return (term, target_taxonomy, target_term)
Пример #2
0
    def convert_ref(self, in_data, **kwargs):
        ref = None
        if '$ref' in in_data:
            ref = in_data['$ref']
        elif 'links' in in_data:
            ref = (in_data['links'] or {}).get('self', None)
        if not ref:
            raise ValidationError('Either links or $ref must be provided for a Taxonomy record')  # noqa

        path = url_to_path(ref)
        try:
            tax, term = Taxonomy.find_taxonomy_and_term(path)
        except NoResultFound:
            raise ValidationError('Taxonomy $ref link is invalid: {}'.format(ref))  # noqa

        if not tax:
            raise ValidationError('Taxonomy $ref link is invalid: {}'.format(ref))  # noqa

        return {'$ref': ref}