def patch(self, request, pk, format=None): sessionslot = self.get_object(pk) gapi = google_apis() end_date = request.data.get("end_date") end_date = aux_fns.date_combine_time(str(end_date), int(sessionslot.msm)) calendar_id = sessionslot.mentee_computer.library.calendar_id event_id = sessionslot.event_id # gapi.update_event(calendar_id, event_id, end_date) newId = gapi.update_event(calendar_id, event_id, end_date) sessionslot.save() serializer = SessionSlotSerializer( sessionslot, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def book_sessionslot(request): """ Gets an sessionslot list at a given time based on preferences then randomly picks one sessionslot and populates it with the mentor's name (queries specific fields by primary key). URL example: api/book/?library=1&language=1&msm=1 """ appts = SessionSlot.objects.all() library_params = request.query_params.get("library") language_params = request.query_params.get("language") msm_params = request.query_params.get("msm") if library_params is None or library_params == "0": appts = appts.filter( mentor=None, language=language_params, msm=msm_params,) else: appts = appts.filter( mentor=None, mentee_computer__library=library_params, language=language_params, msm=msm_params, ) # Check if there are no sessionslots that match the request. if not appts: return Response( { "success": "false", "message": "No available sessionslots exist with those specifications.", } ) #choose a random session slot from among those filtered myappt = random.choice(appts) myappt.mentor = request.user myappt.start_date = datetime.today() + timedelta( days=(aux_fns.diff_today_dsm(myappt.msm) + 7) ) myappt.end_date = myappt.start_date + timedelta(weeks=17) gapi = google_apis() start_time = aux_fns.date_combine_time( str(myappt.start_date), int(myappt.msm)) end_date = aux_fns.date_combine_time(str(myappt.end_date), int(myappt.msm)) event_id, hangouts_link = gapi.calendar_event( myappt.mentor.first_name, myappt.mentee_computer.computer_email, myappt.mentor.mp.vbb_email, myappt.mentor.mp.personal_email, myappt.mentee_computer.library.program_director_email, start_time, end_date, myappt.mentee_computer.library.calendar_id, myappt.mentee_computer.room_id, ) myappt.event_id = event_id myappt.hangouts_link = hangouts_link myappt.save() library_time = aux_fns.display_day( myappt.mentee_computer.library.time_zone, myappt.msm, myappt.end_date ) newMentorNotice_mail = os.path.join( "api", "emails", "templates", "newMentorNotice.html" ) sessionConfirm_mail = os.path.join( "api", "emails", "templates", "sessionConfirm.html" ) gapi.email_send( myappt.mentee_computer.library.program_director_email, "New Mentoring Session Booked for " + library_time, newMentorNotice_mail, { '__directorname': myappt.mentee_computer.library.program_director_name, '__sessionslot': library_time, '__start': myappt.start_date.strftime("%x"), '__mentorname': myappt.mentor.first_name +" "+ myappt.mentor.last_name, '__mentoremail': myappt.mentor.email, '__occupation': myappt.mentor.mp.occupation, '__languages': myappt.mentor.mp.languages, '__computer': str(myappt.mentee_computer) }, ['*****@*****.**'] ) gapi.email_send( myappt.mentor.mp.vbb_email, "New Mentoring Session Booked for " + myappt.display(), sessionConfirm_mail, { '__mentorname' : myappt.mentor.first_name, '__sessionslot': myappt.display(), '__start': myappt.start_date.strftime("%x"), '__programname': myappt.mentee_computer.library.name, '__programdirector': myappt.mentee_computer.library.program_director_name, '__hangout': myappt.hangouts_link, '__vbbemail': myappt.mentor.email, '__pdemail': myappt.mentee_computer.library.program_director_email, }, [myappt.mentor.mp.personal_email], ) training_mail = os.path.join("api", "emails", "templates", "training.html") gapi.email_send( myappt.mentor.mp.vbb_email, "VBB Mentor Training", training_mail, { '__mentorname': myappt.mentor.first_name, "__whatsapp_group": myappt.mentee_computer.library.whatsapp_group }, cc=[myappt.mentor.mp.personal_email] ) gapi.group_subscribe( myappt.mentee_computer.library.announcements_group, myappt.mentor.mp.personal_email, ) gapi.group_subscribe( myappt.mentee_computer.library.announcements_group, myappt.mentor.mp.vbb_email ) gapi.group_subscribe( myappt.mentee_computer.library.collaboration_group, myappt.mentor.mp.vbb_email ) # FIXME - Add try/except/finally blocks for error checking (not logged in, sessionslot got taken before they refreshed) return Response( {"success": "true", "user": str( myappt.mentor), "sessionslot": str(myappt), } )