def get_lunch_subject(self, day):
        """ Locate lunch subject with the appropriate day in the 'message for directors' field. """

        category = self.get_lunch_category()
        lunch_subjects = ClassSubject.objects.filter(parent_program__id=self.program.id, category=self.get_lunch_category(), message_for_directors=day.isoformat())
        lunch_subject = None
        example_timeslot = self.days[day]['lunch'][0]
        timeslot_length = (example_timeslot.end - example_timeslot.start).seconds / 3600.0

        if lunch_subjects.count() == 0:
            #   If no lunch was found, create a new subject
            new_subject = ClassSubject()
            new_subject.grade_min = 7
            new_subject.grade_max = 12
            new_subject.parent_program = self.program
            new_subject.category = category
            new_subject.class_info = 'Enjoy a break for lunch with your friends!  Please register for at least one lunch period on each day of the program.'
            new_subject.class_size_min = 0
            # If the program doesn't have a max size, we unfortunately still
            # need one here.  Set a really big one.
            new_subject.class_size_max = self.program.program_size_max or 10**6
            new_subject.status = 10
            new_subject.duration = '%.4f' % timeslot_length
            new_subject.message_for_directors = day.isoformat()
            new_subject.save()
            new_subject.title = 'Lunch Period'
            new_subject.save()
            lunch_subject = new_subject
        else:
            #   Otherwise, return an existing lunch subject
            lunch_subject = lunch_subjects[0]
        return lunch_subject
    def get_lunch_subject(self, day):
        """ Locate lunch subject with the appropriate day in the 'message for directors' field. """

        category = self.get_lunch_category()
        lunch_subjects = ClassSubject.objects.filter(
            parent_program__id=self.program.id,
            category=self.get_lunch_category(),
            message_for_directors=day.isoformat())
        lunch_subject = None
        example_timeslot = self.days[day]['lunch'][0]
        timeslot_length = (example_timeslot.end -
                           example_timeslot.start).seconds / 3600.0

        if lunch_subjects.count() == 0:
            #   If no lunch was found, create a new subject
            new_subject = ClassSubject()
            new_subject.grade_min = 7
            new_subject.grade_max = 12
            new_subject.parent_program = self.program
            new_subject.category = category
            new_subject.class_info = 'Enjoy a break for lunch with your friends!  Please register for at least one lunch period on each day of the program.'
            new_subject.class_size_min = 0
            new_subject.class_size_max = self.program.program_size_max
            new_subject.status = 10
            new_subject.duration = '%.4f' % timeslot_length
            new_subject.message_for_directors = day.isoformat()
            new_subject.anchor = self.program.classes_node()
            new_subject.save()
            nodestring = new_subject.category.symbol + str(new_subject.id)
            new_anchor = new_subject.anchor.tree_create([nodestring])
            new_anchor.friendly_name = 'Lunch Period'
            new_anchor.save()
            new_subject.anchor = new_anchor
            new_subject.save()
            lunch_subject = new_subject
            #   print 'Generated subject: %s' % lunch_subject
        else:
            #   Otherwise, return an existing lunch subject
            lunch_subject = lunch_subjects[0]
            #   print 'Selected subject: %s' % lunch_subject
        return lunch_subject
    def get_lunch_subject(self, day):
        """ Locate lunch subject with the appropriate day in the 'message for directors' field. """

        category = self.get_lunch_category()
        lunch_subjects = ClassSubject.objects.filter(
            parent_program__id=self.program.id,
            category=self.get_lunch_category(),
            message_for_directors=day.isoformat())
        lunch_subject = None
        example_timeslot = self.days[day]['lunch'][0]
        timeslot_length = (example_timeslot.end -
                           example_timeslot.start).seconds / 3600.0

        if lunch_subjects.count() == 0:
            #   If no lunch was found, create a new subject
            new_subject = ClassSubject()
            new_subject.grade_min = 7
            new_subject.grade_max = 12
            new_subject.parent_program = self.program
            new_subject.category = category
            new_subject.class_info = 'Enjoy a break for lunch with your friends!  Please register for at least one lunch period on each day of the program.'
            new_subject.class_size_min = 0
            # If the program doesn't have a max size, we unfortunately still
            # need one here.  Set a really big one.
            new_subject.class_size_max = self.program.program_size_max or 10**6
            new_subject.status = 10
            new_subject.duration = '%.4f' % timeslot_length
            new_subject.message_for_directors = day.isoformat()
            new_subject.save()
            new_subject.title = 'Lunch Period'
            new_subject.save()
            lunch_subject = new_subject
        else:
            #   Otherwise, return an existing lunch subject
            lunch_subject = lunch_subjects[0]
        return lunch_subject
    def satprep_classgen(self, request, tl, one, two, module, extra, prog):
        """ This view will generate the classes for all the users. """

        delete = False

        if not request.method == 'POST' and not request.POST.has_key(
                'newclass_create'):
            #   Show the form asking for a room number and capacity for each teacher.

            user_list = self.program.getLists()['teachers_satprepinfo']['list']
            reginfos = [
                module_ext.SATPrepTeacherModuleInfo.objects.get(program=prog,
                                                                user=u)
                for u in user_list
            ]
            reginfos.sort(key=lambda x: x.subject + x.section)
            user_list = [r.user for r in reginfos]

            context = {
                'timeslots': prog.getTimeSlots(),
                'teacher_data': zip([ESPUser(u) for u in user_list], reginfos)
            }

            return render_to_response(self.baseDir() + 'newclass_confirm.html',
                                      request, (prog, tl), context)

        #   Delete current classes if specified (currently turned off, not necessary)
        if delete:
            cur_classes = ClassSubject.objects.filter(
                parent_program=self.program)
            [cls.delete() for cls in cur_classes]

        data = request.POST

        #   Pull the timeslots from the multiselect field on the form.
        timeslots = []
        for ts_id in data.getlist('timeslot_ids'):
            ts = Event.objects.get(id=ts_id)
            timeslots.append(ts)

        #   Create classrooms based on the form input.
        for key in data:
            key_dir = key.split('_')
            if len(key_dir) == 2 and key_dir[0] == 'room' and len(
                    data[key]) > 0:
                #   Extract a room number and capacity from POST data.
                room_num = data.get(key)
                cap_key = 'capacity_' + key_dir[1]
                room_capacity = int(data.get(cap_key))
                reginfo = module_ext.SATPrepTeacherModuleInfo.objects.get(
                    id=int(key_dir[1]))
                user = reginfo.user

                #   Initialize a class subject.
                newclass = ClassSubject()
                newclass.parent_program = self.program
                newclass.class_info = '%s: Section %s (%s)' % (
                    reginfo.get_subject_display(), reginfo.section,
                    reginfo.get_section_display())
                newclass.grade_min = 9
                newclass.grade_max = 12

                newclass.class_size_min = 0
                newclass.class_size_max = room_capacity

                newclass.category = ClassCategories.objects.get(
                    category='SATPrep')
                newclass.anchor = self.program.classes_node()

                newclass.save()

                nodestring = 'SAT' + str(newclass.id)
                newclass.anchor = self.program.classes_node().tree_create(
                    [nodestring])
                newclass.anchor.friendly_name = 'SAT Prep %s - %s' % (
                    reginfo.get_subject_display(),
                    reginfo.get_section_display())
                newclass.anchor.save()
                newclass.anchor.tree_create(['TeacherEmail'])
                newclass.save()

                newclass.makeTeacher(user)
                newclass.accept()

                #   Create a section of the class for each timeslot.
                #   The sections are all held in the same room by default.  This can be changed
                #   in the scheduling module later.
                for ts in timeslots:
                    new_room, created = Resource.objects.get_or_create(
                        name=room_num,
                        res_type=ResourceType.get_or_create('Classroom'),
                        event=ts)
                    new_room.num_students = room_capacity
                    new_room.save()
                    sec = newclass.add_section(
                        duration=(ts.duration().seconds / 3600.0))
                    sec.meeting_times.add(ts)
                    sec.assign_room(new_room)
                    sec.status = 10
                    sec.save()

        #dummy_anchor.delete()
        return HttpResponseRedirect('/manage/%s/schedule_options' %
                                    self.program.getUrlBase())