def schedule_lunch(user): logger.info('Schedule lunch for user %s' % user) today = datetime.today().date() try: profile = user.profile except: logger.info('User %s never filled out their profile' % user) return try: calendar_id = get_calendar_id(user) except UserSocialAuth.DoesNotExist as e: # admin users logger.error(e) return logger.info('Found calendar id %s for user %s' % (calendar_id, user)) start, end = _get_week_start_end(today, offset_weeks=1) logger.info('Start %s, end %s' % (start, end)) already_exists = LunchAttendee.get_existing(user, start, end) if already_exists: logger.info('Already has a lunch this week %s, aborting.' % (already_exists)) return #if profile and profile.already_tried_to_schedule(today): # logger.info('Already tried to schedule this user, defer until %s' % (profile.min_lunch_date)) # return dates_to_check = [ (start + timedelta(days=WEDNESDAY)), (start + timedelta(days=THURSDAY)), (start + timedelta(days=FRIDAY)), ] logger.info('Checking dates %s' % dates_to_check) lunch_date = get_random_date_available(user, calendar_id, dates_to_check) if not lunch_date: logger.info('Found NO availability for user %s' % user) if profile: profile.min_lunch_date = end profile.save() logger.info('Updating min_lunch_date for user %s to %s' % (user, end)) # TODO: send email to user? return logger.info('Found availability for date %s' % lunch_date) open_lunch = Lunch.get_open_for_date(lunch_date) if open_lunch: logger.info('Found existing open lunch entry %s' % open_lunch) else: open_lunch = Lunch.create_new(lunch_date) logger.info('Created new lunch entry %s' % open_lunch) open_lunch.add_attendee(user) logger.info('Added %s to lunch %s as an attendee' % (user, open_lunch)) new_event_id = open_lunch.create_or_update_calendar_event(user) if new_event_id: logger.info('Created new calendar event %s' % new_event_id)
def profile(request): profile = get_profile_or_none(request.user) lunch_attendees = LunchAttendee.get_latest_for_user(request.user) form = ProfileForm(instance=profile) if request.method == 'POST': form = ProfileForm(request.POST, instance=profile) if form.is_valid(): profile = form.save(commit=False) profile.user = request.user profile.save() messages.add_message(request, messages.INFO, 'Your profile was updated') return HttpResponseRedirect(reverse('profile')) # TODO: this sucks if profile: celery.schedule_lunch_task.delay(request.user.id) return render(request, 'profile.html', locals())