From 5f5c43ec089a23bdf6e7385331cd651e2a1b046b Mon Sep 17 00:00:00 2001 From: Arthur ZHU Date: Fri, 4 Mar 2022 17:10:07 +0100 Subject: [PATCH] associer un contact a une offre --- app/entreprises/app_relations_entreprises.py | 3 +- app/entreprises/forms.py | 34 +++++++++++++++++++ app/entreprises/models.py | 12 +++++++ app/entreprises/routes.py | 12 +++++-- app/templates/entreprises/_offre.html | 13 +++++++ app/templates/entreprises/offres_recues.html | 10 ++++++ ...74b7a_tables_module_gestion_relations_.py} | 14 +++++--- 7 files changed, 89 insertions(+), 9 deletions(-) rename migrations/versions/{af05f03b81be_tables_module_gestion_relations_.py => 40ece1f74b7a_tables_module_gestion_relations_.py} (97%) diff --git a/app/entreprises/app_relations_entreprises.py b/app/entreprises/app_relations_entreprises.py index 68ff99865..6b2a2e560 100644 --- a/app/entreprises/app_relations_entreprises.py +++ b/app/entreprises/app_relations_entreprises.py @@ -85,6 +85,7 @@ def get_offre_files_and_depts(offre: EntrepriseOffre, depts: list): Retourne l'offre, les fichiers attachés a l'offre et les département liés """ offre_depts = EntrepriseOffreDepartement.query.filter_by(offre_id=offre.id).all() + contact = EntrepriseContact.query.filter_by(id=offre.contact_id).first() if not offre_depts or check_offre_depts(depts, offre_depts): files = [] path = os.path.join( @@ -100,7 +101,7 @@ def get_offre_files_and_depts(offre: EntrepriseOffre, depts: list): for _file in glob.glob(f"{dir}/*"): file = [os.path.basename(dir), os.path.basename(_file)] files.append(file) - return [offre, files, offre_depts] + return [offre, files, offre_depts, contact] return None diff --git a/app/entreprises/forms.py b/app/entreprises/forms.py index 0540c0802..19719bcc5 100644 --- a/app/entreprises/forms.py +++ b/app/entreprises/forms.py @@ -144,6 +144,7 @@ class MultiCheckboxField(SelectMultipleField): class OffreCreationForm(FlaskForm): + hidden_entreprise_id = HiddenField() intitule = _build_string_field("Intitulé (*)") description = TextAreaField( "Description (*)", validators=[DataRequired(message=CHAMP_REQUIS)] @@ -159,17 +160,26 @@ class OffreCreationForm(FlaskForm): duree = _build_string_field("Durée (*)") depts = MultiCheckboxField("Départements", validators=[Optional()], coerce=int) expiration_date = DateField("Date expiration", validators=[Optional()]) + contact = SelectField("Contact à contacté (*)", validators=[Optional()]) submit = SubmitField("Envoyer", render_kw=SUBMIT_MARGE) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + self.contact.choices = [ + (contact.id, f"{contact.nom} {contact.prenom}") + for contact in EntrepriseContact.query.filter_by( + entreprise_id=self.hidden_entreprise_id.data + ) + ] + self.depts.choices = [ (dept.id, dept.acronym) for dept in Departement.query.all() ] class OffreModificationForm(FlaskForm): + hidden_entreprise_id = HiddenField() intitule = _build_string_field("Intitulé (*)") description = TextAreaField( "Description (*)", validators=[DataRequired(message=CHAMP_REQUIS)] @@ -185,11 +195,19 @@ class OffreModificationForm(FlaskForm): duree = _build_string_field("Durée (*)") depts = MultiCheckboxField("Départements", validators=[Optional()], coerce=int) expiration_date = DateField("Date expiration", validators=[Optional()]) + contact = SelectField("Contact à contacté (*)", validators=[Optional()]) submit = SubmitField("Modifier", render_kw=SUBMIT_MARGE) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + self.contact.choices = [ + (contact.id, f"{contact.nom} {contact.prenom}") + for contact in EntrepriseContact.query.filter_by( + entreprise_id=self.hidden_entreprise_id.data + ) + ] + self.depts.choices = [ (dept.id, dept.acronym) for dept in Departement.query.all() ] @@ -206,8 +224,16 @@ class ContactCreationForm(FlaskForm): ) poste = _build_string_field("Poste", required=False) service = _build_string_field("Service", required=False) + # depts = MultiCheckboxField("Départements", validators=[Optional()], coerce=int) submit = SubmitField("Envoyer", render_kw=SUBMIT_MARGE) + # def __init__(self, *args, **kwargs): + # super().__init__(*args, **kwargs) + + # self.depts.choices = [ + # (dept.id, dept.acronym) for dept in Departement.query.all() + # ] + def validate(self): validate = True if not FlaskForm.validate(self): @@ -245,8 +271,16 @@ class ContactModificationForm(FlaskForm): ) poste = _build_string_field("Poste", required=False) service = _build_string_field("Service", required=False) + # depts = MultiCheckboxField("Départements", validators=[Optional()], coerce=int) submit = SubmitField("Modifier", render_kw=SUBMIT_MARGE) + # def __init__(self, *args, **kwargs): + # super().__init__(*args, **kwargs) + + # self.depts.choices = [ + # (dept.id, dept.acronym) for dept in Departement.query.all() + # ] + def validate(self): validate = True if not FlaskForm.validate(self): diff --git a/app/entreprises/models.py b/app/entreprises/models.py index dd0b4ba44..b4c1e7ca8 100644 --- a/app/entreprises/models.py +++ b/app/entreprises/models.py @@ -75,6 +75,9 @@ class EntrepriseOffre(db.Model): duree = db.Column(db.Text) expiration_date = db.Column(db.Date) expired = db.Column(db.Boolean, default=False) + contact_id = db.Column( + db.Integer, db.ForeignKey("are_contacts.id", ondelete="cascade") + ) def to_dict(self): return { @@ -136,6 +139,15 @@ class EntrepriseOffreDepartement(db.Model): dept_id = db.Column(db.Integer, db.ForeignKey("departement.id", ondelete="cascade")) +# class EntrepriseContactDepartement(db.Model): +# __tablename__ = "are_contact_departement" +# id = db.Column(db.Integer, primary_key=True) +# contact_id = db.Column( +# db.Integer, db.ForeignKey("are_contacts.id", ondelete="cascade") +# ) +# dept_id = db.Column(db.Integer, db.ForeignKey("departement.id", ondelete="cascade")) + + class EntreprisePreferences(db.Model): __tablename__ = "are_preferences" id = db.Column(db.Integer, primary_key=True) diff --git a/app/entreprises/routes.py b/app/entreprises/routes.py index d1633e71d..281ad0470 100644 --- a/app/entreprises/routes.py +++ b/app/entreprises/routes.py @@ -221,6 +221,7 @@ def offres_recues(): ) offres_recues_with_files = [] for offre in offres_recues: + contact = EntrepriseContact.query.filter_by(id=offre[1].contact_id).first() files = [] path = os.path.join( Config.SCODOC_VAR_DIR, @@ -235,7 +236,7 @@ def offres_recues(): for file in glob.glob(f"{dir}/*"): file = [os.path.basename(dir), os.path.basename(file)] files.append(file) - offres_recues_with_files.append([offre[0], offre[1], files]) + offres_recues_with_files.append([offre[0], offre[1], files, contact]) return render_template( "entreprises/offres_recues.html", title="Offres reçues", @@ -491,7 +492,7 @@ def add_offre(id): entreprise = Entreprise.query.filter_by(id=id, visible=True).first_or_404( description=f"entreprise {id} inconnue" ) - form = OffreCreationForm() + form = OffreCreationForm(hidden_entreprise_id=id) if form.validate_on_submit(): offre = EntrepriseOffre( entreprise_id=entreprise.id, @@ -501,6 +502,7 @@ def add_offre(id): missions=form.missions.data.strip(), duree=form.duree.data.strip(), expiration_date=form.expiration_date.data, + contact_id=form.contact.data, ) db.session.add(offre) db.session.commit() @@ -537,15 +539,19 @@ def edit_offre(id): description=f"offre {id} inconnue" ) offre_depts = EntrepriseOffreDepartement.query.filter_by(offre_id=offre.id).all() - form = OffreModificationForm() + form = OffreModificationForm( + hidden_entreprise_id=offre.entreprise_id, contact=offre.contact_id + ) offre_depts_list = [(offre_dept.dept_id) for offre_dept in offre_depts] if form.validate_on_submit(): + print(form.contact.data) offre.intitule = form.intitule.data.strip() offre.description = form.description.data.strip() offre.type_offre = form.type_offre.data.strip() offre.missions = form.missions.data.strip() offre.duree = form.duree.data.strip() offre.expiration_date = form.expiration_date.data + offre.contact_id = form.contact.data if offre_depts_list != form.depts.data: for dept in form.depts.data: if dept not in offre_depts_list: diff --git a/app/templates/entreprises/_offre.html b/app/templates/entreprises/_offre.html index cff6f03ab..11391da36 100644 --- a/app/templates/entreprises/_offre.html +++ b/app/templates/entreprises/_offre.html @@ -9,6 +9,16 @@ {% if offre[2] %} Département(s) : {% for offre_dept in offre[2] %}
{{ offre_dept.dept_id|get_dept_acronym }}
{% endfor %}
{% endif %} + + {% if offre[0].contact_id %} + Contacté {{ offre[3].nom }} {{ offre[3].prenom }} + {% if offre[3].mail and offre[3].telephone %} + ({{ offre[3].mail }} - {{ offre[3].telephone }})
+ {% else %} + ({{ offre[3].mail }}{{offre[3].telephone}})
+ {% endif %} + {% endif %} + {% for fichier in offre[1] %} {{ fichier[1] }} {% if current_user.has_permission(current_user.Permission.RelationsEntreprisesChange, None) %} @@ -16,6 +26,7 @@ {% endif %}
{% endfor %} + {% if current_user.has_permission(current_user.Permission.RelationsEntreprisesChange, None) %} Ajoutez un fichier {% endif %} @@ -26,9 +37,11 @@ Modifier l'offre Supprimer l'offre {% endif %} + {% if current_user.has_permission(current_user.Permission.RelationsEntreprisesSend, None) %} Envoyer l'offre {% endif %} + {% if current_user.has_permission(current_user.Permission.RelationsEntreprisesChange, None) %} {% if not offre[0].expired %} Rendre expirée diff --git a/app/templates/entreprises/offres_recues.html b/app/templates/entreprises/offres_recues.html index a8814739f..e070ed505 100644 --- a/app/templates/entreprises/offres_recues.html +++ b/app/templates/entreprises/offres_recues.html @@ -16,6 +16,16 @@ Type de l'offre : {{ offre[1].type_offre }}
Missions : {{ offre[1].missions }}
Durée : {{ offre[1].duree }}
+ + {% if offre[1].contact_id %} + Contacté {{ offre[3].nom }} {{ offre[3].prenom }} + {% if offre[3].mail and offre[3].telephone %} + ({{ offre[3].mail }} - {{ offre[3].telephone }})
+ {% else %} + ({{ offre[3].mail }}{{offre[3].telephone}})
+ {% endif %} + {% endif %} + lien vers l'entreprise
{% for fichier in offre[2] %} diff --git a/migrations/versions/af05f03b81be_tables_module_gestion_relations_.py b/migrations/versions/40ece1f74b7a_tables_module_gestion_relations_.py similarity index 97% rename from migrations/versions/af05f03b81be_tables_module_gestion_relations_.py rename to migrations/versions/40ece1f74b7a_tables_module_gestion_relations_.py index 7f23ca9fc..3afa6a87b 100644 --- a/migrations/versions/af05f03b81be_tables_module_gestion_relations_.py +++ b/migrations/versions/40ece1f74b7a_tables_module_gestion_relations_.py @@ -1,8 +1,8 @@ """tables module gestion relations entreprises -Revision ID: af05f03b81be +Revision ID: 40ece1f74b7a Revises: b9aadc10227f -Create Date: 2022-03-01 17:12:32.927643 +Create Date: 2022-03-04 16:06:28.163870 """ from alembic import op @@ -10,7 +10,7 @@ import sqlalchemy as sa from sqlalchemy.dialects import postgresql # revision identifiers, used by Alembic. -revision = "af05f03b81be" +revision = "40ece1f74b7a" down_revision = "b9aadc10227f" branch_labels = None depends_on = None @@ -98,6 +98,10 @@ def upgrade(): sa.Column("duree", sa.Text(), nullable=True), sa.Column("expiration_date", sa.Date(), nullable=True), sa.Column("expired", sa.Boolean(), nullable=True), + sa.Column("contact_id", sa.Integer(), nullable=True), + sa.ForeignKeyConstraint( + ["contact_id"], ["are_contacts.id"], ondelete="cascade" + ), sa.ForeignKeyConstraint( ["entreprise_id"], ["are_entreprises.id"], ondelete="cascade" ), @@ -146,9 +150,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 ### @@ -190,7 +194,6 @@ def downgrade(): sa.PrimaryKeyConstraint("id", name="entreprises_pkey"), postgresql_ignore_search_path=False, ) - op.create_index("ix_entreprises_dept_id", "entreprises", ["dept_id"], unique=False) op.create_table( "entreprise_correspondant", sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False), @@ -242,6 +245,7 @@ def downgrade(): ), sa.PrimaryKeyConstraint("id", name="entreprise_contact_pkey"), ) + op.create_index("ix_entreprises_dept_id", "entreprises", ["dept_id"], unique=False) op.drop_table("are_offre_departement") op.drop_table("are_envoi_offre_etudiant") op.drop_table("are_envoi_offre")