def refresh_interviews(self, interviewer, period_start, period_end): db_session = DB_Session_Factory.get_db_session() db_session.autoflush = False try: existing_interviews = [] interviews_to_delete = [] interviews_for_interviewer = [] current_date = period_start.date() candidate_cache = {} existing_interviews = interviewer.interviews.filter(Interview.start_time >= period_start, Interview.start_time < (current_date + timedelta(days=1))).with_lockmode('update').all() for existing_interview in existing_interviews: interviews_to_delete.append(existing_interview) events_request = self.service.events().list(calendarId = interviewer.email, timeZone = LOS_ANGELES_TZ, timeMin = period_start.isoformat(), timeMax = period_end.isoformat(), orderBy = 'startTime', singleEvents = True) while (events_request != None): response = events_request.execute(self.http) for event in response.get('items', []): summary = event.get('summary', '') match = re.search("OnSite Interview - (.*?)(?:,(.*))?$", summary) if match: if (re.match("^.*?[Ss][Hh][Aa][Dd][Oo][Ww].*?Interview", summary)): continue print "Found interview: " + summary candidate_name = match.group(1) position = match.group(2) if match.group(2) is not None else "" print "Name: " + candidate_name print "Position: " + position candidate = candidate_cache.get(candidate_name, db_session.query(Candidate).get(candidate_name)) if candidate is None: candidate = Candidate(candidate_name, position, '*****@*****.**') candidate_cache[candidate_name] = candidate room = event.get('location', 'Unknown Location') start_time = google_ts_to_datetime(event.get('start', {}).get('dateTime', DEFAULT_DATE)) if start_time.date() != current_date: flush_interviews(interviewer, interviews_to_delete, interviews_for_interviewer, current_date, start_time, db_session) current_date = start_time.date() existing_interviews = interviewer.interviews.filter(Interview.start_time >= start_time, Interview.start_time < (current_date + timedelta(days=1))).all() interviews_to_delete = [] interviews_for_interviewer = [] for existing_interview in existing_interviews: interviews_to_delete.append(existing_interview) end_time = google_ts_to_datetime(event.get('end', {}).get('dateTime', DEFAULT_DATE)) interview_exists = False for existing_interview in existing_interviews: if existing_interview.candidate_name == candidate_name: existing_interview.room = room existing_interview.start_time = start_time existing_interview.end_time = end_time existing_interview.candidate = candidate if existing_interview in interviews_to_delete: interviews_to_delete.remove(existing_interview) interview_exists = True interviews_for_interviewer.append(existing_interview) break if interview_exists is False: new_interview = Interview(interviewer.email, start_time, end_time, candidate_name, room) new_interview.candidate = candidate db_session.add(new_interview) existing_interviews.append(new_interview) interviews_for_interviewer.append(new_interview) events_request = self.service.events().list_next(events_request, response) except AccessTokenRefreshError: print ("The credentials have been revoked or expired, please re-run" "the application to re-authorize") flush_interviews(interviewer, interviews_to_delete, interviews_for_interviewer, current_date, period_end, db_session)
end_time = google_ts_to_datetime(event.get("end", {}).get("dateTime", DEFAULT_DATE)) interview_exists = False for existing_interview in existing_interviews: if existing_interview.candidate_name == candidate_name: existing_interview.room = room existing_interview.start_time = start_time existing_interview.end_time = end_time existing_interview.candidate = candidate if existing_interview in interviews_to_delete: interviews_to_delete.remove(existing_interview) interview_exists = True interviews_for_interviewer.append(existing_interview) break if interview_exists is False: new_interview = Interview(interviewer.email, start_time, end_time, candidate_name, room) new_interview.candidate = candidate db_session.add(new_interview) existing_interviews.append(new_interview) interviews_for_interviewer.append(new_interview) events_request = service.events().list_next(events_request, response) for interview_to_delete in interviews_to_delete: print "Deleting interview with " + interview_to_delete.candidate_name candidate = interview_to_delete.candidate db_session.delete(interview_to_delete) if not candidate.interviews: db_session.delete(candidate) phone_number_index = 0 for index, existing_interview in enumerate(interviews_for_interviewer): if existing_interview.phone_number_to_use is None: # Make sure that no two consecutive interviews use the same phone number while (