Exemple #1
0
    def post(self) -> Response:
        avis = self.request.POST.get("avis", "")
        objet = clean_html(self.request.POST.get("objet", ""))
        reponse = clean_html(self.request.POST.get("reponse", ""))
        comments = clean_html(self.request.POST.get("comments", ""))

        avis_changed = avis != (self.amendement.user_content.avis or "")
        objet_changed = objet != (self.amendement.user_content.objet or "")
        reponse_changed = reponse != (self.amendement.user_content.reponse or "")
        comments_changed = comments != (self.amendement.user_content.comments or "")

        if not self.is_on_my_table:
            message = (
                "Les modifications n’ont PAS été enregistrées "
                "car l’amendement n’est plus sur votre table."
            )
            if self.amendement.user_table:
                message += (
                    f" Il est actuellement sur la table de "
                    f"{self.amendement.user_table.user}."
                )
            self.request.session.flash(Message(cls="danger", text=message))
            return HTTPFound(location=self.my_table_url)

        if avis_changed:
            AvisAmendementModifie.create(self.request, self.amendement, avis)

        if objet_changed:
            ObjetAmendementModifie.create(self.request, self.amendement, objet)

        if reponse_changed:
            ReponseAmendementModifiee.create(self.request, self.amendement, reponse)

        if comments_changed:
            CommentsAmendementModifie.create(self.request, self.amendement, comments)

        self.amendement.stop_editing()

        self.request.session.flash(
            Message(cls="success", text="Les modifications ont bien été enregistrées.")
        )
        if "save-and-transfer" in self.request.POST:
            return HTTPFound(
                location=self.request.resource_url(
                    self.context.parent.parent,
                    "transfer_amendements",
                    query={
                        "nums": self.amendement.num,
                        "from_save": 1,
                        "back": self.back_url,
                    },
                )
            )
        else:
            self.request.session["highlighted_amdt"] = self.amendement.slug
            return HTTPFound(location=self.back_url)
def import_amendement(
    request: Request,
    lecture: Lecture,
    amendements: Dict[int, Amendement],
    item: dict,
    counter: Counter,
    previous_reponse: str,
) -> None:
    try:
        numero = item["num"]
        avis = item["avis"] or ""
        objet = item["objet"] or ""
        reponse = item["reponse"] or ""
    except KeyError:
        counter["reponses_errors"] += 1
        return

    try:
        num = normalize_num(numero)
    except ValueError:
        logging.warning("Invalid amendement number %r", numero)
        counter["reponses_errors"] += 1
        return

    amendement = amendements.get(num)
    if not amendement:
        logging.warning("Could not find amendement number %r", num)
        counter["reponses_errors"] += 1
        return

    avis = normalize_avis(avis)
    if avis != (amendement.user_content.avis or ""):
        AvisAmendementModifie.create(request, amendement, avis)

    objet = clean_html(objet)
    if objet != (amendement.user_content.objet or ""):
        ObjetAmendementModifie.create(request, amendement, objet)

    reponse = clean_html(normalize_reponse(reponse, previous_reponse))
    if reponse != (amendement.user_content.reponse or ""):
        ReponseAmendementModifiee.create(request, amendement, reponse)

    if "comments" in item:
        comments = clean_html(item["comments"])
        if comments != (amendement.user_content.comments or ""):
            CommentsAmendementModifie.create(request, amendement, comments)

    if "affectation_email" in item and item["affectation_email"]:
        transfer_amendement(request, lecture, amendement, item)

    previous_reponse = reponse
    counter["reponses"] += 1
Exemple #3
0
def test_amendement_journal_objet(app, lecture_an, amendements_an, user_david):
    from zam_repondeur.models.events.amendement import ObjetAmendementModifie

    with transaction.manager:
        ObjetAmendementModifie.create(request=None,
                                      amendement=amendements_an[0],
                                      objet="Objet",
                                      user=user_david)
        assert len(amendements_an[0].events) == 1
        assert amendements_an[0].events[0].data["old_value"] == ""
        assert amendements_an[0].events[0].data["new_value"] == "Objet"

    resp = app.get("/lectures/an.15.269.PO717460/amendements/666/journal",
                   user=user_david)
    assert first_summary_text(resp) == "David a ajouté l’objet"
    assert first_details_text(resp) == "Objet"
def test_amendement_journal_objet(app, lecture_an_url, amendements_an,
                                  user_david):
    from zam_repondeur.models.events.amendement import ObjetAmendementModifie

    with transaction.manager:
        ObjetAmendementModifie.create(
            amendement=amendements_an[0],
            objet="Objet",
            request=DummyRequest(remote_addr="127.0.0.1", user=user_david),
        )
        assert len(amendements_an[0].events) == 1
        assert amendements_an[0].events[0].data["old_value"] == ""
        assert amendements_an[0].events[0].data["new_value"] == "Objet"

    resp = app.get(f"{lecture_an_url}/amendements/666/journal",
                   user=user_david)
    assert first_summary_text(resp) == "David a ajouté l’objet."
    assert first_details_text(resp) == "Objet"
def test_amendement_journal_objet_clean(app, lecture_an_url, amendements_an,
                                        user_david):
    from zam_repondeur.models.events.amendement import ObjetAmendementModifie

    with transaction.manager:
        ObjetAmendementModifie.create(
            amendement=amendements_an[0],
            objet="<script>Objet</script>",
            request=DummyRequest(remote_addr="127.0.0.1", user=user_david),
        )
        assert len(amendements_an[0].events) == 1
        assert amendements_an[0].events[0].data["old_value"] == ""
        assert amendements_an[0].events[0].data[
            "new_value"] == "<script>Objet</script>"

    resp = app.get(f"{lecture_an_url}/amendements/666/journal",
                   user=user_david)
    assert first_summary_text(resp) == "David a ajouté l’objet."
    assert (resp.parser.css_first(".timeline li details p").html ==
            "<p><ins>Objet</ins> <del></del></p>")
Exemple #6
0
def import_amendement(
    request: Request,
    lecture: Lecture,
    amendements: Dict[int, Amendement],
    item: dict,
    counter: Counter,
    previous_reponse: str,
    team: Team,
) -> None:
    try:
        numero = item["num"]
        avis = item["avis"] or ""
        objet = item["objet"] or ""
        reponse = item["reponse"] or ""
    except KeyError:
        counter["reponses_errors"] += 1
        return

    try:
        num = normalize_num(numero)
    except ValueError:
        logging.warning("Invalid amendement number %r", numero)
        counter["reponses_errors"] += 1
        return

    amendement = amendements.get(num)
    if not amendement:
        logging.warning("Could not find amendement number %r", num)
        counter["reponses_errors"] += 1
        return

    avis = normalize_avis(avis)
    if avis != (amendement.user_content.avis or ""):
        AvisAmendementModifie.create(amendement=amendement,
                                     avis=avis,
                                     request=request)

    objet = clean_html(objet)
    if objet != (amendement.user_content.objet or ""):
        ObjetAmendementModifie.create(amendement=amendement,
                                      objet=objet,
                                      request=request)

    reponse = clean_html(normalize_reponse(reponse, previous_reponse))
    if reponse != (amendement.user_content.reponse or ""):
        ReponseAmendementModifiee.create(amendement=amendement,
                                         reponse=reponse,
                                         request=request)

    if "comments" in item:
        comments = clean_html(item["comments"])
        if comments != (amendement.user_content.comments or ""):
            CommentsAmendementModifie.create(amendement=amendement,
                                             comments=comments,
                                             request=request)

    # Order matters, if there is a box *and* an email, the amendement will be
    # transfered to the box then to the user who has precedence.
    if "affectation_box" in item and item["affectation_box"]:
        _transfer_to_box_amendement_on_import(request, lecture, amendement,
                                              item)

    if "affectation_email" in item and item["affectation_email"]:
        _transfer_to_user_amendement_on_import(request, lecture, amendement,
                                               item, team)

    previous_reponse = reponse
    counter["reponses"] += 1
    def post(self) -> Response:
        # Special case: unbatch (TODO: move to a separate route)
        if len(self.get_nums(self.request.POST)) == 1:
            amendement = self.get_amendements_from(self.request.POST)[0]
            BatchUnset.create(amendement=amendement, request=self.request)
            return HTTPFound(location=self.my_table_url)

        amendements = list(
            Batch.expanded_batches(self.get_amendements_from(
                self.request.POST)))

        self.check_amendements_are_all_on_my_table(amendements)
        self.check_amendements_have_all_same_reponse_or_empty(amendements)
        self.check_amendements_are_all_from_same_article(amendements)

        batch = Batch.create()
        shared_reponse: Optional[ReponseTuple] = None
        to_be_updated: List[Amendement] = []
        for amendement in amendements:
            if amendement.location.batch:
                BatchUnset.create(amendement=amendement, request=self.request)
            BatchSet.create(
                amendement=amendement,
                batch=batch,
                amendements_nums=[
                    amendement.num for amendement in amendements
                ],
                request=self.request,
            )
            reponse = amendement.user_content.as_tuple()
            if not reponse.is_empty:
                shared_reponse = reponse
            else:
                to_be_updated.append(amendement)

        if shared_reponse is not None and to_be_updated:
            for amendement in to_be_updated:
                if (amendement.user_content.avis or "") != shared_reponse.avis:
                    AvisAmendementModifie.create(
                        amendement=amendement,
                        avis=shared_reponse.avis,
                        request=self.request,
                    )
                if (amendement.user_content.objet
                        or "") != shared_reponse.objet:
                    ObjetAmendementModifie.create(
                        amendement=amendement,
                        objet=shared_reponse.objet,
                        request=self.request,
                    )
                if (amendement.user_content.reponse
                        or "") != shared_reponse.content:
                    ReponseAmendementModifiee.create(
                        amendement=amendement,
                        reponse=shared_reponse.content,
                        request=self.request,
                    )
                if (amendement.user_content.comments
                        or "") != shared_reponse.comments:
                    CommentsAmendementModifie.create(
                        amendement=amendement,
                        comments=shared_reponse.comments,
                        request=self.request,
                    )

        return HTTPFound(location=self.my_table_url)