예제 #1
0
def login_by_bday(request, *args, **kwargs):
    """ Let a student pick their school. """

    if request.user.is_authenticated():
        return registration_redirect(request)

    redirect_to = request.REQUEST.get(REDIRECT_FIELD_NAME,
                                      settings.DEFAULT_REDIRECT)
    redirect_str = u''
    if redirect_to:
        redirect_str = u'?%s=%s' % (REDIRECT_FIELD_NAME, redirect_to)

    if request.method == 'POST':
        form = BirthdaySelectForm(request.POST)
        if form.is_valid():
            month = form.cleaned_data['month']
            day = form.cleaned_data['day']
            return HttpResponseRedirect(u'/myesp/login/bybday/%s/%s/%s' %
                                        (month, day, redirect_str))
    else:
        form = BirthdaySelectForm()

    return render_to_response(
        'registration/login_by_bday.html', request,
        request.get_node('Q/Web/myesp'), {
            'form': form,
            'action': request.get_full_path(),
            'redirect_field_name': REDIRECT_FIELD_NAME,
            'next': redirect_to,
            'pwform': BarePasswordForm().as_table()
        })
예제 #2
0
def login_by_bday_pickname(request, month, day, *args, **kwargs):
    """ Let a student pick their name. """

    if request.user.is_authenticated():
        return registration_redirect(request)

    redirect_to = request.REQUEST.get(REDIRECT_FIELD_NAME,
                                      settings.DEFAULT_REDIRECT)
    redirect_str = u''
    if redirect_to:
        redirect_str = u'?%s=%s' % (REDIRECT_FIELD_NAME, redirect_to)

    if request.method == 'POST' and request.POST.has_key('username'):
        preset_username = request.POST['username']
        if preset_username == '-1':
            return HttpResponseRedirect('/myesp/register/')
        form = BarePasswordForm()
        action = '/myesp/login/'
    else:
        #   Add the birthday information to the user's session
        request.session['birth_month'] = month
        request.session['birth_day'] = day

        # Prepare a new student-select box
        user_filter = Q(registrationprofile__student_info__dob__month=month,
                        registrationprofile__student_info__dob__day=day)
        candidate_users = ESPUser.objects.filter(
            is_active=True).filter(user_filter).distinct().order_by(
                'first_name', 'id')
        form = StudentSelectForm(
            students=[(s.username, '%s (%s)' % (s.first_name, s.username))
                      for s in candidate_users] +
            [('-1', 'I don\'t see my name in this list...')])
        preset_username = ''
        action = request.get_full_path()
        if request.REQUEST.has_key('dynamic'):
            return HttpResponse(form.as_table())

    return render_to_response(
        'registration/login_by_bday_pickname.html', request,
        request.get_node('Q/Web/myesp'), {
            'form': form,
            'action': action,
            'redirect_field_name': REDIRECT_FIELD_NAME,
            'next': redirect_to,
            'preset_username': preset_username
        })
예제 #3
0
def login_byschool_pickname(request, school_id, *args, **kwargs):
    """ Let a student pick their name. """

    if request.user.is_authenticated():
        return registration_redirect(request)
    redirect_to = request.REQUEST.get(REDIRECT_FIELD_NAME,
                                      settings.DEFAULT_REDIRECT)
    redirect_str = u''
    if redirect_to:
        redirect_str = u'?%s=%s' % (REDIRECT_FIELD_NAME, redirect_to)

    # Get the school
    school_set = K12School.objects.filter(id=school_id)
    if school_set.count() < 1:
        return HttpResponseRedirect('/myesp/login/byschool/%s' % redirect_str)
    school = school_set[0]

    if request.method == 'POST' and request.POST.has_key('username'):
        preset_username = request.POST['username']
        if preset_username == '-1':
            return HttpResponseRedirect('/myesp/register/')
        form = BarePasswordForm()
        action = '/myesp/login/'
    else:
        # Prepare a new student-select box
        candidate_users = ESPUser.objects.filter(
            registrationprofile__student_info__k12school=school.id).distinct(
            ).order_by('first_name', 'id')
        form = StudentSelectForm(
            students=[(s.username, '%s (%s)' % (s.name(), s.username))
                      for s in candidate_users] +
            [('-1', 'I don\'t see my name in this list...')])
        preset_username = ''
        action = request.get_full_path()
        if request.REQUEST.has_key('dynamic'):
            return HttpResponse(form.as_table())

    return render_to_response(
        'registration/login_byschool_pickname.html', request, {
            'form': form,
            'action': action,
            'redirect_field_name': REDIRECT_FIELD_NAME,
            'next': redirect_to,
            'preset_username': preset_username
        })
예제 #4
0
def profile_editor(request, prog_input=None, responseuponCompletion = True, role=''):
    """ Display the registration profile page, the page that contains the contact information for a student, as attached to a particular program """

    from esp.users.models import K12School
    from esp.web.views.main import registration_redirect
    
    STUDREP_VERB = GetNode('V/Flags/UserRole/StudentRep')
    STUDREP_QSC  = GetNode('Q')


    if prog_input is None:
        prog = None
        navnode = GetNode('Q/Web/myesp')
    else:
        prog = prog_input
        navnode = prog.anchor

    curUser = request.user
    context = {'logged_in': request.user.is_authenticated() }
    context['user'] = request.user
    context['program'] = prog

    curUser = ESPUser(curUser)
    curUser.updateOnsite(request)

    #   Get the profile form from the user's type, although we need to handle 
    #   a couple of extra possibilities for the 'role' variable.
    user_types = ESPUser.getAllUserTypes()
    additional_types = [['',  {'label': 'Not specified', 'profile_form': 'UserContactForm'}],
                        ['Administrator', {'label': 'Administrator', 'profile_form': 'UserContactForm'}],
                       ]
    additional_type_labels = [x[0] for x in additional_types]
    #   Handle all-lowercase versions of role being passed in by calling title()
    user_type_labels = [x[0] for x in user_types]
    if role.title() in user_type_labels:
        target_type = user_types[user_type_labels.index(role.title())][1]
    else:
        target_type = additional_types[additional_type_labels.index(role.title())][1]
    mod = __import__('esp.users.forms.user_profile', (), (), target_type['profile_form'])
    FormClass = getattr(mod, target_type['profile_form'])

    context['profiletype'] = role

    if request.method == 'POST' and request.POST.has_key('profile_page'):
        form = FormClass(curUser, request.POST)

        # Don't suddenly demand an explanation from people who are already student reps
        if UserBit.objects.UserHasPerms(curUser, STUDREP_QSC, STUDREP_VERB):
            if hasattr(form, 'repress_studentrep_expl_error'):
                form.repress_studentrep_expl_error()

        if form.is_valid():
            new_data = form.cleaned_data

            regProf = RegistrationProfile.getLastForProgram(curUser, prog)

            if regProf.id is None:
                old_regProf = RegistrationProfile.getLastProfile(curUser)
            else:
                old_regProf = regProf

            for field_name in ['address_zip','address_city','address_street','address_state']:
                if field_name in new_data and new_data[field_name] != getattr(old_regProf.contact_user,field_name,False):
                    new_data['address_postal'] = ''

            if new_data['address_postal'] == '':
                new_data['address_postal'] = False

            regProf.contact_user = ContactInfo.addOrUpdate(regProf, new_data, regProf.contact_user, '', curUser)
            regProf.contact_emergency = ContactInfo.addOrUpdate(regProf, new_data, regProf.contact_emergency, 'emerg_')

            if new_data.has_key('dietary_restrictions') and new_data['dietary_restrictions']:
                regProf.dietary_restrictions = new_data['dietary_restrictions']

            if role == 'student':
                regProf.student_info = StudentInfo.addOrUpdate(curUser, regProf, new_data)
                regProf.contact_guardian = ContactInfo.addOrUpdate(regProf, new_data, regProf.contact_guardian, 'guard_')
            elif role == 'teacher':
                regProf.teacher_info = TeacherInfo.addOrUpdate(curUser, regProf, new_data)
            elif role == 'guardian':
                regProf.guardian_info = GuardianInfo.addOrUpdate(curUser, regProf, new_data)
            elif role == 'educator':
                regProf.educator_info = EducatorInfo.addOrUpdate(curUser, regProf, new_data)
            blah = regProf.__dict__
            regProf.save()

            curUser.first_name = new_data['first_name']
            curUser.last_name  = new_data['last_name']
            curUser.email     = new_data['e_mail']
            curUser.save()
            if responseuponCompletion == True:
                return registration_redirect(request)
            else:
                return True
        else:
            #   Force loading the school back in if possible...
            replacement_data = form.data.copy()
            try:
                replacement_data['k12school'] = form.fields['k12school'].clean(form.data['k12school']).id
            except:
                pass
            form = FormClass(curUser, replacement_data)

    else:
        if prog_input is None:
            regProf = RegistrationProfile.getLastProfile(curUser)
        else:
            regProf = RegistrationProfile.getLastForProgram(curUser, prog)
        if regProf.id is None:
            regProf = RegistrationProfile.getLastProfile(curUser)
        new_data = {}
        if curUser.isStudent():
            new_data['studentrep'] = (UserBit.objects.filter(user = curUser,
                                     verb = STUDREP_VERB,
                                     qsc  = STUDREP_QSC).count() > 0)
        new_data['first_name'] = curUser.first_name
        new_data['last_name']  = curUser.last_name
        new_data['e_mail']     = curUser.email
        new_data = regProf.updateForm(new_data, role)
        if request.session.has_key('birth_month') and request.session.has_key('birth_day'):
            new_data['dob'] = datetime.date(1994, int(request.session['birth_month']), int(request.session['birth_day']))
        if request.session.has_key('school_id'):
            new_data['k12school'] = request.session['school_id']

        #   Set default values for state fields
        state_fields = ['address_state', 'emerg_address_state']
        state_tag_map = {}
        for field in state_fields:
            state_tag_map[field] = 'local_state'
        form = FormClass(curUser, initial=new_data, tag_map=state_tag_map)

    context['request'] = request
    context['form'] = form
    return render_to_response('users/profile.html', request, navnode, context)
예제 #5
0
def profile_editor(request, prog_input=None, responseuponCompletion = True, role=''):
    """ Display the registration profile page, the page that contains the contact information for a student, as attached to a particular program """

    from esp.users.models import K12School
    from esp.web.views.main import registration_redirect

    if prog_input is None:
        prog = None
    else:
        prog = prog_input

    curUser = request.user
    context = {'logged_in': request.user.is_authenticated() }
    context['user'] = request.user
    context['program'] = prog

    curUser.updateOnsite(request)

    #   Get the profile form from the user's type, although we need to handle
    #   a couple of extra possibilities for the 'role' variable.
    user_types = ESPUser.getAllUserTypes()
    additional_types = [['',  {'label': 'Not specified', 'profile_form': 'UserContactForm'}],
                        ['Administrator', {'label': 'Administrator', 'profile_form': 'UserContactForm'}],
                       ]
    additional_type_labels = [x[0] for x in additional_types]
    #   Handle all-lowercase versions of role being passed in by calling title()
    user_type_labels = [x[0] for x in user_types]
    if role.title() in user_type_labels:
        target_type = user_types[user_type_labels.index(role.title())][1]
    else:
        target_type = additional_types[additional_type_labels.index(role.title())][1]
    mod = __import__('esp.users.forms.user_profile', (), (), target_type['profile_form'])
    FormClass = getattr(mod, target_type['profile_form'])

    context['profiletype'] = role

    if request.method == 'POST' and 'profile_page' in request.POST:
        form = FormClass(curUser, request.POST)

        # Don't suddenly demand an explanation from people who are already student reps
        if curUser.hasRole("StudentRep"):
            if hasattr(form, 'repress_studentrep_expl_error'):
                form.repress_studentrep_expl_error()

        if form.is_valid():
            new_data = form.cleaned_data

            regProf = RegistrationProfile.getLastForProgram(curUser, prog)

            if regProf.id is None:
                old_regProf = RegistrationProfile.getLastProfile(curUser)
            else:
                old_regProf = regProf

            for field_name in ['address_zip','address_city','address_street','address_state']:
                if field_name in new_data and new_data[field_name] != getattr(old_regProf.contact_user,field_name,False):
                    new_data['address_postal'] = ''

            if new_data['address_postal'] == '':
                new_data['address_postal'] = False

            regProf.contact_user = ContactInfo.addOrUpdate(regProf, new_data, regProf.contact_user, '', curUser)
            regProf.contact_emergency = ContactInfo.addOrUpdate(regProf, new_data, regProf.contact_emergency, 'emerg_')

            if new_data.get('dietary_restrictions'):
                regProf.dietary_restrictions = new_data['dietary_restrictions']

            if role == 'student':
                regProf.student_info = StudentInfo.addOrUpdate(curUser, regProf, new_data)
                regProf.contact_guardian = ContactInfo.addOrUpdate(regProf, new_data, regProf.contact_guardian, 'guard_')
            elif role == 'teacher':
                regProf.teacher_info = TeacherInfo.addOrUpdate(curUser, regProf, new_data)
            elif role == 'guardian':
                regProf.guardian_info = GuardianInfo.addOrUpdate(curUser, regProf, new_data)
            elif role == 'educator':
                regProf.educator_info = EducatorInfo.addOrUpdate(curUser, regProf, new_data)
            blah = regProf.__dict__
            regProf.save()

            curUser.first_name = new_data['first_name']
            curUser.last_name  = new_data['last_name']
            curUser.email     = new_data['e_mail']
            curUser.save()
            if responseuponCompletion == True:
                return registration_redirect(request)
            else:
                return True
        else:
            #   Force loading the school back in if possible...
            replacement_data = form.data.copy()
            try:
                replacement_data['k12school'] = form.fields['k12school'].clean(form.data['k12school']).id
            except:
                pass
            form = FormClass(curUser, replacement_data)

    else:
        if prog_input is None:
            regProf = RegistrationProfile.getLastProfile(curUser)
        else:
            regProf = RegistrationProfile.getLastForProgram(curUser, prog)
        if regProf.id is None:
            regProf = RegistrationProfile.getLastProfile(curUser)
        new_data = {}
        if curUser.isStudent():
            new_data['studentrep'] = curUser.groups.filter(name="StudentRep").exists()
        new_data['first_name'] = curUser.first_name
        new_data['last_name']  = curUser.last_name
        new_data['e_mail']     = curUser.email
        new_data = regProf.updateForm(new_data, role)

        if regProf.student_info and regProf.student_info.dob:
            new_data['dob'] = regProf.student_info.dob

        #   Set default values for state fields
        state_fields = ['address_state', 'emerg_address_state']
        state_tag_map = {}
        for field in state_fields:
            state_tag_map[field] = 'local_state'
        form = FormClass(curUser, initial=new_data, tag_map=state_tag_map)

    context['request'] = request
    context['form'] = form
    return render_to_response('users/profile.html', request, context)