Skip to content

Commit

Permalink
Create a google-meet event
Browse files Browse the repository at this point in the history
  • Loading branch information
walosha committed Jan 26, 2023
1 parent 8e3217c commit fad501b
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ env.bak/
venv.bak/
photos/
chapters.txt
media/
media/
church-375900-fbd978c352ca.json
8 changes: 8 additions & 0 deletions event/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ class Meta:
'owner': {'read_only': True},
}




class CalenderSerializer(serializers.Serializer):

eventTitle = serializers.CharField(label="event", max_length=255, required=True)
startDateTime = serializers.DateTimeField(label="startDateTime", input_formats=['%Y/%m/%d %H:%M'], required=True)
endDateTime = serializers.DateTimeField(label="endDateTime", input_formats=['%Y/%m/%d %H:%M'], required=True)
3 changes: 2 additions & 1 deletion event/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.urls import path, include
from .views import EventListCreateAPIView, EventRetrieveAPIView, EventDestroyAPIView, EventUpdateAPIView
from .views import EventListCreateAPIView, EventRetrieveAPIView, EventDestroyAPIView, EventUpdateAPIView,CalenderCreateAPIView

urlpatterns = [
path('<uuid:pk>/', EventRetrieveAPIView.as_view(),
name="event_detail"),
path('', EventListCreateAPIView.as_view(), name="event_list"),
path('calender/', CalenderCreateAPIView.as_view(), name="calender_create"),
path('<uuid:pk>/', EventUpdateAPIView.as_view(), name="event_update"),
path('<uuid:pk>/', EventDestroyAPIView.as_view(),
name="event_delete"),
Expand Down
100 changes: 98 additions & 2 deletions event/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
from rest_framework import generics
from .serializers import EventSerializer
import os
from .models import Event
from rest_framework import generics,response,status
from .serializers import EventSerializer,CalenderSerializer
from googleapiclient.discovery import build
from google.oauth2 import service_account
from datetime import datetime
import pytz,uuid




SCOPES = ["https://www.googleapis.com/auth/calendar"]
service_account_email = os.getenv('GOOGLE_SERVICE_ACCOUNT')
credentials = service_account.Credentials.from_service_account_file('church-375900-fbd978c352ca.json')
scoped_credentials = credentials.with_scopes(SCOPES)
calendarId = os.getenv('CALENDER_ID')

class EventListCreateAPIView (generics.ListCreateAPIView):
queryset = Event.objects.all()
serializer_class = EventSerializer
Expand All @@ -27,3 +40,86 @@ class EventDestroyAPIView (generics.DestroyAPIView):
class EventUpdateAPIView (generics.UpdateAPIView):
queryset = Event.objects.all()
serializer_class = EventSerializer





def build_service(request):

service = build("calendar", "v3", credentials=scoped_credentials)
return service

class CalenderCreateAPIView (generics.CreateAPIView):
serializer_class = CalenderSerializer


def post(self, request, *args, **kwargs):
request = request.data
if self.request:
return self.form_valid(request)
else:
return self.form_invalid(request)


def form_valid(self, form):

eventTitle = form.get("eventTitle")
start_date_data = form.get("startDateTime")
end_date_data = form.get("endDateTime")

if start_date_data > end_date_data:
return response.Response({"mesage": 'Please enter the correct period.'})
service = build_service(self.request)
print(datetime.now(pytz.timezone('US/Central')).isoformat())
event = {
"summary": eventTitle,
"start": {"dateTime": datetime.now(pytz.timezone('US/Central')).isoformat()},
"end": {"dateTime": datetime.now(pytz.timezone('US/Central')).isoformat()},
# "attendees": [{"email": "walosha@gmail.com"}],
"conferenceData": {"createRequest": {"requestId": f"{uuid.uuid4().hex}"}},
"description": eventTitle,
"reminders": {"useDefault": True}
}

result = service.events().insert(calendarId=calendarId, body=event).execute()
print(result)
return response.responses({"data":result.get('organizer'),"status":status.HTTP_201_CREATED})

# def get_success_url(self):

# messages.add_message(self.request, messages.INFO, 'Form submission success!!')

# return reverse('cal:home')

# def get_context_data(self, **kwargs):
# context = super(HomeView, self).get_context_data(**kwargs)
# form = BookingForm()
# booking_event = []
# service = build_service(self.request)
# events = (
# service.events().list(
# calendarId=calendarId,
# ).execute()
# )

# for event in events['items']:

# event_title = event['summary']

# # Deleted the last 6 characters (deleted UTC time)
# start_date_time = event["start"]["dateTime"]
# start_date_time = start_date_time[:-6]

# # Deleted the last 6 characters (deleted UTC time)
# end_date_time = event['end']["dateTime"]
# end_date_time = end_date_time[:-6]

# booking_event.append([event_title, start_date_time, end_date_time])

# context = {
# "form":form,
# "booking_event" : booking_event,
# }

# return context
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
- Events (meetings, events and programs) for activities Callender
- Attendance system for events
- Blog (post & comments )
- Podcast (Video and audio for sermons)
- Polls and Surveys
- pleadges, offering and donation
- Prayer Request ,Contact
- schedule meetings
- Email
- Live streeming
- e-bible
- Video and audio for sermons
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ pillow==9.4.0
cloudinary=1.31.0
django-podcasting=1.3.2
python-dotenv=0.21.1
google-api-python-client

0 comments on commit fad501b

Please sign in to comment.