def entry_review(request):
    error = " "
    try:
        #Attempt to find user's chosen school
        assigned_school = School.objects.get(assigned_to=request.user)
    except exceptions.ObjectDoesNotExist:
        # No school is associated with this user! Redirect to the select_schools page
        return HttpResponseRedirect('../school_select/school_select.html')

    #NOTE: School.objects.get(pk=int(form.getlist('school','')[0])) was previously used to get school from drop-down menu

    #Required that school form is pre-fetched to populate form
    student_list = SchoolStudent.objects.filter(school=assigned_school)
    individual_list, pair_list = compadmin.processGrade(
        student_list)  #processGrade is defined below this method
    invigilator_list = Invigilator.objects.filter(school=assigned_school)
    responsible_teacher = ResponsibleTeacher.objects.filter(
        school=assigned_school)

    for p in range(8, 13):
        pair_list[p] = pair_list[p]

    if not responsible_teacher:
        return HttpResponseRedirect('../students/newstudents.html')
    else:
        assigned_school.entered = 1  #The school has made an entry
        assigned_school.save()

    c = {
        'type': 'Students',
        'schooln': assigned_school,
        'responsible_teacher': responsible_teacher[0],
        'student_list': individual_list,
        'pair_list': pair_list,
        'entries_open': compadmin.isOpen() or request.user.is_staff,
        'invigilator_list': invigilator_list,
        'grades': range(8, 13),
        'error': error,
        'invigilator_range': range(10 - len(invigilator_list)),
        'igrades': range(8, 13),
        'ierror': error,
        "only_back": True
    }

    if request.method == 'POST' and 'edit_entry' in request.POST and (
            compadmin.isOpen()
            or request.user.is_staff):  # If the form has been submitted.
        return HttpResponseRedirect('../students/newstudents.html')
    if request.method == 'POST' and 'resend_confirmation' in request.POST:  # If the form has been submitted.
        confirmation.send_confirmation(
            request, assigned_school, cc_admin=False
        )  #Needs to only be bound to this user's email address
        return HttpResponseRedirect('../submitted.html')

    c.update(csrf(request))
    return render_to_response('entry_review.html',
                              c,
                              context_instance=RequestContext(request))
def entry_review(request):
    error = " "
    try:
        #Attempt to find user's chosen school
        assigned_school = School.objects.get(assigned_to=request.user)
    except exceptions.ObjectDoesNotExist:
        # No school is associated with this user! Redirect to the select_schools page
        return HttpResponseRedirect('../school_select/school_select.html')

    #NOTE: School.objects.get(pk=int(form.getlist('school','')[0])) was previously used to get school from drop-down menu

    #Required that school form is pre-fetched to populate form
    student_list = SchoolStudent.objects.filter(school = assigned_school)
    individual_list, pair_list = compadmin.processGrade(student_list) #processGrade is defined below this method
    invigilator_list = Invigilator.objects.filter(school = assigned_school)
    responsible_teacher = ResponsibleTeacher.objects.filter(school = assigned_school)

    for p in range(8,13):
        pair_list[p] = pair_list[p]

    if not responsible_teacher:
        return HttpResponseRedirect('../students/newstudents.html')
    else:
        assigned_school.entered=1 #The school has made an entry
        assigned_school.save()
        
    c = {'type':'Students',
        'schooln':assigned_school,
        'responsible_teacher':responsible_teacher[0],
        'student_list':individual_list,
        'pair_list':pair_list,
        'entries_open':compadmin.isOpen(),
        'invigilator_list': invigilator_list,
        'grades':range(8,13), 
        'error':error,
        'invigilator_range':range(10-len(invigilator_list)), 
        'igrades':range(8,13),
        'ierror':error}

    if request.method == 'POST' and 'edit_entry' in request.POST and compadmin.isOpen():  # If the form has been submitted.
        return HttpResponseRedirect('../students/newstudents.html')
    if request.method == 'POST' and 'resend_confirmation' in request.POST:  # If the form has been submitted.
        confirmation.send_confirmation(request, assigned_school, cc_admin=False) #Needs to only be bound to this user's email address
        return HttpResponseRedirect('../submitted.html')

    c.update(csrf(request))
    return render_to_response('entry_review.html', c, context_instance=RequestContext(request))
def newstudents(request):
    error = " "
    try:
        #Attempt to find user's chosen school
        assigned_school = School.objects.get(assigned_to=request.user)
    except exceptions.ObjectDoesNotExist:
        # No school is associated with this user! Redirect to the select_schools page
        return HttpResponseRedirect('../school_select/school_select.html')

    #NOTE: School.objects.get(pk=int(form.getlist('school','')[0])) was previously used to get school from drop-down menu

    #Required that school form is pre-fetched to populate form
    student_list = SchoolStudent.objects.filter(school = assigned_school)
    individual_list, pair_list = compadmin.processGrade(student_list) #processGrade is defined below this method
    invigilator_list = Invigilator.objects.filter(school = assigned_school)
    responsible_teacher = ResponsibleTeacher.objects.filter(school = assigned_school)

    editEntry = False
    language_selection_options = ['e', 'a', 'b']

    if responsible_teacher:
        editEntry = True
    
    if assigned_school and responsible_teacher:
        language_temp = assigned_school.language
        language_selection = [language_temp]
        language_selection.extend([c for c in language_selection_options if c != language_temp])
    else:
        language_selection = ['e', 'a', 'b']


    entries_per_grade = {} #Dictionary with grade:range(...)
    pairs_per_grade = {}
    for grade in range(8,13):
        entries_per_grade[grade] = range(compadmin.admin_number_of_individuals()-len(individual_list[grade]))
        #Place the "Previously Selected" number of pairs at the top of the list (So it appears as a default)
        pairs_per_grade[grade] = [pair_list[grade]]
        pairs_per_grade[grade].extend([i for i in range(0,compadmin.admin_number_of_pairs()+1) if i != pair_list[grade]])

    if request.method == 'POST':  # If the form has been submitted...

        form = (request.POST) # A form bound to the POST data

        #Delete all previously stored information

        try:
            assigned_school.language = form.getlist('language','')[0]
            assigned_school.save()

            #Register a single responsible teacher (assigned to that school)
            rtschool = assigned_school #School.objects.get(pk=int(form.getlist('school','')[0]))
            rtfirstname = form.getlist('rt_firstname','')[0]
            rtsurname = form.getlist('rt_surname','')[0]
            rtphone_primary = form.getlist('rt_phone_primary','')[0].strip().replace(' ', '')
            rtphone_alt = form.getlist('rt_phone_alt','')[0].strip().replace(' ', '')
            rtemail = form.getlist('rt_email','')[0].strip().replace(' ', '')

            #rtregistered_by =  User.objects.get(pk=int(form.getlist('rt_registered_by','')[0]))
            query = ResponsibleTeacher(firstname = rtfirstname , surname = rtsurname, phone_primary = rtphone_primary, 
                                      phone_alt = rtphone_alt, school = rtschool,
                                      email = rtemail)

            #Delete responsible teacher before saving the new one
            for rt in responsible_teacher:
                rt.delete()

            query.save()
            query.reference=query.id
            query.save()

        #Registering per grade
            for grade in range (8,13):
                  #Registering the different pairs
                  #Information is set to null, only school name is given and reference
                  #Reference if the ID of the first person in the pair
                  for p in range(int(form.getlist("pairs",'')[grade-8])):
                        firstname = 'Pair/Paar'
                        surname = str(grade)+chr(65+p)
                        language = form.getlist('language','')[0]
                        school = assigned_school
                        reference = '%3s%2s%2s'%(str(school.id).zfill(3),str(grade).zfill(2),str(11+p).zfill(2))
                        #registered_by =  User.objects.get(pk=int(form.getlist('registered_by','')[p]))
                        paired = True 
                        #Save first entry for pair
                        query = SchoolStudent(firstname = firstname , surname = surname, language = language,reference = reference,
                                school = school, grade = grade , paired = paired)
                        query.save()
                        #Save second entry for pair
                        #query1 = SchoolStudent(firstname = firstname , surname = surname, language = language, reference = reference, 
                                #school = school, grade=grade,
                                #paired = paired)
                        #query1.save()

            #Add invigilator information
            for invigilator in invigilator_list:
                invigilator.delete()
 
            for j in range(10):
                if form.getlist('inv_firstname','')[j] == u'':
                    ierror = "Invigilator information incomplete"
                else:
                    school = assigned_school
                    ifirstname = correctCapitals(form.getlist('inv_firstname','')[j])
                    isurname = correctCapitals(form.getlist('inv_surname','')[j])
                    iphone_primary = form.getlist('inv_phone_primary','')[j].strip().replace(' ', '')
                    iphone_alt = form.getlist('inv_phone_alt','')[j].strip().replace(' ', '')
                    iemail = form.getlist('inv_email','')[j].strip().replace(' ', '')
                    #iregistered_by =  User.objects.get(pk=int(form.getlist('inv_registered_by','')[j]))

                    query = Invigilator(school = school, firstname = ifirstname,surname = isurname,
                                       phone_primary = iphone_primary , phone_alt = iphone_alt, email = iemail)
                    query.save()

        #Registering students, maximum number of students 25
        #Returns an error if information entered incorrectly         

            for student in student_list:
                student.delete()

            for i in range (5*compadmin.admin_number_of_individuals()):
                if form.getlist('firstname','')[i] == u'': continue
                firstname =  correctCapitals(form.getlist('firstname','')[i])
                surname =  correctCapitals(form.getlist('surname','')[i])
                language =  form.getlist('language','')[0]
                school = assigned_school
                grade = form.getlist('grade','')[i]
                reference = '%3s%2s%2s'%(str(school.id).zfill(3),str(grade).zfill(2),str(i%5+1).zfill(2))
                #registered_by =  User.objects.get(pk=int(form.getlist('registered_by','')[i]))
                paired = False 

                query = SchoolStudent(firstname = firstname , surname = surname, language = language,reference = reference,
                        school = school, grade = grade , paired = paired)

                query.save()

            if 'submit_form' in request.POST: #Send confirmation email and continue
                assigned_school.entered=1 #The school has made an entry
                assigned_school.save()
                 #The school has made an entry
                confirmation.send_confirmation(request, assigned_school,cc_admin=True)
                return HttpResponseRedirect('../submitted.html')
            else:
                print 'This should not happen'

        except Exception as e:
            error = "%s: Incorrect information inserted into fields. Please insert correct information" % e
    else:
        form = StudentForm() # An unbound form

    if responsible_teacher:
        responsible_teacher = responsible_teacher[0]
    else:
        #If not null, then the form has been filled out.
        #Therefore - redirect to entry_review page
        pass #HttpResponseRedirect('../entry_review.html')

    c = {'type':'Students',
        'schooln':assigned_school,
        'language_options':language_selection,
        'responsible_teacher':responsible_teacher,
        'student_list':individual_list,
        'pairs_per_grade':pairs_per_grade,
        'pair_range':pairs_per_grade,
        'entries_per_grade':entries_per_grade,
        'invigilator_list': invigilator_list,
        'entries_open':compadmin.isOpen(),
        'grades':range(8,13), 
        'error':error,
        'invigilator_range':range(10-len(invigilator_list)), 
        'igrades':range(8,13),
        'editEntry':editEntry,
        'ierror':error}

    c.update(csrf(request))
    #TODO Cancel button (Go back to 'Entry Review' - if possible)
    return render_to_response('newstudents.html', c, context_instance=RequestContext(request))
Esempio n. 4
0
def newstudents(request):
    error = " "
    try:
        #Attempt to find user's chosen school
        assigned_school = School.objects.get(assigned_to=request.user)
    except exceptions.ObjectDoesNotExist:
        # No school is associated with this user! Redirect to the select_schools page
        return HttpResponseRedirect('../school_select/school_select.html')

    #NOTE: School.objects.get(pk=int(form.getlist('school','')[0])) was previously used to get school from drop-down menu

    #Required that school form is pre-fetched to populate form
    student_list = SchoolStudent.objects.filter(school=assigned_school)
    individual_list, pair_list = compadmin.processGrade(
        student_list)  #processGrade is defined below this method
    invigilator_list = Invigilator.objects.filter(school=assigned_school)
    responsible_teacher = ResponsibleTeacher.objects.filter(
        school=assigned_school).filter(is_primary=True)
    alt_responsible_teacher = ResponsibleTeacher.objects.filter(
        school=assigned_school).filter(is_primary=False)

    editEntry = False
    language_selection_options = ['e', 'a', 'b']

    if (responsible_teacher or alt_responsible_teacher):
        editEntry = True

    if assigned_school and (responsible_teacher or alt_responsible_teacher):
        language_temp = assigned_school.language
        language_selection = [language_temp]
        language_selection.extend(
            [c for c in language_selection_options if c != language_temp])
    else:
        language_selection = ['e', 'a', 'b']

    entries_per_grade = {}  #Dictionary with grade:range(...)
    pairs_per_grade = {}
    for grade in range(8, 13):
        entries_per_grade[grade] = range(
            compadmin.admin_number_of_individuals() -
            len(individual_list[grade]))
        #Place the "Previously Selected" number of pairs at the top of the list (So it appears as a default)
        pairs_per_grade[grade] = [pair_list[grade]]
        pairs_per_grade[grade].extend([
            i for i in range(0,
                             compadmin.admin_number_of_pairs() + 1)
            if i != pair_list[grade]
        ])

    if request.method == 'POST':  # If the form has been submitted...

        form = (request.POST)  # A form bound to the POST data

        #Delete all previously stored information

        try:
            assigned_school.address = '%s, %s, %s' % (form.getlist(
                'physical_address', '')[0], form.getlist(
                    'code', '')[0], form.getlist('city', '')[0])
            assigned_school.language = form.getlist('language', '')[0]
            assigned_school.phone = form.getlist('school_number', '')[0]
            assigned_school.save()

            #Register a single responsible teacher (assigned to that school)
            rtschool = assigned_school  #School.objects.get(pk=int(form.getlist('school','')[0]))
            rtfirstname = form.getlist('rt_firstname', '')[0]
            rtsurname = form.getlist('rt_surname', '')[0]
            rtphone_primary = form.getlist('rt_phone_primary',
                                           '')[0].strip().replace(' ', '')
            rtphone_alt = form.getlist('rt_phone_alt',
                                       '')[0].strip().replace(' ', '')
            rtphone_cell = form.getlist('rt_phone_cell',
                                        '')[0].strip().replace(' ', '')
            rtemail_school = form.getlist('rt_email_school',
                                          '')[0].strip().replace(' ', '')
            rtemail_personal = form.getlist('rt_email_personal',
                                            '')[0].strip().replace(' ', '')

            #rtregistered_by =  User.objects.get(pk=int(form.getlist('rt_registered_by','')[0]))
            rt_query = ResponsibleTeacher(firstname=rtfirstname,
                                          surname=rtsurname,
                                          phone_primary=rtphone_primary,
                                          phone_alt=rtphone_alt,
                                          phone_cell=rtphone_cell,
                                          school=rtschool,
                                          email_school=rtemail_school,
                                          email_personal=rtemail_personal,
                                          is_primary=True)

            #Delete responsible teacher before saving the new one
            for rt in responsible_teacher:
                rt.delete()

            rt_query.save()
            rt_query.reference = rt_query.id
            rt_query.save()

            #Register an alternate responsible teacher
            artschool = assigned_school  #School.objects.get(pk=int(form.getlist('school','')[0]))
            artfirstname = form.getlist('art_firstname', '')[0]
            artsurname = form.getlist('art_surname', '')[0]
            artphone_primary = form.getlist('art_phone_primary',
                                            '')[0].strip().replace(' ', '')
            artphone_alt = form.getlist('art_phone_alt',
                                        '')[0].strip().replace(' ', '')
            artphone_cell = form.getlist('art_phone_cell',
                                         '')[0].strip().replace(' ', '')
            artemail_school = form.getlist('art_email_school',
                                           '')[0].strip().replace(' ', '')
            artemail_personal = form.getlist('art_email_personal',
                                             '')[0].strip().replace(' ', '')

            #rtregistered_by =  User.objects.get(pk=int(form.getlist('rt_registered_by','')[0]))
            art_query = ResponsibleTeacher(firstname=artfirstname,
                                           surname=artsurname,
                                           phone_primary=artphone_primary,
                                           phone_alt=artphone_alt,
                                           phone_cell=artphone_cell,
                                           school=artschool,
                                           email_school=artemail_school,
                                           email_personal=artemail_personal,
                                           is_primary=False)

            #Delete responsible teacher before saving the new one
            for art in alt_responsible_teacher:
                art.delete()

            art_query.save()
            art_query.reference = art_query.id
            art_query.save()

            #Registering per grade
            for grade in range(8, 13):
                #Registering the different pairs
                #Information is set to null, only school name is given and reference
                #Reference if the ID of the first person in the pair

                if compadmin.admin_number_of_pairs() == 0:
                    break

                for p in range(int(form.getlist("pairs", '')[grade - 8])):
                    firstname = 'Pair/Paar'
                    surname = str(grade) + chr(
                        65 + p)  # Maps 0, 1, 2, 3... to A, B, C...
                    pair_number = 51 + p
                    language = form.getlist('language', '')[0]
                    school = assigned_school
                    reference = '%3s%2s%2s' % (str(
                        school.id).zfill(3), str(grade).zfill(2),
                                               str(pair_number).zfill(2))
                    paired = True
                    location = assigned_school.location

                    query = SchoolStudent(firstname=firstname,
                                          surname=surname,
                                          language=language,
                                          reference=reference,
                                          school=school,
                                          grade=grade,
                                          paired=paired,
                                          location=location)
                    query.save()

            #Add invigilator information
            for invigilator in invigilator_list:
                invigilator.delete()
            if compadmin.competition_has_invigilator():
                for j in range(10):
                    if form.getlist('inv_firstname', '')[j] == u'':
                        ierror = "Invigilator information incomplete"
                    else:
                        school = assigned_school
                        ifirstname = correctCapitals(
                            form.getlist('inv_firstname', '')[j])
                        isurname = correctCapitals(
                            form.getlist('inv_surname', '')[j])
                        iphone_primary = form.getlist('inv_phone_primary',
                                                      '')[j].strip().replace(
                                                          ' ', '')
                        iphone_alt = form.getlist('inv_phone_alt',
                                                  '')[j].strip().replace(
                                                      ' ', '')
                        iemail = form.getlist('inv_email',
                                              '')[j].strip().replace(' ', '')
                        inotes = form.getlist('inv_notes', '')[j].strip()
                        location = assigned_school.location

                        query = Invigilator(school=school,
                                            firstname=ifirstname,
                                            surname=isurname,
                                            location=location,
                                            phone_primary=iphone_primary,
                                            phone_alt=iphone_alt,
                                            email=iemail,
                                            notes=inotes)
                        query.save()

            #Registering students, maximum number of students 25
            #Returns an error if information entered incorrectly

            for student in student_list:
                student.delete()

            for i in range(5 * compadmin.admin_number_of_individuals()):
                if form.getlist('firstname', '')[i] == u'': continue
                firstname = correctCapitals(form.getlist('firstname', '')[i])
                surname = correctCapitals(form.getlist('surname', '')[i])
                ind_nr = (i % compadmin.admin_number_of_individuals()) + 1
                language = form.getlist('language', '')[0]
                school = assigned_school
                grade = form.getlist('grade', '')[i]
                reference = '%3s%2s%2s' % (str(
                    school.id).zfill(3), str(grade).zfill(2),
                                           str(ind_nr).zfill(2))
                paired = False
                location = assigned_school.location

                query = SchoolStudent(firstname=firstname,
                                      surname=surname,
                                      language=language,
                                      reference=reference,
                                      school=school,
                                      grade=grade,
                                      paired=paired,
                                      location=location)
                query.save()

            if 'submit_form' in request.POST:  #Send confirmation email and continue
                assigned_school.entered = 1  #The school has made an entry
                assigned_school.save()
                #The school has made an entry
                try:
                    confirmation.send_confirmation(request,
                                                   assigned_school,
                                                   cc_admin=True)
                    return HttpResponseRedirect('../submitted.html')
                except Exception as e:
                    print(e)
                    return HttpResponseRedirect('../submitted_noemail.html')
            else:
                print('This should not happen')

        except Exception as e:
            error = "%s: Incorrect information inserted into fields. Please insert correct information" % e
    else:
        form = StudentForm()  # An unbound form

    if responsible_teacher:
        responsible_teacher = responsible_teacher[0]
    else:
        responsible_teacher = None
        #If not null, then the form has been filled out.
        #Therefore - redirect to entry_review page
        pass  #HttpResponseRedirect('../entry_review.html')
    if alt_responsible_teacher:
        alt_responsible_teacher = alt_responsible_teacher[0]
    else:
        alt_responsible_teacher = None
    invigilators = compadmin.competition_has_invigilator()
    full = []
    if assigned_school.entered == 1:
        full = assigned_school.address.split(',')
    full += [''] * (3 - len(full))
    address = full[0].strip()
    code = full[1].strip()
    city = full[2].strip()

    c = {
        'type': 'Students',
        'schooln': assigned_school,
        'language_options': language_selection,
        'responsible_teacher': responsible_teacher,
        'alt_responsible_teacher': alt_responsible_teacher,
        'student_list': individual_list,
        'pairs_per_grade': pairs_per_grade,
        'pair_range': pairs_per_grade,
        'max_num_pairs': compadmin.admin_number_of_pairs(),
        'entries_per_grade': entries_per_grade,
        'invigilator_list': invigilator_list,
        'entries_open': compadmin.isOpen() or request.user.is_staff,
        'grades': range(8, 13),
        'error': error,
        'invigilator_range': range(10 - len(invigilator_list)),
        'igrades': range(8, 13),
        'editEntry': editEntry,
        'ierror': error,
        'invigilators': invigilators,
        'address': address,
        'code': code,
        'city': city,
        'maxEntries': compadmin.get_max_entries()
    }

    c.update(csrf(request))
    #TODO Cancel button (Go back to 'Entry Review' - if possible)
    return render_to_response('newstudents.html',
                              c,
                              context_instance=RequestContext(request))