def get(self): key = self.request.get("key", False) if not key or key != "hartley-rulez": self.error(401) return self.response.out.write("What's the magic password??") # commence shufflin' http://youtu.be/KQ6zr6kCPj8 all_employees = [e for e in Employee.all() ] # lists are easier to work w than Query objects already_matched_employees = [] created_lunches = [] for employee in all_employees: if employee not in already_matched_employees: already_matched_employees.append(employee) employees_left = [ e for e in all_employees if e not in already_matched_employees ] partner = choose_partner(employees_left, employee) if partner: logging.info("creating lunch between %s and %s" % (employee, partner)) already_matched_employees.append(partner) l = Lunch(employees=[employee.key(), partner.key()]) l.put() created_lunches.append(l) else: logging.info("couldn't find lunch mate for %s" % employee) already_matched_employees.remove(employee) logging.info("created lunches:") logging.info(created_lunches)
def get(self): key = self.request.get("key", False) if not key or key != "hartley-rulez": self.error(401) return self.response.out.write("What's the magic password??") # commence shufflin' http://youtu.be/KQ6zr6kCPj8 all_employees = [e for e in Employee.all()] # lists are easier to work w than Query objects already_matched_employees = [] created_lunches = [] for employee in all_employees: if employee not in already_matched_employees: already_matched_employees.append(employee) employees_left = [e for e in all_employees if e not in already_matched_employees] partner = choose_partner(employees_left, employee) if partner: logging.info("creating lunch between %s and %s" % (employee, partner)) already_matched_employees.append(partner) l = Lunch(employees=[employee.key(), partner.key()]) l.put() created_lunches.append(l) else: logging.info("couldn't find lunch mate for %s" % employee) already_matched_employees.remove(employee) logging.info("created lunches:") logging.info(created_lunches)
def new_lunch_template(request,curr_date): if request.method == 'POST': form = LunchForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('/lunch/') else: new_lunch = Lunch() new_lunch.date = curr_date new_lunch.time = "13:00" form = LunchForm(instance=new_lunch) return render_to_response('new_lunch.html', {'form': form})
def get(self, timeframe): current_user = users.get_current_user() template_values = {} if timeframe == "week": template_values["lunch"] = Lunch.all().filter("employees = ", current_user).order("-assigned")[0] elif timeframe == "month": template_values["lunch"] = Lunch.all().filter("employees = ", current_user).order("-assigned")[:4] else: self.error(404) return self.response.out.write("I don't know what timeframe you're trying to access...") self.response.out.write(template.render("templates/lunch.html", template_values))
def curr_week_lunch_list(): weekday_num = datetime.date.today().isoweekday() begin_curr_week_date = datetime.date.today()-datetime.timedelta(days=weekday_num-1) lunch_list = [] for i in range(14): curr_date = begin_curr_week_date + datetime.timedelta(days=i) lunch_suggestions = Lunch.objects.filter(date__exact=curr_date) if not lunch_suggestions: new_lunch = Lunch() new_lunch.date = curr_date new_lunch.time = datetime.time(13,00) # lunch at 13:00 lunch_list.append(new_lunch) else: lunch_list.append(lunch_suggestions[0]) return lunch_list
def get(self, timeframe): current_user = users.get_current_user() template_values = {} if timeframe == "week": template_values["lunch"] = Lunch.all().filter( "employees = ", current_user).order("-assigned")[0] elif timeframe == "month": template_values["lunch"] = Lunch.all().filter( "employees = ", current_user).order("-assigned")[:4] else: self.error(404) return self.response.out.write( "I don't know what timeframe you're trying to access...") self.response.out.write( template.render("templates/lunch.html", template_values))
def choose_partner(available_employees, this_employee): """ logic that returns a partner (Employee) for the single Employee from the list of employees, or None """ if len(available_employees) == 0: return None from random import choice # create list of departments depts = [d["slug"] for d in DEPARTMENTS] # seperate employees into departments pools = { dept: [e for e in available_employees if e.department == dept] for dept in depts } # choose a random department sanity_check = 0 while True and sanity_check < 25: sanity_check += 1 chosen_dept = choice(depts) if len(pools[chosen_dept]) == 0: continue # must be employees in this department if chosen_dept == this_employee.department: continue # must be a different department than this_employee's # alright, we've found a potential dept, now let's find an employee sanity_check_2 = 0 while True and sanity_check_2 < 25: sanity_check_2 += 1 potential_partner = choice(pools[chosen_dept]) logging.info("attempting to see if %s and %s have eaten before" % (this_employee, potential_partner)) have_these_two_been_matched_before = Lunch.all().filter( "employees = ", this_employee).filter("employees = ", potential_partner).count() logging.info("result: %s" % (have_these_two_been_matched_before, )) if not have_these_two_been_matched_before: return potential_partner if sanity_check == 25: logging.warning("couldn't find a department to match %s from %s" % (this_employee, available_employees)) return None
def choose_partner(available_employees, this_employee): """ logic that returns a partner (Employee) for the single Employee from the list of employees, or None """ if len(available_employees) == 0: return None from random import choice # create list of departments depts = [d["slug"] for d in DEPARTMENTS] # seperate employees into departments pools = {dept: [e for e in available_employees if e.department == dept] for dept in depts} # choose a random department sanity_check = 0 while True and sanity_check < 25: sanity_check += 1 chosen_dept = choice(depts) if len(pools[chosen_dept]) == 0: continue # must be employees in this department if chosen_dept == this_employee.department: continue # must be a different department than this_employee's # alright, we've found a potential dept, now let's find an employee sanity_check_2 = 0 while True and sanity_check_2 < 25: sanity_check_2 += 1 potential_partner = choice(pools[chosen_dept]) logging.info("attempting to see if %s and %s have eaten before" % (this_employee, potential_partner)) have_these_two_been_matched_before = Lunch.all().filter("employees = ", this_employee).filter("employees = ", potential_partner).count() logging.info("result: %s" % (have_these_two_been_matched_before, )) if not have_these_two_been_matched_before: return potential_partner if sanity_check == 25: logging.warning("couldn't find a department to match %s from %s" % (this_employee, available_employees)) return None