From eb288fa6a1df32aa63e173fbb901b1e439dd05f8 Mon Sep 17 00:00:00 2001 From: Arthur ZHU Date: Fri, 15 Apr 2022 18:16:48 +0200 Subject: [PATCH] ajout champ formulaire contact, entreprise site --- app/entreprises/forms.py | 38 ++++++++++++++++++- app/entreprises/models.py | 12 ++++-- app/entreprises/routes.py | 38 ++++++++++++++++--- app/templates/entreprises/form.html | 27 +++++++------ ...53555_tables_module_gestion_relations_.py} | 35 +++++++++++------ 5 files changed, 114 insertions(+), 36 deletions(-) rename migrations/versions/{e5043b68e6b9_tables_module_gestion_relations_.py => 2a99f5553555_tables_module_gestion_relations_.py} (94%) diff --git a/app/entreprises/forms.py b/app/entreprises/forms.py index cff363b9..654b2618 100644 --- a/app/entreprises/forms.py +++ b/app/entreprises/forms.py @@ -398,21 +398,55 @@ class CorrespondantModificationForm(FlaskForm): class ContactCreationForm(FlaskForm): date = _build_string_field( - "Date", + "Date (*)", render_kw={"type": "datetime-local"}, ) + utilisateur = _build_string_field( + "Utilisateur (*)", + render_kw={"placeholder": "Tapez le nom de l'utilisateur"}, + ) notes = TextAreaField("Notes (*)", validators=[DataRequired(message=CHAMP_REQUIS)]) submit = SubmitField("Envoyer", render_kw=SUBMIT_MARGE) + def validate_utilisateur(self, utilisateur): + utilisateur_data = self.utilisateur.data.upper().strip() + stm = text( + "SELECT id, UPPER(CONCAT(nom, ' ', prenom, ' ', '(', user_name, ')')) FROM \"user\" WHERE UPPER(CONCAT(nom, ' ', prenom, ' ', '(', user_name, ')'))=:utilisateur_data" + ) + utilisateur = ( + User.query.from_statement(stm) + .params(utilisateur_data=utilisateur_data) + .first() + ) + if utilisateur is None: + raise ValidationError("Champ incorrect (selectionnez dans la liste)") + class ContactModificationForm(FlaskForm): date = _build_string_field( - "Date", + "Date (*)", render_kw={"type": "datetime-local"}, ) + utilisateur = _build_string_field( + "Utilisateur (*)", + render_kw={"placeholder": "Tapez le nom de l'utilisateur"}, + ) notes = TextAreaField("Notes (*)", validators=[DataRequired(message=CHAMP_REQUIS)]) submit = SubmitField("Modifier", render_kw=SUBMIT_MARGE) + def validate_utilisateur(self, utilisateur): + utilisateur_data = self.utilisateur.data.upper().strip() + stm = text( + "SELECT id, UPPER(CONCAT(nom, ' ', prenom, ' ', '(', user_name, ')')) FROM \"user\" WHERE UPPER(CONCAT(nom, ' ', prenom, ' ', '(', user_name, ')'))=:utilisateur_data" + ) + utilisateur = ( + User.query.from_statement(stm) + .params(utilisateur_data=utilisateur_data) + .first() + ) + if utilisateur is None: + raise ValidationError("Champ incorrect (selectionnez dans la liste)") + class StageApprentissageCreationForm(FlaskForm): etudiant = _build_string_field( diff --git a/app/entreprises/models.py b/app/entreprises/models.py index 74a4ccc4..949a05e7 100644 --- a/app/entreprises/models.py +++ b/app/entreprises/models.py @@ -35,10 +35,13 @@ class Entreprise(db.Model): } -# class EntrepriseSite(db.Model): -# __tablename__ = "are_sites" -# id = db.Column(db.Integer, primary_key=True) -# nom = db.Column(db.Text) +class EntrepriseSite(db.Model): + __tablename__ = "are_sites" + id = db.Column(db.Integer, primary_key=True) + entreprise_id = db.Column( + db.Integer, db.ForeignKey("are_entreprises.id", ondelete="cascade") + ) + nom = db.Column(db.Text) class EntrepriseCorrespondant(db.Model): @@ -47,6 +50,7 @@ class EntrepriseCorrespondant(db.Model): entreprise_id = db.Column( db.Integer, db.ForeignKey("are_entreprises.id", ondelete="cascade") ) + site_id = db.Column(db.Integer, db.ForeignKey("are_sites.id", ondelete="cascade")) nom = db.Column(db.Text) prenom = db.Column(db.Text) telephone = db.Column(db.Text) diff --git a/app/entreprises/routes.py b/app/entreprises/routes.py index 09ae715a..7fe2e727 100644 --- a/app/entreprises/routes.py +++ b/app/entreprises/routes.py @@ -805,17 +805,31 @@ def add_contact(id): entreprise = Entreprise.query.filter_by(id=id, visible=True).first_or_404( description=f"entreprise {id} inconnue" ) - form = ContactCreationForm() + form = ContactCreationForm( + date=f"{datetime.now().strftime('%Y-%m-%dT%H:%M')}", + utilisateur=f"{current_user.nom} {current_user.prenom} ({current_user.user_name})" + if current_user.nom and current_user.prenom + else "", + ) if form.validate_on_submit(): + utilisateur_data = form.utilisateur.data.upper().strip() + stm = text( + "SELECT id, UPPER(CONCAT(nom, ' ', prenom, ' ', '(', user_name, ')')) FROM \"user\" WHERE UPPER(CONCAT(nom, ' ', prenom, ' ', '(', user_name, ')'))=:utilisateur_data" + ) + utilisateur = ( + User.query.from_statement(stm) + .params(utilisateur_data=utilisateur_data) + .first() + ) contact = EntrepriseContact( date=form.date.data, - user=current_user.id, # a voir + user=utilisateur.id, entreprise=entreprise.id, notes=form.notes.data.strip(), ) db.session.add(contact) db.session.commit() - return redirect(url_for("entreprises.fiche_entreprise", id=entreprise.id)) + return redirect(url_for("entreprises.contacts", id=entreprise.id)) return render_template( "entreprises/form.html", title="Ajout contact", @@ -834,12 +848,26 @@ def edit_contact(id): ) form = ContactModificationForm() if form.validate_on_submit(): + utilisateur_data = form.utilisateur.data.upper().strip() + stm = text( + "SELECT id, UPPER(CONCAT(nom, ' ', prenom, ' ', '(', user_name, ')')) FROM \"user\" WHERE UPPER(CONCAT(nom, ' ', prenom, ' ', '(', user_name, ')'))=:utilisateur_data" + ) + utilisateur = ( + User.query.from_statement(stm) + .params(utilisateur_data=utilisateur_data) + .first() + ) contact.date = form.date.data + contact.user = utilisateur.id contact.notes = form.notes.data db.session.commit() - return redirect(url_for("entreprises.fiche_entreprise", id=contact.entreprise)) + return redirect(url_for("entreprises.contacts", id=contact.entreprise)) elif request.method == "GET": - form.date.data = contact.date + utilisateur = User.query.filter_by(id=contact.user).first() + form.date.data = contact.date.strftime("%Y-%m-%dT%H:%M") + form.utilisateur.data = ( + f"{utilisateur.nom} {utilisateur.prenom} ({utilisateur.user_name})" + ) form.notes.data = contact.notes return render_template( "entreprises/form.html", diff --git a/app/templates/entreprises/form.html b/app/templates/entreprises/form.html index 1b348eb2..fe51a97b 100644 --- a/app/templates/entreprises/form.html +++ b/app/templates/entreprises/form.html @@ -4,6 +4,8 @@ {% block styles %} {{super()}} + + {% endblock %} {% block app_content %} @@ -19,20 +21,6 @@ {% endblock %} \ No newline at end of file diff --git a/migrations/versions/e5043b68e6b9_tables_module_gestion_relations_.py b/migrations/versions/2a99f5553555_tables_module_gestion_relations_.py similarity index 94% rename from migrations/versions/e5043b68e6b9_tables_module_gestion_relations_.py rename to migrations/versions/2a99f5553555_tables_module_gestion_relations_.py index bc57fbf5..6a5ca0c7 100644 --- a/migrations/versions/e5043b68e6b9_tables_module_gestion_relations_.py +++ b/migrations/versions/2a99f5553555_tables_module_gestion_relations_.py @@ -1,8 +1,8 @@ """tables module gestion relations entreprises -Revision ID: e5043b68e6b9 +Revision ID: 2a99f5553555 Revises: b9aadc10227f -Create Date: 2022-04-04 09:14:54.605480 +Create Date: 2022-04-15 18:12:46.620653 """ from alembic import op @@ -10,7 +10,7 @@ import sqlalchemy as sa from sqlalchemy.dialects import postgresql # revision identifiers, used by Alembic. -revision = "e5043b68e6b9" +revision = "2a99f5553555" down_revision = "b9aadc10227f" branch_labels = None depends_on = None @@ -65,15 +65,10 @@ def upgrade(): sa.PrimaryKeyConstraint("id"), ) op.create_table( - "are_correspondants", + "are_sites", sa.Column("id", sa.Integer(), nullable=False), sa.Column("entreprise_id", sa.Integer(), nullable=True), sa.Column("nom", sa.Text(), nullable=True), - sa.Column("prenom", sa.Text(), nullable=True), - sa.Column("telephone", sa.Text(), nullable=True), - sa.Column("mail", sa.Text(), nullable=True), - sa.Column("poste", sa.Text(), nullable=True), - sa.Column("service", sa.Text(), nullable=True), sa.ForeignKeyConstraint( ["entreprise_id"], ["are_entreprises.id"], ondelete="cascade" ), @@ -95,6 +90,23 @@ def upgrade(): ), sa.PrimaryKeyConstraint("id"), ) + op.create_table( + "are_correspondants", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("entreprise_id", sa.Integer(), nullable=True), + sa.Column("site_id", sa.Integer(), nullable=True), + sa.Column("nom", sa.Text(), nullable=True), + sa.Column("prenom", sa.Text(), nullable=True), + sa.Column("telephone", sa.Text(), nullable=True), + sa.Column("mail", sa.Text(), nullable=True), + sa.Column("poste", sa.Text(), nullable=True), + sa.Column("service", sa.Text(), nullable=True), + sa.ForeignKeyConstraint( + ["entreprise_id"], ["are_entreprises.id"], ondelete="cascade" + ), + sa.ForeignKeyConstraint(["site_id"], ["are_sites.id"], ondelete="cascade"), + sa.PrimaryKeyConstraint("id"), + ) op.create_table( "are_offres", sa.Column("id", sa.Integer(), nullable=False), @@ -164,9 +176,9 @@ def upgrade(): sa.ForeignKeyConstraint(["offre_id"], ["are_offres.id"], ondelete="cascade"), sa.PrimaryKeyConstraint("id"), ) - op.drop_index("ix_entreprises_dept_id", table_name="entreprises") op.drop_table("entreprise_contact") op.drop_table("entreprise_correspondant") + op.drop_index("ix_entreprises_dept_id", table_name="entreprises") op.drop_table("entreprises") # ### end Alembic commands ### @@ -264,8 +276,9 @@ def downgrade(): op.drop_table("are_envoi_offre_etudiant") op.drop_table("are_envoi_offre") op.drop_table("are_offres") - op.drop_table("are_stages_apprentissages") op.drop_table("are_correspondants") + op.drop_table("are_stages_apprentissages") + op.drop_table("are_sites") op.drop_table("are_contacts") op.drop_table("are_preferences") op.drop_table("are_logs")