Exemplo n.º 1
0
def root_taxonomy(db):
    """Create root taxonomy element."""
    from flask_taxonomies.models import Taxonomy
    root = Taxonomy.create_taxonomy(code="root")
    db.session.add(root)
    db.session.commit()
    return root
Exemplo n.º 2
0
 def create_taxonomy(cls, code, extra_data=None) -> Taxonomy:
     """Creates a new taxonomy.
     :param code: taxonomy code
     :param extra_data: taxonomy metadata
     :raises IntegrityError
     :returns Taxonomy
     """
     before_taxonomy_created.send(current_app._get_current_object(),
                                  code=code, extra_data=extra_data)
     created = Taxonomy.create_taxonomy(code=code, extra_data=extra_data)
     db.session.commit()
     after_taxonomy_created.send(created)
     return created
Exemplo n.º 3
0
def create_update_taxonomy(data, drop):
    tax_dict = next(convert_data_to_dict(data))
    if 'code' not in tax_dict:
        raise ValueError('Taxonomy does not contain "code"')
    code = tax_dict.pop('code')
    taxonomy = Taxonomy.get(code=code)
    if taxonomy and drop:
        db.session.delete(taxonomy)
        db.session.commit()
        taxonomy = None

    if taxonomy:
        merged_dict = taxonomy.extra_data
        merged_dict.update(tax_dict)
        taxonomy.update(extra_data=merged_dict)
    else:
        taxonomy = Taxonomy.create_taxonomy(code, tax_dict)
    db.session.commit()
    return taxonomy