Skip to content

Commit

Permalink
Fix Notion payload on form submission
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienheureux committed Jan 21, 2025
1 parent 2683328 commit 8684808
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
19 changes: 11 additions & 8 deletions core/notion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,31 @@

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",
}

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()}},
},
}

Expand All @@ -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=}")
11 changes: 8 additions & 3 deletions qfdmd/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down
9 changes: 5 additions & 4 deletions templates/forms/contact.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
Envoyer mon message
</button>
{% else %}
<div class="flex flex-col">
<div class="qf-flex qf-flex-row qf-gap-1w qf-items-center">
<span class="fr-icon--lg fr-icon fr-icon-success-fill" aria-hidden="true"></span>
<p>

<span>
Votre message a bien été envoyé
</p>
<div>
</span>
</div>
{% endif %}
</form>
</turbo-frame>

0 comments on commit 8684808

Please sign in to comment.