diff --git a/core/notion.py b/core/notion.py index fe493392f..7aa8aada8 100644 --- a/core/notion.py +++ b/core/notion.py @@ -2,18 +2,19 @@ import requests from django.conf import settings +from django.utils import timezone logger = logging.getLogger(__name__) -def create_new_row_in(database_id, data): - NOTION_TOKEN = settings.NOTION_TOKEN - if not NOTION_TOKEN: +def create_new_row_in_notion_table(database_id, data): + notion_token = settings.NOTION.get("TOKEN") + if not notion_token: logging.warning("The notion token is not set in local environment") return headers = { - "Authorization": f"Bearer {NOTION_TOKEN}", + "Authorization": f"Bearer {notion_token}", "Content-Type": "application/json", "Notion-Version": "2022-06-28", } @@ -21,9 +22,11 @@ def create_new_row_in(database_id, data): payload = { "parent": {"database_id": database_id}, "properties": { - "Name": {"title": [{"text": {"content": "Sample Row"}}]}, - "Status": {"select": {"name": "In Progress"}}, - "Date": {"date": {"start": "2025-01-15"}}, + "Nom": {"title": [{"text": {"content": data.get("name")}}]}, + "Email": {"email": data.get("email")}, + "Objet": {"rich_text": [{"text": {"content": data.get("subject")}}]}, + "Message": {"rich_text": [{"text": {"content": data.get("message")}}]}, + "Date": {"date": {"start": timezone.now().isoformat()}}, }, } @@ -32,6 +35,6 @@ def create_new_row_in(database_id, data): ) if response.status_code == 200: - logger.info("Row added successfully!") + logger.info("New contact form submission") else: logger.error(f"Failed to add row:{response.status_code=}, {response.text=}") diff --git a/qfdmd/views.py b/qfdmd/views.py index 8a1be554c..09788d31b 100644 --- a/qfdmd/views.py +++ b/qfdmd/views.py @@ -7,7 +7,7 @@ from django.urls import reverse_lazy from django.views.generic import DetailView, FormView, ListView -from core.notion import create_new_row_in +from core.notion import create_new_row_in_notion_table from qfdmd.forms import ContactForm, SearchForm from qfdmd.models import CMSPage, Suggestion, Synonyme @@ -47,8 +47,13 @@ class ContactFormView(FormView): success_url = reverse_lazy("qfdmd:nous-contacter-confirmation") def form_valid(self, form): - create_new_row_in( - settings.NOTION.get("CONTACT_FORM_DATABASE_ID"), form.cleaned_data + cleaned_data = form.cleaned_data + submitted_subject = cleaned_data.get("subject") + cleaned_data["subject"] = dict(self.form_class().fields["subject"].choices)[ + submitted_subject + ] + create_new_row_in_notion_table( + settings.NOTION.get("CONTACT_FORM_DATABASE_ID"), cleaned_data ) return super().form_valid(form) diff --git a/templates/forms/contact.html b/templates/forms/contact.html index 9efeed9e4..78d238b17 100644 --- a/templates/forms/contact.html +++ b/templates/forms/contact.html @@ -10,12 +10,13 @@ Envoyer mon message {% else %} -
+
-

+ + Votre message a bien été envoyé -

-
+ +
{% endif %}