def authors_():
    """
    POST: add an author
    GET: get list of authors
    :return:
    """
    if request.method == "GET":
        n_page, size, sort_by = get_pagination(request)

        the_query = AuthorDB.query
        # sort_desc = request.args.get("sortDesc")
        # region filter
        first_name = request.args.get("first_name", "")
        if first_name:
            the_query = the_query.filter(
                AuthorDB.first_name.like(f"%{first_name}%"))
            logging.error(f"{first_name}")
        family_name = request.args.get("family_name", "")
        if family_name:
            the_query = the_query.filter(
                AuthorDB.family_name.like(f"%{family_name}%"))
            logging.error(f"{family_name}")
        valid = request.args.get("valid", "1")
        if valid in ["1", "0"]:
            the_query = the_query.filter(AuthorDB.valide == int_to_bool(valid))
        else:
            the_query = the_query.filter(AuthorDB.valide == True)

        if sort_by:
            the_query = the_query.order_by(sort_by)
        else:
            the_query = the_query.order_by("family_name")

        authors = [
            Author.from_db_to_data(author)
            for author in the_query.paginate(page=n_page, per_page=size).items
        ]
        logging.error(len(authors))
        return json_result(True, authors=authors), 200
    elif request.method == "POST":
        data = request.get_json()

        if dv.check_author(data):
            author_exists = AuthorDB.query.filter_by(
                first_name=data["first_name"],
                family_name=data["family_name"]).first()
            if not author_exists:
                author_db = Author.from_data_to_db(data)
                db.session.add(author_db)
                db.session.commit()
                return json_result(True, id=author_db.id), 201
            return json_result(True, id=author_exists.id), 200
        else:
            return json_result(False), 304
def book_reference(id_):
    if request.method == "GET":
        ref_livre_db = ReferenceBibliographiqueLivre.from_id_to_db(id_)
        if ref_livre_db:
            logging.warning(Author.get_authors_by_ref(id_, 1, 10, ""))
            logging.warning(Enregistrement.get_records_by_ref(id_, 1, 10, ""))
            ref_livre = ReferenceBibliographiqueLivre.from_db_to_data(
                ref_livre_db)
            if ref_livre:
                ref_livre["authors"] = [{
                    "text": f"{author_db.first_name} {author_db.family_name}",
                    "value": author_db.id
                } for author_db in ref_livre_db.authors]
                logging.debug(ref_livre)
                return json_result(True, reference=ref_livre), 200
            else:
                logging.error("reference void")
                return json_result(False), 404
        return json_result(False), 404
    elif request.method == "PUT":
        data = request.get_json()
        ref_biblio_db = ReferenceBibliographiqueLivre.from_id_to_db(id_)
        if ref_biblio_db:
            auteurs_db = []
            for auteur in data["auteurs"]:
                if "value" in auteur:
                    author_db = Author.from_id_to_db(auteur["value"])
                    if author_db:
                        auteurs_db.append(author_db)
            ref_biblio_db.authors = auteurs_db
            ref_biblio_db.titre = data["titre"]
            ref_biblio_db.lieu_edition = data["lieu_edition"]
            ref_biblio_db.editeur = data["editeur"]
            ref_biblio_db.annee = data["annee"]
            ref_biblio_db.nd_page = data["nb_page"]

            db.session.commit()

            return json_result(True), 200
        return json_result(False), 404
    elif request.method == "DELETE":
        ref_biblio_db = ReferenceBibliographiqueLivreDB.query.filter_by(
            id=id_).first()
        if ref_biblio_db:
            db.session.delete(ref_biblio_db)
            db.session.commit()
            return json_result(True), 204
        return json_result(False), 404
def get_reference_entries(id_):
    n_page, size, sort_by = get_pagination(request)
    entries = []
    authors = Author.get_authors_by_ref(id_, n_page, size, sort_by)
    records = Enregistrement.get_records_by_author(id_, n_page, size, sort_by)
    entries.extend(authors)
    entries.extend(records)
    return json_result(True, entries=entries), 200
def get_record_entries(id_):
    n_page, size, sort_by = get_pagination(request)
    entries = []
    authors = Author.get_authors_by_record(id_, n_page, size, sort_by)
    references = ReferenceBibliographiqueLivre.get_references_by_record(
        id_, n_page, size, sort_by)
    entries.extend(authors)
    entries.extend(references)
    return json_result(True, entries=entries), 200
Example #5
0
def import_complete_catalogue_from_file():
    for number, processed_row in enumerate(rows[1:]):
        if number % 100 == 0:
            print(number)
        # print(processed_row)
        description = processed_row["description"]
        # print(description)
        ref = icu.extraire_ref_biblio(description)
        if ref is None:
            print(number, "ref is None")
            continue
        if "authors" not in ref:
            print(number, "failed because authors not in ref")
            continue
        auteurs = []
        for author in ref["authors"]:
            # print(author)
            author_exists = AuthorDB.query.filter_by(first_name=author["first_name"],
                                                     family_name=author["family_name"]).first()
            if not author_exists:
                author_db = Author.from_data_to_db(author)
                db.session.add(author_db)
                db.session.commit()
                author_exists = author_db
            auteurs.append(dict(value=author_exists.id))
        # if dv.check_reference_bibliographique_livre(ref):
        ref["auteurs"] = auteurs
        # print(ref)
        reference_db = ReferenceBibliographiqueLivre.from_data_to_db(ref)
        # print(reference_db)

        record = icu.extraire_enregistrements(processed_row)
        if record is None:
            print("record is None")
            continue
        # print(record)
        # region reference
        db.session.add(reference_db)
        db.session.commit()
        # endregion
        # region record
        enregistrement_db = Enregistrement.from_data_to_db(record)
        # print(enregistrement_db)
        enregistrement_db.reference = reference_db
        db.session.add(enregistrement_db)
        db.session.commit()
        # endregion
        # store_new_stored_row(processed_row)
        # return json_result(True, data=processed_row, ref=ref, record=record, already_stored=already_stored), 200
    return json_result(True), 200
def chercher_auteurs():
    data = request.get_json()
    the_query = AuthorDB.query
    filtered = False
    if "first_name" in data and data["first_name"]:
        the_query = the_query.filter_by(first_name=data["first_name"])
        filtered = True
    if "family_name" in data and data["family_name"]:
        the_query = the_query.filter_by(family_name=data["family_name"])
        filtered = True
    if filtered:
        results = the_query.all()
    else:
        results = []
    results = [Author.from_db_to_data(author_db) for author_db in results]
    return json_result(True, results=results), 200
def author_(id_):
    """
    GET: read an author
    PUT: update an author
    DELETE: delete an author

    :param id_:
    :return:
    """

    if request.method == "GET":
        author_db = AuthorDB.query.filter_by(id=id_).first()
        author = Author.from_db_to_data(author_db)
        # logging.warning("ref")
        # logging.warning(ReferenceBibliographiqueLivre.get_references_by_author(id_, 1, 10, ""))
        # logging.warning("enregistrement")
        # logging.warning(Enregistrement.get_records_by_author(id_, 1, 10, ""))

        return json_result(True, author=author), 200
    elif request.method == "PUT":
        data = request.get_json()
        author = AuthorDB.query.filter_by(id=id_).first()
        if validation.check_author(data):
            author.first_name = data["first_name"]
            author.family_name = data["family_name"]
            db.session.commit()
            return json_result(True), 200
        else:
            return json_result(False), 400
    elif request.method == "DELETE":
        id_author = id_
        author_db = AuthorDB.query.filter_by(id=id_author).first()
        # exists ReferenceBibliographiqueLivreDB.query.filter_by(au)
        # TODO refuser si c'est utilisé dans des références
        if author_db:
            db.session.delete(author_db)
            db.session.commit()
            return json_result(True), 204
        return json_result(False), 400
def get_reference_count_entries(id_):
    author_count = Author.get_authors_by_ref_count(id_)
    record_count = Enregistrement.get_records_by_author_count(id_)
    total = author_count + record_count
    return json_result(True, total=total)
def get_record_count_entries(id_):
    author_count = Author.get_authors_by_record_count(id_)
    reference_count = ReferenceBibliographiqueLivre.get_references_by_record_count(
        id_)
    total = author_count + reference_count
    return json_result(True, total=total), 200