Skip to content

Commit

Permalink
Merge pull request #146 from ECE444-2023Fall/rahul-google-calendar-UI
Browse files Browse the repository at this point in the history
Added Google Calendar button and UI to event page
  • Loading branch information
rahuljchopra authored Nov 11, 2023
2 parents 887387f + 3f91736 commit b40532a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
23 changes: 13 additions & 10 deletions app/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
)
from flask_login import login_required, current_user
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google.oauth2.credentials import Credentials as AppCredentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
Expand All @@ -20,7 +20,7 @@
import os.path
import os

from app.main import db, es # db is for database
from app.main import db, es # db is for database and es is for elasticsearch
from app.globals import Role
from app.auth import organizer_required
from app.database import Credentials, EventRegistration, EventDetails, EventBanner, EventRating
Expand Down Expand Up @@ -419,12 +419,14 @@ def submit_rating(event_id):
return redirect(url_for('events.show_event', id=event_id))


@events.route("/events/<int:id>/calendar_invite", methods=["GET"])
@login_required
def create_google_calendar_event(id):
# Get the event details from the database
event = EventDetails.query.filter_by(id=id).all()
event = EventDetails.query.filter_by(id=id).first()

if not event:
print("Error! The event id passed to create_google_calendar_event() does not exit in the database")
logging.info("Error! The event id passed to create_google_calendar_event() does not exit in the database")

SCOPES = ['https://www.googleapis.com/auth/calendar']
creds = None
Expand All @@ -433,7 +435,7 @@ def create_google_calendar_event(id):
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
creds = AppCredentials.from_authorized_user_file('token.json', SCOPES)

# If there are no (valid) credentials available, let the user log in
if not creds or not creds.valid:
Expand All @@ -454,17 +456,18 @@ def create_google_calendar_event(id):
"location": event.venue,
"colorId": 6,
"start": {
"dateTime": f"{event.start_date}T{event.start_time}:00",
"dateTime": f"{event.start_date}T{event.start_time}",
"timeZone": "Canada/Eastern"
},
"end": {
"dateTime": f"{event.end_date}T{event.end_time}:00",
"dateTime": f"{event.end_date}T{event.end_time}",
"timeZone": "Canada/Eastern"
}
}

event = service.events().insert(calendarId="primary", body=event_data).execute()
print(f'Event created: {event.get("htmlLink")}')
event_calendar = service.events().insert(calendarId="primary", body=event_data).execute()
logging.info(f'Event created: {event_calendar.get("htmlLink")}')
return redirect(url_for("events.show_event", id=event.id))

except HttpError as error:
print("An error occurred:", error)
logging.error("An error occurred: %s", error)
22 changes: 15 additions & 7 deletions app/templates/event.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ <h1>{{event.name}}</h1>

<div class="event-section">
<h4 class="event-section-header">Date and Time</h4>
<p>Starts on: {{event.start_date}}</p>
<p>Ends on: {{event.end_date}}</p>
<p>Starts On: {{event.start_date}}</p>
<p>Ends On: {{event.end_date}}</p>
<p>From: {{event.start_time}} to {{event.end_time}} EST</p>
</div>

Expand All @@ -57,9 +57,9 @@ <h4 class="event-section-header">About this event</h4>
<div class="event-section">
<h4 class="event-section-header">Ticket Price</h4>
{% if event.ticket_price == 0.0 %}
<p>Tickets cost: Free</p>
<p>Tickets Cost: Free</p>
{% else %}
<p>Tickets cost (per person): {{event.ticket_price}} CAD</p>
<p>Tickets Cost (per person): {{event.ticket_price}} CAD</p>
{% endif %}
</div>

Expand All @@ -79,9 +79,14 @@ <h4 class="event-section-header">Additional Information</h4>
{% if event.redirect_link != "" %}
<button type="button" class="btn btn-light" href="{{event.redirect_link}}">Register</button>
{% else %}
<form action="/events/register/{{event.id}}" method="post">
<input type="submit" id="register_btn" class="btn btn-primary" value="Register"></input>
</form>
<div class="d-flex">
<form action="/events/register/{{event.id}}" method="post" class="me-3">
<input type="submit" id="register_btn" class="btn btn-primary btn-lg" value="Register">
</form>
<form action="/events/{{event.id}}/calendar_invite" method="get">
<input type="submit" id="add_calendar_btn" class="btn btn-primary btn-lg" value="Add to Google Calendar">
</form>
</div>
{% endif %}
</div>
</div>
Expand All @@ -93,6 +98,7 @@ <h4 class="event-section-header">Additional Information</h4>
//Disable this check to be able to test the ratings function easily.
if("{{is_past_event}}" == "True") {
document.getElementById('register_btn').disabled = true;
document.getElementById('add_calendar_btn').disabled = true;
}
else if("{{is_registered}}" == "True") {
console.log("JS: User is already registered");
Expand All @@ -101,6 +107,8 @@ <h4 class="event-section-header">Additional Information</h4>
}
else {
console.log("JS: User is not registered");
document.getElementById('add_calendar_btn').disabled = true;

}
</script>

Expand Down

0 comments on commit b40532a

Please sign in to comment.