def test_import_same_liasse_xml_again_preserve_response(
        lecture_essoc2018_an_nouvelle_lecture_commission_fond):
    from zam_repondeur.fetch.an.liasse_xml import import_liasse_xml

    # Let's import amendements
    amendements, _ = import_liasse_xml(
        open_liasse("liasse.xml"),
        lecture_essoc2018_an_nouvelle_lecture_commission_fond)

    # Now let's add a response
    amendements[1].user_content.avis = "Favorable"
    amendements[1].user_content.objet = "Objet"
    amendements[1].user_content.reponse = "Réponse"

    # And import the same amendements again
    amendements2, errors = import_liasse_xml(
        open_liasse("liasse.xml"),
        lecture_essoc2018_an_nouvelle_lecture_commission_fond)

    assert amendements == amendements2
    assert amendements2[1].user_content.avis == "Favorable"
    assert amendements2[1].user_content.objet == "Objet"
    assert amendements2[1].user_content.reponse == "Réponse"

    assert errors == []
def test_remove_parent_amendement(
        lecture_essoc2018_an_nouvelle_lecture_commission_fond):
    from zam_repondeur.fetch.an.liasse_xml import import_liasse_xml

    # Let's import amendements with a parent
    amendements, _ = import_liasse_xml(
        open_liasse("liasse.xml"),
        lecture_essoc2018_an_nouvelle_lecture_commission_fond)
    assert amendements[1].parent.num == 28

    # And import a liasse without the parent
    amendements2, errors = import_liasse_xml(
        open_liasse("liasse_removed_parent.xml"),
        lecture_essoc2018_an_nouvelle_lecture_commission_fond,
    )
    assert amendements[1].parent == amendements2[1].parent is None
    assert errors == []
def test_article_changed(
        lecture_essoc2018_an_nouvelle_lecture_commission_fond):
    from zam_repondeur.fetch.an.liasse_xml import import_liasse_xml

    # Let's import amendements
    amendements, _ = import_liasse_xml(
        open_liasse("liasse.xml"),
        lecture_essoc2018_an_nouvelle_lecture_commission_fond)
    assert amendements[0].article.num == "2"

    # And import a liasse with a different target article
    amendements2, errors = import_liasse_xml(
        open_liasse("liasse_modified_article.xml"),
        lecture_essoc2018_an_nouvelle_lecture_commission_fond,
    )
    assert amendements[0].article.num == amendements2[0].article.num == "3"
    assert errors == []
Example #4
0
def _do_upload_liasse_xml(context: LectureResource,
                          request: Request) -> Response:
    try:
        liasse_field = request.POST["liasse"]
    except KeyError:
        request.session.flash(
            ("warning", "Veuillez d’abord sélectionner un fichier"))
        return

    if liasse_field == b"":
        request.session.flash(
            ("warning", "Veuillez d’abord sélectionner un fichier"))
        return

    try:
        amendements = import_liasse_xml(liasse_field.file)
    except ValueError:
        request.session.flash(
            ("danger", "Le format du fichier n’est pas valide."))
        return

    if len(amendements) == 0:
        request.session.flash(
            ("warning", "Aucun amendement n’a été trouvé dans ce fichier."))
        return

    lecture = context.model()
    filtered_amendements = [
        amendement for amendement in amendements
        if amendement.chambre == lecture.chambre and amendement.session ==
        lecture.session and amendement.num_texte == lecture.num_texte
        and amendement.organe == lecture.organe
    ]
    ignored = len(amendements) - len(filtered_amendements)

    if len(filtered_amendements) == 0:
        amendement = amendements[0]
        other_lecture = Lecture(
            chambre=amendement.chambre,
            session=amendement.session,
            num_texte=amendement.num_texte,
            organe=amendement.organe,
        )
        request.session.flash(
            ("danger",
             f"La liasse correspond à une autre lecture ({other_lecture})."))
        return

    if ignored > 0:
        request.session.flash(
            ("warning",
             f"{ignored} amendements ignorés car non liés à cette lecture."))

    added, updated, unchanged = _add_or_update_amendements(
        filtered_amendements)
    assert added + updated + unchanged == len(filtered_amendements)
    _set_flash_messages(request, added, updated, unchanged)
Example #5
0
def test_import_liasse_xml():
    from zam_repondeur.fetch.an.liasse_xml import import_liasse_xml
    from zam_repondeur.fetch.models import Amendement

    amendements = import_liasse_xml(SAMPLE_LIASSE.open(mode="rb"))

    assert isinstance(amendements, list)
    assert len(amendements) == 2
    assert all(isinstance(item, Amendement) for item in amendements)

    _check_amendement_0(amendements[0])
    _check_amendement_1(amendements[1])
def test_import_liasse_xml_article_additionnel(
        lecture_essoc2018_an_nouvelle_lecture_commission_fond):
    from zam_repondeur.fetch.an.liasse_xml import import_liasse_xml

    amendements, errors = import_liasse_xml(
        open_liasse("liasse_apres.xml"),
        lecture_essoc2018_an_nouvelle_lecture_commission_fond,
    )

    assert amendements[0].article.num == "2"
    assert amendements[0].article.pos == "après"

    assert errors == []
def test_import_liasse_xml_with_known_but_missing_parent(
        lecture_essoc2018_an_nouvelle_lecture_commission_fond):
    from zam_repondeur.fetch.an.liasse_xml import import_liasse_xml

    # Let's import the parent amendement
    amendements, errors = import_liasse_xml(
        open_liasse("liasse_only_parent.xml"),
        lecture_essoc2018_an_nouvelle_lecture_commission_fond,
    )
    assert len(amendements) == 1
    assert amendements[0].num == 28
    assert amendements[0].parent is None
    assert errors == []

    # Now let's import a liasse with only a child amendement
    amendements, errors = import_liasse_xml(
        open_liasse("liasse_only_child.xml"),
        lecture_essoc2018_an_nouvelle_lecture_commission_fond,
    )
    assert len(amendements) == 1
    assert amendements[0].num == 26
    assert amendements[0].parent.num == 28
    assert errors == []
def test_import_liasse_second_part(
        app, dossier_essoc2018,
        texte_essoc2018_an_nouvelle_lecture_commission_fond):
    from zam_repondeur.fetch.an.liasse_xml import import_liasse_xml, LectureDoesNotMatch
    from zam_repondeur.models import DBSession, Lecture

    with transaction.manager:
        part1 = Lecture.create(
            chambre="an",
            session="15",
            texte=texte_essoc2018_an_nouvelle_lecture_commission_fond,
            partie=1,
            titre="Nouvelle lecture – Titre lecture",
            organe="PO744107",
            dossier=dossier_essoc2018,
        )
        part2 = Lecture.create(
            chambre="an",
            session="15",
            texte=texte_essoc2018_an_nouvelle_lecture_commission_fond,
            partie=2,
            titre="Nouvelle lecture – Titre lecture",
            organe="PO744107",
            dossier=dossier_essoc2018,
        )

    DBSession.add(part2)

    with pytest.raises(LectureDoesNotMatch):
        import_liasse_xml(open_liasse("liasse_second_part.xml"), part1)

    amendements, errors = import_liasse_xml(
        open_liasse("liasse_second_part.xml"), part2)

    assert (len(amendements), len(errors)) == (3, 0)
    for amendement in amendements:
        assert amendement.lecture == part2
def test_import_smaller_liasse_xml_preserves_responses(
        lecture_essoc2018_an_nouvelle_lecture_commission_fond):
    from zam_repondeur.fetch.an.liasse_xml import import_liasse_xml
    from zam_repondeur.models import Amendement, DBSession

    # Let's import amendements
    amendements, _ = import_liasse_xml(
        open_liasse("liasse.xml"),
        lecture_essoc2018_an_nouvelle_lecture_commission_fond)

    # Now let's add a response
    amendements[1].user_content.avis = "Favorable"
    amendements[1].user_content.objet = "Objet"
    amendements[1].user_content.reponse = "Réponse"

    # Even for the one not being reimported
    amendements[2].user_content.avis = "Défavorable"
    amendements[2].user_content.objet = "Objet"
    amendements[2].user_content.reponse = "Réponse"

    # And import a smaller liasse
    amendements2, errors = import_liasse_xml(
        open_liasse("liasse_smaller.xml"),
        lecture_essoc2018_an_nouvelle_lecture_commission_fond,
    )

    assert amendements[0] == amendements2[0]
    assert amendements[1] == amendements2[1]
    assert len(amendements2) == 2
    assert amendements2[1].user_content.avis == "Favorable"
    assert amendements2[1].user_content.objet == "Objet"
    assert amendements2[1].user_content.reponse == "Réponse"
    assert DBSession.query(Amendement).count() == 3
    assert amendements[2].user_content.avis == "Défavorable"

    assert errors == []
def test_import_liasse_xml_with_unknown_parent(
        lecture_essoc2018_an_nouvelle_lecture_commission_fond):
    from zam_repondeur.fetch.an.liasse_xml import import_liasse_xml

    # Let's import a liasse with only a child amendement
    amendements, errors = import_liasse_xml(
        open_liasse("liasse_only_child.xml"),
        lecture_essoc2018_an_nouvelle_lecture_commission_fond,
    )

    assert amendements == []
    assert len(errors) == 1
    uid, cause = errors[0]
    assert uid == "AMANR5L15PO744107B0806P0D1N26"
    assert cause == "Unknown parent amendement 28"
def test_import_liasse_xml(
        lecture_essoc2018_an_nouvelle_lecture_commission_fond):
    from zam_repondeur.fetch.an.liasse_xml import import_liasse_xml
    from zam_repondeur.models import Amendement

    amendements, errors = import_liasse_xml(
        open_liasse("liasse.xml"),
        lecture_essoc2018_an_nouvelle_lecture_commission_fond)

    assert isinstance(amendements, list)
    assert len(amendements) == 3
    assert all(isinstance(item, Amendement) for item in amendements)

    _check_amendement_0(amendements[0])
    _check_amendement_1(amendements[1])
    _check_amendement_gouvernemental(amendements[2])

    assert errors == []
Example #12
0
def upload_liasse_xml(context: LectureResource, request: Request) -> Response:
    try:
        liasse_field = request.POST["liasse"]
    except KeyError:
        request.session.flash(
            Message(cls="warning",
                    text="Veuillez d’abord sélectionner un fichier"))
        return HTTPFound(location=request.resource_url(context, "options"))

    if liasse_field == b"":
        request.session.flash(
            Message(cls="warning",
                    text="Veuillez d’abord sélectionner un fichier"))
        return HTTPFound(location=request.resource_url(context, "options"))

    # Backup uploaded file to make troubleshooting easier
    backup_path = get_backup_path(request)
    if backup_path is not None:
        save_uploaded_file(liasse_field, backup_path)

    lecture = context.model()

    try:
        amendements, errors = import_liasse_xml(liasse_field.file, lecture)
    except ValueError:
        logger.exception("Erreur d'import de la liasse XML")
        request.session.flash(
            Message(cls="danger",
                    text="Le format du fichier n’est pas valide."))
        return HTTPFound(location=request.resource_url(context, "options"))
    except LectureDoesNotMatch as exc:
        request.session.flash(
            Message(
                cls="danger",
                text=
                f"La liasse correspond à une autre lecture ({exc.lecture_fmt}).",
            ))
        return HTTPFound(location=request.resource_url(context, "options"))

    if errors:
        if len(errors) == 1:
            what = "l'amendement"
        else:
            what = "les amendements"
        uids = ", ".join(uid for uid, cause in errors)
        request.session.flash(
            Message(cls="warning",
                    text=f"Impossible d'importer {what} {uids}."))

    if len(amendements) == 0:
        request.session.flash(
            Message(
                cls="warning",
                text="Aucun amendement valide n’a été trouvé dans ce fichier.",
            ))
        return HTTPFound(location=request.resource_url(context, "options"))

    if len(amendements) == 1:
        message = "1 nouvel amendement récupéré (import liasse XML)."
    else:
        message = (
            f"{len(amendements)} nouveaux amendements récupérés (import liasse XML)."
        )
    request.session.flash(Message(cls="success", text=message))
    AmendementsRecuperesLiasse.create(request, lecture, count=len(amendements))
    DBSession.add(lecture)
    return HTTPFound(location=request.resource_url(context, "amendements"))
def test_import_liasse_xml_lecture_does_not_match(lecture_an):
    from zam_repondeur.fetch.an.liasse_xml import import_liasse_xml, LectureDoesNotMatch

    with pytest.raises(LectureDoesNotMatch):
        import_liasse_xml(open_liasse("liasse.xml"), lecture_an)
def test_import_liasse_xml_lecture_is_not_an(lecture_senat):
    from zam_repondeur.fetch.an.liasse_xml import import_liasse_xml, BadChambre

    with pytest.raises(BadChambre):
        import_liasse_xml(open_liasse("liasse.xml"), lecture_senat)