def handle(self, *args, **options): """ Shifts all session slots to be encoded in UTC instead of EST """ def _writelog(logstr): with open("log_remove_end.txt",'a') as writefile: writefile.write(logstr + '\n') try: gapi = google_apis() allslots = SessionSlot.objects.all() for slot in allslots: try: if slot.mentor and slot.event_id: try: gapi.remove_end_date(slot.mentee_computer.library.calendar_id,slot.event_id) _writelog("Successfully updated google event for {}".format(slot.id)) except Exception as e: _writelog("{} Failed to update google event for {}".format(str(e),slot.id)) else: _writelog("No google event for slot {}".format(slot.id)) except: _writelog("Failed to add {}".format(slot.id)) except Exception as e: return str(e) return "true"
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), } )
def first_time_signup(request): """ When a user signs up, create a mentor profile. If they are new mentors, create a vbb email and send a welcome email. """ fname = request.data.get("first_name").title() lname = request.data.get("last_name").title() pemail = request.data.get("personal_email").lower() gapi = google_apis() #TODO test this functionality more thoroughly if request.data["vbb_email"] is not None and request.data["vbb_email"] != '': #check to see if the serializer works serializer = MentorProfileSerializer(data=request.data) if not (serializer.is_valid()): return Response( {"success": "false", "message": (str(serializer.errors)), } ) # FIXME use proper protocol and add a status request.data["vbb_email"] = request.data["vbb_email"].lower() mps = MentorProfile.objects.filter(vbb_email=request.data["vbb_email"]) if mps is not None and len(mps) > 0: return Response( { "success": "false", "message": "Sorry, this VBB email has already been used to create a mentor profile.", } # , # status=status.HTTP_400_BAD_REQUEST #FIXME include status ) welcome_mail = os.path.join( "api", "emails", "templates", "welcomeLetterExisting.html" ) gapi.email_send( pemail, # personal email form form # FIXME change back to welcome to the VBB family when we start registering new mentors "Welcome to the New VBB Portal!", welcome_mail, { "__first_name": fname, # first name from form "__new_email": request.data["vbb_email"], }, [request.data["vbb_email"]], ) else: # check to see if the serializer works request.data["vbb_email"] = "*****@*****.**" serializer = MentorProfileSerializer(data=request.data) if not (serializer.is_valid()): return Response( {"success": "false", "message": (str(serializer.errors)), } ) # FIXME use proper protocol and add a status request.data["vbb_email"], pwd = gapi.account_create( fname.lower(), lname.lower(), pemail ) welcome_mail = os.path.join( "api", "emails", "templates", "welcomeLetter.html") gapi.email_send( pemail, # personal email form form # FIXME change back to welcome to the VBB family when we start registering new mentors "Welcome to the New VBB Portal!", welcome_mail, { "__first_name": fname, # first name from form "__new_email": request.data[ "vbb_email" ], # email generated by shwetha's code "__password": pwd, # password generated by shwetha's code }, [request.data["vbb_email"]], ) gapi.group_subscribe( "*****@*****.**", pemail) gapi.group_subscribe( "*****@*****.**", request.data["vbb_email"] ) gapi.group_subscribe( "*****@*****.**", request.data["vbb_email"] ) serializer = MentorProfileSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response({ 'success': 'true', 'serializer': serializer.data}, status=status.HTTP_201_CREATED ) return Response({ 'success': 'false', 'message': (str(serializer.errors)), })#FIXME use proper protocol and add a status