generated from codersforcauses/django-nextjs-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Issue 9 configure google calendar integration #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Ninjawarrior69
merged 17 commits into
main
from
issue-9-Configure_google_calendar_integration
Dec 8, 2025
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a01d002
added google api python client package
Ninjawarrior69 108e818
added google api key template and CRUD for events
Ninjawarrior69 e60a240
Revert "added google api key template and CRUD for events"
Ninjawarrior69 cdf4d14
Redownload google api python client package and add google api .env t…
Ninjawarrior69 ad19067
added crud for events and test file
Ninjawarrior69 3f66aaf
poetry update
Ninjawarrior69 7e72f9d
poetry.lock new file
Ninjawarrior69 143a396
Updated the google calendar test file plus client.py. Works on person…
Ninjawarrior69 05f0bea
fix back code check issue and updated cleint.py
Ninjawarrior69 9a5edae
remove duplicate aws variables, added docs to events and removed depe…
Ninjawarrior69 9be90f6
fix google creds file error
Ninjawarrior69 9a27950
implement copilot changes
Ninjawarrior69 299df76
Merge branch 'main' into issue-9-Configure_google_calendar_integration
Ninjawarrior69 950a181
update poetry.lock
Ninjawarrior69 c10105b
change env variable names
Ninjawarrior69 4e0a17a
fix .json file name
Ninjawarrior69 ab847af
edit configuration
ErikaKK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -295,3 +295,6 @@ dist | |
| pyrightconfig.json | ||
|
|
||
| opt/ | ||
|
|
||
| # google api | ||
| server/api/booking/google_calendar/google_calendar_service.json | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import os | ||
| from pathlib import Path | ||
| from google.oauth2 import service_account | ||
| from googleapiclient.discovery import build | ||
| from dotenv import load_dotenv | ||
|
|
||
|
|
||
| # Resolve BASE_DIR and load .env | ||
| # adjust to server root | ||
| BASE_DIR = Path(__file__).resolve().parent.parent.parent.parent | ||
| load_dotenv(os.path.join(BASE_DIR, ".env")) | ||
|
|
||
|
|
||
| def get_calendar_service(): | ||
|
|
||
| cred_path = os.getenv("GOOGLE_CREDENTIALS_FILE") | ||
| if not cred_path: | ||
| raise ValueError( | ||
| "GOOGLE_CREDENTIALS_FILE is missing or invalid. " | ||
| f"Checked: {os.path.join(BASE_DIR, '.env')}" | ||
| ) | ||
|
|
||
| creds = service_account.Credentials.from_service_account_file( | ||
| cred_path, | ||
| scopes=["https://www.googleapis.com/auth/calendar"] | ||
| ) | ||
| return build("calendar", "v3", credentials=creds) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import os | ||
| from .client import get_calendar_service | ||
|
|
||
|
|
||
| def requires_calendar_id(func): | ||
| def wrapper(*args, **kwargs): | ||
| CALENDAR_ID = os.getenv("GOOGLE_CALENDAR_ID") | ||
| if not CALENDAR_ID: | ||
| raise ValueError( | ||
| "GOOGLE_CALENDAR_ID is missing. Set it in your environment." | ||
| ) | ||
| return func(CALENDAR_ID, *args, **kwargs) | ||
| return wrapper | ||
|
|
||
|
|
||
| @requires_calendar_id | ||
| def create_event(CALENDAR_ID, event_data: dict): | ||
| """ | ||
| Create a new Google Calendar event. | ||
|
|
||
| **Expected event_data keys (Google Calendar API format):** | ||
| - summary (str): Title of the event. | ||
| - description (str, optional): Details about the event. | ||
| - location (str, optional): Physical or virtual location. | ||
| - start (dict): Start date/time. | ||
| Example: | ||
| { | ||
| "dateTime": "2025-02-01T10:00:00+08:00", | ||
| "timeZone": "Australia/Perth" | ||
| } | ||
| - end (dict): End date/time. | ||
| Example: | ||
| { | ||
| "dateTime": "2025-02-01T11:00:00+08:00", | ||
| "timeZone": "Australia/Perth" | ||
| } | ||
| - 'recurrence': ['RRULE:FREQ=WEEKLY;COUNT=10']. | ||
| Returns: | ||
| dict: The created event object from Google Calendar. | ||
| """ | ||
| service = get_calendar_service() | ||
| return service.events().insert(calendarId=CALENDAR_ID, body=event_data).execute() | ||
|
|
||
|
|
||
| @requires_calendar_id | ||
| def get_event(CALENDAR_ID, event_id: str): | ||
| """ | ||
| Retrieve a specific event. | ||
|
|
||
| Args: | ||
| event_id (str): Google Calendar event ID. | ||
|
|
||
| Returns: | ||
| dict: Event details. | ||
| """ | ||
| service = get_calendar_service() | ||
| return service.events().get(calendarId=CALENDAR_ID, eventId=event_id).execute() | ||
|
|
||
|
|
||
| @requires_calendar_id | ||
| def update_event(CALENDAR_ID, event_id: str, updated_data: dict): | ||
| """ | ||
| Update an existing event. | ||
|
|
||
| Args: | ||
| event_id (str): Google Calendar event ID. | ||
| updated_data (dict): Same structure as event_data. | ||
|
|
||
| Returns: | ||
| dict: Updated event object. | ||
| """ | ||
| service = get_calendar_service() | ||
| return service.events().update(calendarId=CALENDAR_ID, eventId=event_id, body=updated_data).execute() | ||
|
|
||
|
|
||
| @requires_calendar_id | ||
| def delete_event(CALENDAR_ID, event_id: str): | ||
| """ | ||
| Delete an event. | ||
|
|
||
| Args: | ||
| event_id (str): Google Calendar event ID. | ||
|
|
||
| Returns: | ||
| dict: Response from Google Calendar API (usually empty). | ||
| """ | ||
| service = get_calendar_service() | ||
| return service.events().delete(calendarId=CALENDAR_ID, eventId=event_id).execute() |
13 changes: 13 additions & 0 deletions
13
server/api/booking/google_calendar/google_calendar_service.example.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "type": "service_account", | ||
| "project_id": "xxx", | ||
| "private_key_id": "xxx", | ||
| "private_key": "-----BEGIN PRIVATE KEY-----\nxxx\n-----END PRIVATE KEY-----\n", | ||
| "client_email": "xxx@xxx.iam.gserviceaccount.com", | ||
| "client_id": "xxx", | ||
| "auth_uri": "https://accounts.google.com/o/oauth2/auth", | ||
| "token_uri": "https://oauth2.googleapis.com/token", | ||
| "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | ||
| "client_x509_cert_url": "xxx", | ||
| "universe_domain": "googleapis.com" | ||
| } |
45 changes: 45 additions & 0 deletions
45
server/api/booking/google_calendar/test_google_calendar.py
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this test fails when you don't set the environment variables so need to fix it
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import pytest | ||
| import os | ||
| from api.booking.google_calendar.events import ( | ||
| create_event, | ||
| get_event, | ||
| update_event, | ||
| delete_event, | ||
| ) | ||
| # skip if no Google Calendar env variables are set | ||
| pytestmark = pytest.mark.skipif( | ||
| not os.getenv("GOOGLE_CREDENTIALS_FILE") or not os.getenv( | ||
| "GOOGLE_CALENDAR_ID"), | ||
| reason="Google Calendar integration env vars missing. Set GOOGLE_CREDENTIALS_FILE and GOOGLE_CALENDAR_ID to run this test." | ||
| ) | ||
|
|
||
|
|
||
| def test_google_calendar_crud(): | ||
|
|
||
| event_data = { | ||
| "summary": "Test Event", | ||
| "description": "CRUD test event", | ||
| "start": { | ||
| "dateTime": "2025-12-02T15:00:00", | ||
| "timeZone": "Australia/Perth", | ||
| }, | ||
| "end": { | ||
| "dateTime": "2025-12-02T16:00:00", | ||
| "timeZone": "Australia/Perth", | ||
| }, | ||
| 'recurrence': ['RRULE:FREQ=WEEKLY;COUNT=10'], | ||
| } | ||
|
|
||
| created = create_event(event_data) | ||
| event_id = created["id"] | ||
|
|
||
| fetched = get_event(event_id) | ||
| assert fetched["summary"] == "Test Event" | ||
|
|
||
| updated_data = dict(event_data) | ||
| updated_data["summary"] = "Updated Test Event" | ||
|
|
||
| updated = update_event(event_id, updated_data) | ||
| assert updated["summary"] == "Updated Test Event" | ||
|
|
||
| delete_event(event_id) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary blank line added at the end of the Google API section.