示例#1
0
def test_api(app, db):
    taxonomy = current_flask_taxonomies.create_taxonomy("root",
                                                        extra_data={},
                                                        session=db.session)
    db.session.commit()

    identification = TermIdentification(taxonomy=taxonomy, slug="a")
    term = current_flask_taxonomies.create_term(identification,
                                                extra_data={"a": 1})
    assert term.slug == "a"
    db.session.commit()

    identification2 = TermIdentification(parent=term, slug="b")
    term2 = current_flask_taxonomies.create_term(identification2,
                                                 extra_data={"b": 2})
    assert term2.slug == "a/b"
    db.session.commit()

    res = current_flask_taxonomies.list_taxonomies().all()
    print(res)

    # filter
    term_identification = TermIdentification(taxonomy=taxonomy, slug=term.slug)
    assert list(
        current_flask_taxonomies.filter_term(term_identification)) == [term]

    assert list(
        current_flask_taxonomies.filter_term(
            TermIdentification(taxonomy=taxonomy,
                               slug=term2.slug))) == [term2]

    res_term = current_flask_taxonomies.filter_term(
        term_identification).one_or_none()
    assert isinstance(res_term, TaxonomyTerm)
示例#2
0
def taxonomy_tree(app, db, taxonomy):
    id1 = TermIdentification(taxonomy=taxonomy, slug="a")
    term1 = current_flask_taxonomies.create_term(
        id1, extra_data={"test": "extra_data"})
    id2 = TermIdentification(parent=term1, slug="b")
    term2 = current_flask_taxonomies.create_term(
        id2, extra_data={"test": "extra_data"})
    id3 = TermIdentification(taxonomy=taxonomy, slug="a/b/c")
    term3 = current_flask_taxonomies.create_term(
        id3, extra_data={"test": "extra_data"})
    db.session.commit()
示例#3
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()
示例#4
0
def import_countries(db):
    # with db.session.begin_nested():
    try:
        Base.metadata.create_all(db.engine)
    except:
        pass

    tax = current_flask_taxonomies.get_taxonomy(code='country', fail=False)
    if tax:
        return

    tax = current_flask_taxonomies.create_taxonomy(
        code='country',
        extra_data={'title': 'List of countries'},
        url='https://www.kaggle.com/nikitagrec/world-capitals-gps/data')

    continents = {}
    with open(os.path.join(os.path.dirname(__file__), 'countries.csv'),
              'r') as f:
        rdr = csv.DictReader(f)
        for row in rdr:
            try:
                continent = row['ContinentName']
                country = row['CountryCode']
                if continent not in continents:
                    print('Creating continent', continent.lower())
                    continent_term = current_flask_taxonomies.create_term(
                        TermIdentification(taxonomy=tax,
                                           slug=continent.lower().replace(
                                               ' ', '-')), )
                    continents[continent] = continent_term

                slug = '%s/%s' % (continent.lower(), country.lower())
                slug = slug.replace(' ', '-')
                print('Importing', slug)
                current_flask_taxonomies.create_term(TermIdentification(
                    taxonomy=tax, slug=slug),
                                                     extra_data=row)
            except:
                traceback.print_exc()
                return
    try:
        db.session.commit()
    except:
        traceback.print_exc()
        sys.exit(1)
示例#5
0
def _taxonomy_create_term_internal(taxonomy,
                                   slug=None,
                                   term_path='',
                                   extra_data=None,
                                   move_target=None):
    """Create a Term inside a Taxonomy tree."""
    if not slug:
        abort(400, "No slug given for created element.")

    try:
        created = current_flask_taxonomies.create_term(taxonomy=taxonomy,
                                                       term_path=term_path,
                                                       slug=slug,
                                                       extra_data=extra_data)
        created_dict = \
            jsonify_taxonomy_term(created, taxonomy.code, created.tree_path)
        response = jsonify(created_dict)
        response.headers['Location'] = created_dict['links']['self']
        response.status_code = 201
        return response
    except AttributeError as e:
        abort(400, str(e))
    except IntegrityError:
        abort(400, 'Term with this slug already exists on this path.')
示例#6
0
def taxonomy_tree(app, db, taxonomy):
    # accessRights
    id1 = TermIdentification(taxonomy=taxonomy, slug="c_abf2")
    term1 = current_flask_taxonomies.create_term(
        id1,
        extra_data={
            "title": {
                "cs": "otevřený přístup",
                "en": "open access"
            },
            "relatedURI": {
                "coar": "http://purl.org/coar/access_right/c_abf2",
                "vocabs":
                "https://vocabs.acdh.oeaw.ac.at/archeaccessrestrictions/public",
                "eprint": "http://purl.org/eprint/accessRights/OpenAccess"
            }
        })

    # resource type
    id2 = TermIdentification(taxonomy=taxonomy, slug="bakalarske_prace")
    term2 = current_flask_taxonomies.create_term(id2,
                                                 extra_data={
                                                     "title": {
                                                         "cs":
                                                         "Bakalářské práce",
                                                         "en":
                                                         "Bachelor’s theses"
                                                     }
                                                 })

    # institution
    id3 = TermIdentification(taxonomy=taxonomy, slug="61384984")
    term3 = current_flask_taxonomies.create_term(
        id3,
        extra_data={
            "title": {
                "cs": "Akademie múzických umění v Praze",
                "en": "Academy of Performing Arts in Prague"
            },
            "type": "veřejná VŠ",
            "aliases": ["AMU"],
            "related": {
                "rid": "51000"
            },
            "address": "Malostranské náměstí 259/12, 118 00 Praha 1",
            "ico": "61384984",
            "url": "https://www.amu.cz",
            "provider": True,
        })

    # language
    id4 = TermIdentification(taxonomy=taxonomy, slug="cze")
    term4 = current_flask_taxonomies.create_term(
        id4, extra_data={"title": {
            "cs": "čeština",
            "en": "Czech"
        }})

    # contributor
    id5 = TermIdentification(taxonomy=taxonomy, slug="supervisor")
    term5 = current_flask_taxonomies.create_term(id5,
                                                 extra_data={
                                                     "title": {
                                                         "cs": "supervizor",
                                                         "en": "supervisor"
                                                     },
                                                     "dataCiteCode":
                                                     "Supervisor"
                                                 })

    # funder
    id6 = TermIdentification(taxonomy=taxonomy, slug="ntk")
    term6 = current_flask_taxonomies.create_term(
        id6,
        extra_data={
            "title": {
                "cs": "Národní technická knihovna",
                "en": "National library of technology"
            },
            "funderISVaVaICode": "123456789"
        })

    # country
    id7 = TermIdentification(taxonomy=taxonomy, slug="cz")
    term7 = current_flask_taxonomies.create_term(id7,
                                                 extra_data={
                                                     "title": {
                                                         "cs": "Česko",
                                                         "en": "Czechia"
                                                     },
                                                     "code": {
                                                         "number": "203",
                                                         "alpha2": "CZ",
                                                         "alpha3": "CZE"
                                                     }
                                                 })

    # relationship
    id8 = TermIdentification(taxonomy=taxonomy, slug="isversionof")
    term8 = current_flask_taxonomies.create_term(
        id8, extra_data={"title": {
            "cs": "jeVerzí",
            "en": "isVersionOf"
        }})

    # rights
    id9 = TermIdentification(taxonomy=taxonomy, slug="copyright")
    term9 = current_flask_taxonomies.create_term(
        id9,
        extra_data={
            "title": {
                "cs":
                "Dílo je chráněno podle autorského zákona č. 121/2000 Sb.",
                "en":
                "This work is protected under the Copyright Act No. 121/2000 Coll."
            }
        })

    # series
    id9 = TermIdentification(taxonomy=taxonomy, slug="maj")
    term9 = current_flask_taxonomies.create_term(id9,
                                                 extra_data={
                                                     "name": "maj",
                                                     "volume": "1"
                                                 })

    # subject
    id10 = TermIdentification(taxonomy=taxonomy, slug="psh3001")
    term10 = current_flask_taxonomies.create_term(
        id10,
        extra_data={
            "title": {
                "cs": "Reynoldsovo číslo",
                "en": "Reynolds number"
            },
            "reletedURI": ["http://psh.techlib.cz/skos/PSH3001"],
            "DateRevised": "2007-01-26T16:14:37"
        })

    id11 = TermIdentification(taxonomy=taxonomy, slug="psh3000")
    term11 = current_flask_taxonomies.create_term(
        id11,
        extra_data={
            "title": {
                "cs": "turbulentní proudění",
                "en": "turbulent flow"
            },
            "reletedURI": ["http://psh.techlib.cz/skos/PSH3000"],
            "DateRevised": "2007-01-26T16:14:37"
        })

    id12 = TermIdentification(taxonomy=taxonomy, slug="D010420")
    term12 = current_flask_taxonomies.create_term(
        id12,
        extra_data={
            "title": {
                "cs": "pentany",
                "en": "Pentanes"
            },
            "reletedURI": [
                "http://www.medvik.cz/link/D010420",
                "http://id.nlm.nih.gov/mesh/D010420"
            ],
            "DateRevised":
            "2007-01-26T16:14:37",
            "DateCreated":
            "2007-01-26T16:14:37",
            "DateDateEstablished":
            "2007-01-26T16:14:37",
        })

    # studyField
    id13 = TermIdentification(taxonomy=taxonomy,
                              slug="O_herectvi-alternativniho-divadla")
    term13 = current_flask_taxonomies.create_term(
        id13,
        extra_data={
            "title": {
                "cs": "Herectví alternativního divadla",
            },
            "AKVO": "8203R082"
        })

    # conference
    id14 = TermIdentification(taxonomy=taxonomy, slug="cze_conference")
    term14 = current_flask_taxonomies.create_term(id14,
                                                  extra_data={
                                                      "title": {
                                                          "cs":
                                                          "Česká konference",
                                                      },
                                                  })

    # certifying authority
    id15 = TermIdentification(taxonomy=taxonomy, slug="mdcr")
    term15 = current_flask_taxonomies.create_term(
        id15,
        extra_data={
            "title": {
                "cs": "Ministerstvo dopravy",
                "en": "Ministry of transport"
            },
        })

    # N_resultUsage
    id16 = TermIdentification(taxonomy=taxonomy, slug="C")
    term16 = current_flask_taxonomies.create_term(
        id16,
        extra_data={
            "title": {
                "cs": "Výsledek je užíván bez omezení okruhu uživatelů",
            },
        })

    # N_resultUsage
    id17 = TermIdentification(taxonomy=taxonomy, slug="A")
    term17 = current_flask_taxonomies.create_term(
        id17,
        extra_data={
            "title": {
                "cs": "certifikovaná metodika (NmetC)",
            },
        })

    db.session.commit()
示例#7
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
示例#8
0
def taxonomy_tree(app, db, taxonomy):
    # accessRights
    id1 = TermIdentification(taxonomy=taxonomy, slug="c_abf2")
    term1 = current_flask_taxonomies.create_term(
        id1,
        extra_data={
            "title": {
                "cs": "otevřený přístup",
                "en": "open access"
            },
            "relatedURI": {
                "coar": "http://purl.org/coar/access_right/c_abf2",
                "vocabs":
                "https://vocabs.acdh.oeaw.ac.at/archeaccessrestrictions/public",
                "eprint": "http://purl.org/eprint/accessRights/OpenAccess"
            }
        })

    # resource type
    id2 = TermIdentification(taxonomy=taxonomy, slug="bakalarske_prace")
    term2 = current_flask_taxonomies.create_term(id2,
                                                 extra_data={
                                                     "title": {
                                                         "cs":
                                                         "Bakalářské práce",
                                                         "en":
                                                         "Bachelor’s theses"
                                                     }
                                                 })

    # institution
    id3 = TermIdentification(taxonomy=taxonomy, slug="61384984")
    term3 = current_flask_taxonomies.create_term(
        id3,
        extra_data={
            "title": {
                "cs": "Akademie múzických umění v Praze",
                "en": "Academy of Performing Arts in Prague"
            },
            "type": "veřejná VŠ",
            "aliases": ["AMU"],
            "related": {
                "rid": "51000"
            },
            "address": "Malostranské náměstí 259/12, 118 00 Praha 1",
            "ico": "61384984",
            "url": "https://www.amu.cz",
            "provider": True,
        })

    id3b = TermIdentification(taxonomy=taxonomy, slug="60461373")
    term3b = current_flask_taxonomies.create_term(
        id3b,
        extra_data={
            "title": {
                "cs": "Vysoká škola chemicko-technologická v Praze",
                "en": "University of Chemistry and Technology, Prague"
            },
            "type": "veřejná VŠ",
            "aliases": ["VŠCHT"],
            "related": {
                "rid": "22000"
            },
            "address": "Technická 5, 166 28 Praha 6",
            "ico": "60461373",
            "url": "https://www.vscht.cz",
            "provider": True,
        })

    # language
    id4 = TermIdentification(taxonomy=taxonomy, slug="cze")
    term4 = current_flask_taxonomies.create_term(
        id4, extra_data={"title": {
            "cs": "čeština",
            "en": "Czech"
        }})

    # contributor
    id5 = TermIdentification(taxonomy=taxonomy, slug="supervisor")
    term5 = current_flask_taxonomies.create_term(id5,
                                                 extra_data={
                                                     "title": {
                                                         "cs": "supervizor",
                                                         "en": "supervisor"
                                                     },
                                                     "dataCiteCode":
                                                     "Supervisor"
                                                 })

    # funder
    id6 = TermIdentification(taxonomy=taxonomy, slug="ntk")
    term6 = current_flask_taxonomies.create_term(
        id6,
        extra_data={
            "title": {
                "cs": "Národní technická knihovna",
                "en": "National library of technology"
            },
            "funderISVaVaICode": "123456789"
        })

    # country
    id7 = TermIdentification(taxonomy=taxonomy, slug="cz")
    term7 = current_flask_taxonomies.create_term(id7,
                                                 extra_data={
                                                     "title": {
                                                         "cs": "Česko",
                                                         "en": "Czechia"
                                                     },
                                                     "code": {
                                                         "number": "203",
                                                         "alpha2": "CZ",
                                                         "alpha3": "CZE"
                                                     }
                                                 })

    # relationship
    id8 = TermIdentification(taxonomy=taxonomy, slug="isversionof")
    term8 = current_flask_taxonomies.create_term(
        id8, extra_data={"title": {
            "cs": "jeVerzí",
            "en": "isVersionOf"
        }})

    # rights
    id9 = TermIdentification(taxonomy=taxonomy, slug="copyright")
    term9 = current_flask_taxonomies.create_term(
        id9,
        extra_data={
            "title": {
                "cs":
                "Dílo je chráněno podle autorského zákona č. 121/2000 Sb.",
                "en":
                "This work is protected under the Copyright Act No. 121/2000 Coll."
            }
        })

    # series
    id9 = TermIdentification(taxonomy=taxonomy, slug="maj")
    term9 = current_flask_taxonomies.create_term(id9,
                                                 extra_data={
                                                     "seriesTitle": "maj",
                                                     "seriesVolume": "1"
                                                 })

    # subject
    id10 = TermIdentification(taxonomy=taxonomy, slug="psh3001")
    term10 = current_flask_taxonomies.create_term(
        id10,
        extra_data={
            "title": {
                "cs": "Reynoldsovo číslo",
                "en": "Reynolds number"
            },
            "reletedURI": ["http://psh.techlib.cz/skos/PSH3001"],
            "DateRevised": "2007-01-26T16:14:37"
        })

    id11 = TermIdentification(taxonomy=taxonomy, slug="psh3000")
    term11 = current_flask_taxonomies.create_term(
        id11,
        extra_data={
            "title": {
                "cs": "turbulentní proudění",
                "en": "turbulent flow"
            },
            "reletedURI": ["http://psh.techlib.cz/skos/PSH3000"],
            "DateRevised": "2007-01-26T16:14:37"
        })

    id12 = TermIdentification(taxonomy=taxonomy, slug="D010420")
    term12 = current_flask_taxonomies.create_term(
        id12,
        extra_data={
            "title": {
                "cs": "pentany",
                "en": "Pentanes"
            },
            "reletedURI": [
                "http://www.medvik.cz/link/D010420",
                "http://id.nlm.nih.gov/mesh/D010420"
            ],
            "DateRevised":
            "2007-01-26T16:14:37",
            "DateCreated":
            "2007-01-26T16:14:37",
            "DateDateEstablished":
            "2007-01-26T16:14:37",
        })

    db.session.commit()