Ejemplo n.º 1
0
 def __init__(self, context: AmendementResource, request: Request) -> None:
     self.context = context
     self.request = request
     self.amendement = context.model()
     self.amendements = list(Batch.expanded_batches([self.amendement]))
     self.lecture = self.amendement.lecture
     self.my_table_resource = self.context.lecture_resource["tables"][
         self.request.user.email]
     self.is_on_my_table = (self.amendement.location.user_table
                            and self.amendement.location.user_table.user
                            == self.request.user)
Ejemplo n.º 2
0
def export_pdf(context: LectureResource, request: Request) -> Response:
    lecture = context.model(
        noload("amendements"),
        DOSSIER_OPTIONS,
        subqueryload("articles").options(joinedload("user_content")),
    )
    nums, article_param = parse_params(request, lecture=lecture)
    if article_param == "all":
        article_amendements = (
            DBSession.query(Amendement)
            .join(Article)
            .filter(Amendement.lecture == lecture,)
            .options(USER_CONTENT_OPTIONS, LOCATION_OPTIONS)
        )
    else:
        article_type, article_num, article_mult, article_pos = article_param.split(".")
        article_amendements = (
            DBSession.query(Amendement)
            .filter(
                Article.pk == Amendement.article_pk,
                Amendement.lecture == lecture,
                Article.type == article_type,
                Article.num == article_num,
                Article.mult == article_mult,
                Article.pos == article_pos,
            )
            .options(USER_CONTENT_OPTIONS, LOCATION_OPTIONS,)
        )

    amendements = [
        amendement for amendement in article_amendements if amendement.num in nums
    ]
    expanded_amendements = list(Batch.expanded_batches(amendements))

    with NamedTemporaryFile() as file_:
        tmp_file_path = os.path.abspath(file_.name)
        write_pdf_multiple(
            lecture=lecture,
            amendements=amendements,
            article_amendements=AmendementList(article_amendements),
            filename=tmp_file_path,
            request=request,
        )
        return write_response(
            tmp_file_path=tmp_file_path,
            fmt="pdf",
            lecture=lecture,
            article_param=article_param,
            amendements=expanded_amendements,
        )
Ejemplo n.º 3
0
def export_xlsx(context: LectureResource, request: Request) -> Response:
    lecture = context.model(noload("amendements"))
    nums, article_param = parse_params(request, lecture=lecture)
    if article_param == "all":
        amendements = (
            DBSession.query(Amendement)
            .join(Article)
            .filter(
                Amendement.lecture == lecture, Amendement.num.in_(nums),  # type: ignore
            )
            .options(USER_CONTENT_OPTIONS, LOCATION_OPTIONS)
        )
    else:
        article_type, article_num, article_mult, article_pos = article_param.split(".")
        amendements = (
            DBSession.query(Amendement)
            .filter(
                Article.pk == Amendement.article_pk,
                Amendement.lecture == lecture,
                Article.type == article_type,
                Article.num == article_num,
                Article.mult == article_mult,
                Article.pos == article_pos,
                Amendement.num.in_(nums),  # type: ignore
            )
            .options(USER_CONTENT_OPTIONS, LOCATION_OPTIONS)
        )

    expanded_amendements = list(Batch.expanded_batches(amendements))

    with NamedTemporaryFile() as file_:
        tmp_file_path = os.path.abspath(file_.name)
        write_xlsx(lecture, tmp_file_path, request, amendements=expanded_amendements)
        return write_response(
            tmp_file_path=tmp_file_path,
            fmt="xlsx",
            lecture=lecture,
            article_param=article_param,
            amendements=expanded_amendements,
        )
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
def stop_editing(context: AmendementResource, request: Request) -> dict:
    for amendement in Batch.expanded_batches([context.model()]):
        amendement.stop_editing()
    return {}
Ejemplo n.º 6
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)