Skip to content

Commit

Permalink
Prepare ntion calls
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienheureux committed Jan 15, 2025
1 parent 33330b7 commit a8ef448
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ STIMULUS_DEBUG=false
POSTHOG_DEBUG=false
ASSISTANT_MATOMO_ID=82
ASSISTANT_POSTHOG_KEY=phc_fSfhoWDOUxZdKWty16Z3XfRiAoWd1qdJK0N0z9kQHJr # [DEV] project
NOTION_TOKEN=
NOTION_CONTACT_FORM_DATABASE_ID=17c6523d57d78140b87f000cd3ecef4b # Correspond à https://www.notion.so/accelerateur-transition-ecologique-ademe/17c6523d57d7808b8cc5f5ccae264f7c?v=17c6523d57d78140b87f000cd3ecef4b&pvs=4
37 changes: 37 additions & 0 deletions core/notion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import logging

import requests
from django.conf import settings

logger = logging.getLogger(__name__)


def create_new_row_in(database_id, data):
NOTION_TOKEN = settings.NOTION_TOKEN
if not NOTION_TOKEN:
logging.warning("The notion token is not set in local environment")
return

headers = {
"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"}},
},
}

response = requests.post(
"https://api.notion.com/v1/pages", headers=headers, json=payload
)

if response.status_code == 200:
logger.info("Row added successfully!")
else:
logger.error(f"Failed to add row:{response.status_code=}, {response.text=}")
5 changes: 5 additions & 0 deletions core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,8 @@ def context_processors():
cast=str,
),
}

NOTION = {
"TOKEN": decouple.config("NOTION_TOKEN", default=""),
"CONTACT_FORM_DATABASE_ID": decouple.config("NOTION_CONTACT_FORM_DATABASE_ID"),
}
5 changes: 4 additions & 1 deletion qfdmd/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +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 qfdmd.forms import ContactForm, SearchForm
from qfdmd.models import CMSPage, Suggestion, Synonyme

Expand Down Expand Up @@ -46,7 +47,9 @@ class ContactFormView(FormView):
success_url = reverse_lazy("qfdmd:nous-contacter-confirmation")

def form_valid(self, form):
logger.error(f"{form=}")
create_new_row_in(
settings.NOTION.get("CONTACT_FORM_DATABASE_ID"), form.cleaned_data
)
return super().form_valid(form)


Expand Down

0 comments on commit a8ef448

Please sign in to comment.