def onsite_create(self, request, tl, one, two, module, extra, prog):
        if request.method == 'POST':
            form = OnSiteRegForm(request.POST)

            if form.is_valid():
                new_data = form.cleaned_data
                username = ESPUser.get_unused_username(new_data['first_name'],
                                                       new_data['last_name'])
                new_user = ESPUser.objects.create_user(
                    username=username,
                    first_name=new_data['first_name'],
                    last_name=new_data['last_name'],
                    email=new_data['email'])

                self.student = new_user

                regProf = RegistrationProfile.getLastForProgram(
                    new_user, self.program)
                contact_user = ContactInfo(first_name=new_user.first_name,
                                           last_name=new_user.last_name,
                                           e_mail=new_user.email,
                                           user=new_user)
                contact_user.save()
                regProf.contact_user = contact_user

                student_info = StudentInfo(
                    user=new_user,
                    graduation_year=ESPUser.YOGFromGrade(
                        new_data['grade'],
                        ESPUser.program_schoolyear(self.program)))

                try:
                    if isinstance(new_data['k12school'], K12School):
                        student_info.k12school = new_data['k12school']
                    else:
                        if isinstance(new_data['k12school'], int):
                            student_info.k12school = K12School.objects.get(
                                id=int(new_data['k12school']))
                        else:
                            student_info.k12school = K12School.objects.filter(
                                name__icontains=new_data['k12school'])[0]
                except:
                    student_info.k12school = None
                student_info.school = new_data[
                    'school'] if not student_info.k12school else student_info.k12school.name

                student_info.save()
                regProf.student_info = student_info

                regProf.save()

                if new_data['paid']:
                    self.createBit('paid')
                    self.updatePaid(True)
                else:
                    self.updatePaid(False)

                self.createBit('Attended')

                if new_data['medical']:
                    self.createBit('Med')

                if new_data['liability']:
                    self.createBit('Liab')

                self.createBit('OnSite')

                new_user.groups.add(Group.objects.get(name="Student"))

                new_user.recoverPassword()

                return render_to_response(
                    self.baseDir() + 'reg_success.html', request, {
                        'student':
                        new_user,
                        'retUrl':
                        '/onsite/%s/classchange_grid?student_id=%s' %
                        (self.program.getUrlBase(), new_user.id)
                    })

        else:
            form = OnSiteRegForm()

        return render_to_response(self.baseDir() + 'reg_info.html', request,
                                  {'form': form})
Beispiel #2
0
    def onsite_create(self, request, tl, one, two, module, extra, prog):
        if request.method == 'POST':
            form = OnSiteRegForm(request.POST)
            
            if form.is_valid():
                new_data = form.cleaned_data
                username = ESPUser.get_unused_username(new_data['first_name'], new_data['last_name'])
                new_user = ESPUser(username = username,
                                first_name = new_data['first_name'],
                                last_name  = new_data['last_name'],
                                email      = new_data['email'],
                                is_staff   = False,
                                is_superuser = False)
                new_user.save()

                self.student = new_user

                regProf = RegistrationProfile.getLastForProgram(new_user,
                                                                self.program)
                contact_user = ContactInfo(first_name = new_user.first_name,
                                           last_name  = new_user.last_name,
                                           e_mail     = new_user.email,
                                           user       = new_user)
                contact_user.save()
                regProf.contact_user = contact_user

                student_info = StudentInfo(user = new_user, graduation_year = ESPUser.YOGFromGrade(new_data['grade']))
                student_info.save()
                regProf.student_info = student_info

                regProf.save()
                
                if new_data['paid']:
                    self.createBit('Paid')
                    self.updatePaid(True)
                else:
                    self.updatePaid(False)

                self.createBit('Attended')

                if new_data['medical']:
                    self.createBit('MedicalFiled')

                if new_data['liability']:
                    self.createBit('LiabilityFiled')

                self.createBit('OnSite')

                v = GetNode( 'V/Flags/UserRole/Student')
                ub = UserBit()
                ub.user = new_user
                ub.recursive = False
                ub.qsc = GetNode('Q')
                ub.verb = v
                ub.save()

                new_user.recoverPassword()
                
                return render_to_response(self.baseDir()+'reg_success.html', request, (prog, tl), {
                    'student': new_user, 
                    'retUrl': '/onsite/%s/classchange_grid?student_id=%s' % (self.program.getUrlBase(), new_user.id)
                    })

        else:
            form = OnSiteRegForm()

	return render_to_response(self.baseDir()+'reg_info.html', request, (prog, tl), {'form':form, 'current_year':ESPUser.current_schoolyear()})
    def getMissingTeachers(self, prog, starttime=None, when=None):
        """Return a list of class sections with missing teachers"""
        sections = prog.sections().annotate(begin_time=Min("meeting_times__start")) \
                                  .filter(status=10, parent_class__status=10, begin_time__isnull=False)
        if starttime is not None:
            sections = sections.filter(begin_time=starttime.start)
        teachers = ESPUser.objects.filter(
            userbit__in=UserBit.valid_objects(when),
            userbit__qsc__classsubject__sections__in=sections,
            userbit__verb=GetNode('V/Flags/Registration/Teacher'))
        arrived = teachers.filter(
            userbit__in=UserBit.valid_objects(when),
            userbit__qsc=prog.anchor,
            userbit__verb=GetNode('V/Flags/Registration/Teacher/Arrived'))
        missing = teachers.exclude(id__in=arrived)
        missing_sections = sections.filter(
            parent_class__anchor__userbit_qsc__in=UserBit.valid_objects(when),
            parent_class__anchor__userbit_qsc__user__in=missing,
            parent_class__anchor__userbit_qsc__verb=GetNode(
                'V/Flags/Registration/Teacher'))
        userbits = UserBit.valid_objects(when).filter(qsc__classsubject__sections__in=missing_sections,
                                                  verb=GetNode('V/Flags/Registration/Teacher')) \
                                          .distinct() \
                                          .values_list('user', 'qsc__classsubject', 'qsc__friendly_name') \
                                          .order_by('user__last_name', 'user__first_name')

        teacher_dict = {}
        for teacher in list(arrived) + list(missing):
            contact = teacher.getLastProfile().contact_user
            if contact is None:
                contact = ContactInfo(phone_cell='N/A')
            teacher_dict[teacher.id] = {
                'username': teacher.username,
                'name': teacher.name(),
                'last_name': teacher.last_name,
                'phone': contact.phone_cell or contact.phone_day,
                'arrived': True
            }
        for teacher in missing:
            teacher_dict[teacher.id]['arrived'] = False

        class_dict = {}
        class_arr = []
        for teacher_id, class_id, class_name in userbits:
            if class_id not in class_dict:
                class_dict[class_id] = {
                    'id': ClassSubject.objects.get(id=class_id).emailcode(),
                    'name': class_name,
                    'teachers': [],
                    'any_arrived': False
                }
                class_arr.append(class_dict[class_id])
            class_dict[class_id]['teachers'].append(teacher_dict[teacher_id])

        for sec in missing_sections:
            if sec.parent_class.id in class_dict:
                class_ = class_dict[sec.parent_class.id]
                class_['room'] = (sec.prettyrooms() or [None])[0]
                if 'time' in class_:
                    class_['time'] = min(class_['time'], sec.begin_time)
                else:
                    class_['time'] = sec.begin_time

        #Move sections where at least one teacher showed up to end of list
        for sec in class_arr:
            for teacher in sec['teachers']:
                if teacher['arrived']:
                    sec['any_arrived'] = True
                    break
        class_arr = [sec for sec in class_arr if sec['any_arrived'] == False] + \
                    [sec for sec in class_arr if sec['any_arrived'] == True]

        return class_arr, teacher_dict
    def getMissingTeachers(self, prog, starttime=None, when=None):
        """Return a list of class sections with missing teachers"""
        sections = prog.sections().annotate(begin_time=Min("meeting_times__start")) \
                                  .filter(status=10, parent_class__status=10, begin_time__isnull=False)
        if starttime is not None:
            sections = sections.filter(begin_time=starttime.start)
        teachers = ESPUser.objects.filter(classsubject__sections__in=sections)
        arrived = teachers.filter(record__program=prog,
                                  record__event='teacher_checked_in')
        missing = teachers.exclude(id__in=arrived)
        missing_sections = sections.filter(
            parent_class__teachers__in=missing, )
        teacher_tuples = ESPUser.objects.filter(classsubject__sections__in=missing_sections) \
                                          .distinct() \
                                          .values_list('id', 'classsubject__id', 'classsubject__title') \
                                          .order_by('last_name', 'first_name')

        teacher_dict = {}
        for teacher in list(arrived) + list(missing):
            contact = teacher.getLastProfile().contact_user
            if contact is None:
                contact = ContactInfo(phone_cell='N/A')
            teacher_dict[teacher.id] = {
                'username': teacher.username,
                'name': teacher.name(),
                'last_name': teacher.last_name,
                'phone': contact.phone_cell or contact.phone_day,
                'arrived': True
            }
        for teacher in missing:
            teacher_dict[teacher.id]['arrived'] = False

        class_dict = {}
        class_arr = []
        for teacher_id, class_id, class_name in teacher_tuples:
            if class_id not in class_dict:
                class_dict[class_id] = {
                    'id': ClassSubject.objects.get(id=class_id).emailcode(),
                    'name': class_name,
                    'teachers': [],
                    'any_arrived': False
                }
                class_arr.append(class_dict[class_id])
            class_dict[class_id]['teachers'].append(teacher_dict[teacher_id])

        for sec in missing_sections:
            if sec.parent_class.id in class_dict:
                class_ = class_dict[sec.parent_class.id]
                class_['room'] = (sec.prettyrooms() or [None])[0]
                if 'time' in class_:
                    class_['time'] = min(class_['time'], sec.begin_time)
                else:
                    class_['time'] = sec.begin_time

        #Move sections where at least one teacher showed up to end of list
        for sec in class_arr:
            for teacher in sec['teachers']:
                if teacher['arrived']:
                    sec['any_arrived'] = True
                    break
        class_arr = [sec for sec in class_arr if sec['any_arrived'] == False] + \
                    [sec for sec in class_arr if sec['any_arrived'] == True]

        return class_arr, teacher_dict