Example #1
0
def _transfer_to_user_amendement_on_import(request: Request, lecture: Lecture,
                                           amendement: Amendement, item: dict,
                                           team: Team) -> None:
    email = User.normalize_email(item["affectation_email"])

    if not User.email_is_well_formed(email):
        logger.warning("Invalid email address %r", email)
        return

    user, created = get_one_or_create(User, email=email)
    if created:
        affectation_name = User.normalize_name(item["affectation_name"])
        user.name = affectation_name if affectation_name != "" else email
        user.teams.append(team)

    user_table, _ = get_one_or_create(UserTable, user=user, lecture=lecture)
    if amendement.location.user_table is user_table:
        return

    old = amendement.table_name_with_email
    new = str(user)

    amendement.location.user_table = user_table
    amendement.location.shared_table = None

    AmendementTransfere.create(amendement=amendement,
                               old_value=old,
                               new_value=new,
                               request=request)
Example #2
0
    def post(self) -> Response:
        """
        Transfer amendement(s) from this table to another one, or back to the index
        """
        nums: List[int] = self.request.POST.getall("nums")
        if "submit-index" in self.request.POST:
            target = ""
        elif "submit-table" in self.request.POST:
            target = self.request.user.email
        else:
            target = self.request.POST.get("target")
            if not target:
                self.request.session.flash(
                    Message(cls="warning",
                            text="Veuillez sélectionner un·e destinataire."))
                return HTTPFound(location=self.request.resource_url(
                    self.context.lecture_resource,
                    "transfer_amendements",
                    query={"nums": nums},
                ))

        target_table: Optional[UserTable] = None
        if target:
            if target == self.request.user.email:
                target_user = self.request.user
            else:
                target_user = DBSession.query(User).filter(
                    User.email == target).one()
            if self.request.team and target_user not in self.request.team.users:
                raise HTTPForbidden("Transfert non autorisé")
            target_table = target_user.table_for(self.lecture)

        amendements = DBSession.query(Amendement).filter(
            Amendement.lecture == self.lecture,
            Amendement.num.in_(nums)  # type: ignore
        )

        for amendement in amendements:
            old = str(
                amendement.user_table.user) if amendement.user_table else ""
            new = str(target_table.user) if target_table else ""
            if amendement.user_table is target_table:
                continue
            amendement.user_table = target_table
            amendement.stop_editing()
            AmendementTransfere.create(self.request, amendement, old, new)

        if target != self.request.user.email and self.request.POST.get(
                "from_index"):
            next_location = self.request.resource_url(
                self.context.lecture_resource, "amendements")
        else:
            next_location = self.request.resource_url(self.context.parent,
                                                      self.owner.email)
        return HTTPFound(location=next_location)
Example #3
0
 def update_sort(amendement: Amendement, sort: str) -> None:
     if sort != amendement.sort:
         if "irrecevable" in sort.lower():
             AmendementIrrecevable.create(amendement=amendement, sort=sort)
             # Put the amendement back to the index?
             if amendement.location.user_table is not None:
                 AmendementTransfere.create(
                     amendement=amendement,
                     old_value=str(amendement.location.user_table.user),
                     new_value="",
                 )
                 amendement.location.user_table = None
         else:
             amendement.sort = sort
Example #4
0
 def update_sort(self, amendement: Amendement, sort: str) -> bool:
     modified = False
     if sort != amendement.sort:
         if "irrecevable" in sort.lower():
             AmendementIrrecevable.create(request=None,
                                          amendement=amendement,
                                          sort=sort)
             # Put the amendement back to the index?
             if amendement.user_table is not None:
                 AmendementTransfere.create(
                     request=None,
                     amendement=amendement,
                     old_value=str(amendement.user_table.user),
                     new_value="",
                 )
                 amendement.user_table = None
         else:
             amendement.sort = sort
         modified = True
     return modified
Example #5
0
def _transfer_to_box_amendement_on_import(request: Request, lecture: Lecture,
                                          amendement: Amendement,
                                          item: dict) -> None:
    shared_table, created = get_one_or_create(SharedTable,
                                              titre=item["affectation_box"],
                                              lecture=lecture)

    if amendement.location.shared_table is shared_table:
        return

    old = amendement.table_name_with_email
    new = shared_table.titre

    amendement.location.shared_table = shared_table
    amendement.location.user_table = None

    AmendementTransfere.create(amendement=amendement,
                               old_value=old,
                               new_value=new,
                               request=request)
Example #6
0
def test_amendement_journal_affectation_released(app, lecture_an_url,
                                                 amendements_an, user_david):
    from zam_repondeur.models.events.amendement import AmendementTransfere

    with transaction.manager:
        AmendementTransfere.create(
            amendement=amendements_an[0],
            old_value=str(user_david),
            new_value="",
            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"] ==
                "David ([email protected])")
        assert amendements_an[0].events[0].data["new_value"] == ""

    resp = app.get(f"{lecture_an_url}/amendements/666/journal",
                   user=user_david)
    assert first_description_text(
        resp) == "David a remis l’amendement dans l’index."
Example #7
0
def test_amendement_journal_affectation_released(app, lecture_an,
                                                 amendements_an, user_david):
    from zam_repondeur.models.events.amendement import AmendementTransfere

    with transaction.manager:
        AmendementTransfere.create(
            request=None,
            amendement=amendements_an[0],
            old_value=str(user_david),
            new_value="",
            user=user_david,
        )
        assert len(amendements_an[0].events) == 1
        assert (amendements_an[0].events[0].data["old_value"] ==
                "David ([email protected])")
        assert amendements_an[0].events[0].data["new_value"] == ""

    resp = app.get("/lectures/an.15.269.PO717460/amendements/666/journal",
                   user=user_david)
    assert first_description_text(
        resp) == "David a remis l’amendement dans l’index"
Example #8
0
def test_amendement_journal_affectation_taken_by_other(app, lecture_an_url,
                                                       amendements_an,
                                                       user_david, user_ronan):
    from zam_repondeur.models.events.amendement import AmendementTransfere

    with transaction.manager:
        AmendementTransfere.create(
            amendement=amendements_an[0],
            old_value="",
            new_value=str(user_david),
            request=DummyRequest(remote_addr="127.0.0.1", user=user_ronan),
        )
        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"] ==
                "David ([email protected])")

    resp = app.get(f"{lecture_an_url}/amendements/666/journal",
                   user=user_david)
    assert (
        first_description_text(resp) ==
        "Ronan a transféré l’amendement à « David ([email protected]) ».")
Example #9
0
def transfer_amendement(
    request: Request, lecture: Lecture, amendement: Amendement, item: dict
) -> None:
    email = User.normalize_email(item["affectation_email"])

    if not User.validate_email(email):
        logging.warning("Invalid email address %r", email)
        return

    user, created = get_one_or_create(User, email=email)
    if created:
        affectation_name = User.normalize_name(item["affectation_name"])
        user.name = affectation_name if affectation_name != "" else email
        if lecture.owned_by_team:
            user.teams.append(lecture.owned_by_team)

    target_table = user.table_for(lecture)
    old = str(amendement.user_table.user) if amendement.user_table else ""
    new = str(target_table.user) if target_table else ""
    if amendement.user_table is target_table:
        return
    amendement.user_table = target_table
    AmendementTransfere.create(request, amendement, old, new)
Example #10
0
def test_amendement_journal_affectation_taken_by_other(app, lecture_an,
                                                       amendements_an,
                                                       user_david, user_ronan):
    from zam_repondeur.models.events.amendement import AmendementTransfere

    with transaction.manager:
        AmendementTransfere.create(
            request=None,
            amendement=amendements_an[0],
            old_value="",
            new_value=str(user_david),
            user=user_ronan,
        )
        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"] ==
                "David ([email protected])")

    resp = app.get("/lectures/an.15.269.PO717460/amendements/666/journal",
                   user=user_david)
    assert (
        first_description_text(resp) ==
        "Ronan a mis l’amendement sur la table de « David ([email protected]) »"
    )
Example #11
0
    def post(self) -> Response:
        """
        Transfer amendement(s) from this table to another one, or back to the index
        """
        nums: List[str] = self.get_nums()
        if "submit-index" in self.request.POST:
            target = ""
        elif "submit-table" in self.request.POST:
            target = self.request.user.email
        else:
            target = self.request.POST.get("target")
            if not target:
                self.request.session.flash(
                    Message(cls="warning",
                            text="Veuillez sélectionner un·e destinataire."))
                return HTTPFound(location=self.request.resource_url(
                    self.context.lecture_resource,
                    "transfer_amendements",
                    query={"n": nums},
                ))

        target_user_table = self.get_target_user_table(target)
        target_shared_table = self.get_target_shared_table(target)

        amendements = DBSession.query(Amendement).filter(
            Amendement.lecture == self.lecture,
            Amendement.num.in_(nums)  # type: ignore
        )

        for amendement in Batch.expanded_batches(amendements):
            old = amendement.table_name_with_email
            if target_shared_table:
                if target and amendement.location.shared_table is target_shared_table:
                    continue
                new = target_shared_table.titre
                amendement.location.shared_table = target_shared_table
                amendement.location.user_table = None
            else:
                if target and amendement.location.user_table is target_user_table:
                    continue
                new = str(target_user_table.user) if target_user_table else ""
                amendement.location.user_table = target_user_table
                amendement.location.shared_table = None
            amendement.stop_editing()
            AmendementTransfere.create(
                amendement=amendement,
                old_value=old,
                new_value=new,
                request=self.request,
            )

        if target != self.request.user.email and self.request.POST.get(
                "from_index"):
            amendements_collection = self.context.lecture_resource[
                "amendements"]
            next_location = self.request.resource_url(amendements_collection)
        else:
            table = self.context.model()
            table_resource = self.context.parent[table.user.email]
            next_location = self.request.resource_url(table_resource)
        return HTTPFound(location=next_location)