예제 #1
0
def process_update_account_form(request, teacher, old_anchor):
    update_account_form = TeacherEditAccountForm(request.user, request.POST)
    changing_email = False
    new_email = ""
    if update_account_form.is_valid():
        data = update_account_form.cleaned_data

        # check not default value for CharField
        check_update_password(update_account_form, teacher.new_user, request,
                              data)

        teacher.title = data["title"]
        teacher.new_user.first_name = data["first_name"]
        teacher.new_user.last_name = data["last_name"]

        changing_email, new_email = update_email(teacher.new_user, request,
                                                 data)

        teacher.save()
        teacher.new_user.save()

        anchor = "#"

        messages.success(
            request, "Your account details have been successfully changed.")
    else:
        anchor = old_anchor

    return changing_email, new_email, anchor
예제 #2
0
def teacher_edit_account(request):
    teacher = request.user.userprofile.teacher

    backup_tokens = 0
    # For teachers using 2FA, find out how many backup tokens they have
    if default_device(request.user):
        try:
            backup_tokens = request.user.staticdevice_set.all()[0].token_set.count()
        except Exception:
            backup_tokens = 0

    if request.method == "POST":
        form = TeacherEditAccountForm(request.user, request.POST)
        if form.is_valid():
            data = form.cleaned_data
            changing_email = False

            # check not default value for CharField
            if data["password"] != "":
                teacher.user.user.set_password(data["password"])
                teacher.user.user.save()
                update_session_auth_hash(request, form.user)

            teacher.title = data["title"]
            teacher.user.user.first_name = data["first_name"]
            teacher.user.user.last_name = data["last_name"]
            new_email = data["email"]
            if new_email != "" and new_email != teacher.user.user.email:
                # new email to set and verify
                changing_email = True
                send_verification_email(request, teacher.user, new_email)

            teacher.save()
            teacher.user.user.save()

            if changing_email:
                logout(request)
                messages.success(
                    request,
                    "Your account details have been successfully changed. Your email will be changed once you have verified it, until then you can still log in with your old email.",
                )
                return render(
                    request, "portal/email_verification_needed.html", {"userprofile": teacher.user, "email": new_email}
                )

            messages.success(request, "Your account details have been successfully changed.")

            return HttpResponseRedirect(reverse_lazy("teacher_home"))
    else:
        form = TeacherEditAccountForm(
            request.user,
            initial={
                "title": teacher.title,
                "first_name": teacher.user.user.first_name,
                "last_name": teacher.user.user.last_name,
                "school": teacher.school,
            },
        )

    return render(request, "portal/teach/teacher_edit_account.html", {"form": form, "backup_tokens": backup_tokens})
예제 #3
0
def process_update_account_form(request, teacher, old_anchor):
    update_account_form = TeacherEditAccountForm(request.user, request.POST)
    changing_email = False
    new_email = ""
    if update_account_form.is_valid():
        data = update_account_form.cleaned_data
        changing_email = False

        # check not default value for CharField
        if (data['password'] != ''):
            teacher.new_user.set_password(data['password'])
            teacher.new_user.save()
            update_session_auth_hash(request, update_account_form.user)

        teacher.title = data['title']
        teacher.new_user.first_name = data['first_name']
        teacher.new_user.last_name = data['last_name']
        new_email = data['email']
        if new_email != '' and new_email != teacher.new_user.email:
            # new email to set and verify
            changing_email = True
            send_verification_email(request, teacher.new_user, new_email)

        teacher.save()
        teacher.new_user.save()

        anchor = '#'

        messages.success(
            request, 'Your account details have been successfully changed.')
    else:
        anchor = old_anchor

    return changing_email, new_email, anchor
def process_update_account_form(request, teacher, old_anchor):
    update_account_form = TeacherEditAccountForm(request.user, request.POST)
    changing_email = False
    new_email = ""
    if update_account_form.is_valid():
        data = update_account_form.cleaned_data
        changing_email = False

        # check not default value for CharField
        if data["password"] != "":
            teacher.new_user.set_password(data["password"])
            teacher.new_user.save()
            update_session_auth_hash(request, update_account_form.user)

        teacher.title = data["title"]
        teacher.new_user.first_name = data["first_name"]
        teacher.new_user.last_name = data["last_name"]
        new_email = data["email"]
        if new_email != "" and new_email != teacher.new_user.email:
            # new email to set and verify
            changing_email = True
            send_verification_email(request, teacher.new_user, new_email)

        teacher.save()
        teacher.new_user.save()

        anchor = "#"

        messages.success(
            request, "Your account details have been successfully changed."
        )
    else:
        anchor = old_anchor

    return changing_email, new_email, anchor
예제 #5
0
def teacher_edit_account(request):
    teacher = request.user.userprofile.teacher

    backup_tokens = 0
    # For teachers using 2FA, find out how many backup tokens they have
    if using_two_factor(request.user):
        try:
            backup_tokens = request.user.staticdevice_set.all()[0].token_set.count()
        except Exception:
            backup_tokens = 0

    if request.method == 'POST':
        form = TeacherEditAccountForm(request.user, request.POST)
        if form.is_valid():
            data = form.cleaned_data
            changing_email = False

            # check not default value for CharField
            if (data['password'] != ''):
                teacher.user.user.set_password(data['password'])
                teacher.user.user.save()
                update_session_auth_hash(request, form.user)

            teacher.title = data['title']
            teacher.user.user.first_name = data['first_name']
            teacher.user.user.last_name = data['last_name']
            new_email = data['email']
            if new_email != '' and new_email != teacher.user.user.email:
                    # new email to set and verify
                    changing_email = True
                    send_verification_email(request, teacher.user.user, new_email)

            teacher.save()
            teacher.user.user.save()

            if changing_email:
                logout(request)
                messages.success(request, 'Your account details have been successfully changed. Your email will be changed once you have verified it, until then you can still log in with your old email.')
                return render(request, 'portal/email_verification_needed.html',
                              {'userprofile': teacher.user,
                               'email': new_email})

            messages.success(request, 'Your account details have been successfully changed.')

            return HttpResponseRedirect(reverse_lazy('teacher_home'))
    else:
        form = TeacherEditAccountForm(request.user, initial={
            'title': teacher.title,
            'first_name': teacher.user.user.first_name,
            'last_name': teacher.user.user.last_name,
            'school': teacher.school,
        })

    return render(request, 'portal/teach/teacher_edit_account.html',
                  {'form': form,
                   'backup_tokens': backup_tokens})
예제 #6
0
def teacher_edit_account(request):
    teacher = request.user.new_teacher

    backup_tokens = 0
    # For teachers using 2FA, find out how many backup tokens they have
    if using_two_factor(request.user):
        try:
            backup_tokens = request.user.staticdevice_set.all(
            )[0].token_set.count()
        except Exception:
            backup_tokens = 0

    if request.method == 'POST':
        form = TeacherEditAccountForm(request.user, request.POST)
        if form.is_valid():
            data = form.cleaned_data
            changing_email = False

            # check not default value for CharField
            if (data['password'] != ''):
                teacher.new_user.set_password(data['password'])
                teacher.new_user.save()
                update_session_auth_hash(request, form.user)

            teacher.title = data['title']
            teacher.new_user.first_name = data['first_name']
            teacher.new_user.last_name = data['last_name']
            new_email = data['email']
            if new_email != '' and new_email != teacher.new_user.email:
                # new email to set and verify
                changing_email = True
                send_verification_email(request, teacher.new_user, new_email)

            teacher.save()
            teacher.new_user.save()

            if changing_email:
                logout(request)
                messages.success(
                    request,
                    'Your account details have been successfully changed. Your email will be changed once you have verified it, until then you can still log in with your old email.'
                )
                return render(request, 'portal/email_verification_needed.html',
                              {
                                  'userprofile': teacher.user,
                                  'email': new_email
                              })

            messages.success(
                request,
                'Your account details have been successfully changed.')

            return HttpResponseRedirect(reverse_lazy('teacher_home'))
    else:
        form = TeacherEditAccountForm(request.user,
                                      initial={
                                          'title': teacher.title,
                                          'first_name':
                                          teacher.new_user.first_name,
                                          'last_name':
                                          teacher.new_user.last_name,
                                          'school': teacher.school,
                                      })

    return render(request, 'portal/teach/teacher_edit_account.html', {
        'form': form,
        'backup_tokens': backup_tokens
    })
예제 #7
0
def dashboard_teacher_view(request, is_admin):
    teacher = request.user.new_teacher
    school = teacher.school

    coworkers = Teacher.objects.filter(school=school).order_by(
        "new_user__last_name", "new_user__first_name")

    join_requests = Teacher.objects.filter(
        pending_join_request=school).order_by("new_user__last_name",
                                              "new_user__first_name")
    requests = Student.objects.filter(pending_class_request__teacher=teacher)

    update_school_form = OrganisationForm(user=request.user,
                                          current_school=school)
    update_school_form.fields["name"].initial = school.name
    update_school_form.fields["postcode"].initial = school.postcode
    update_school_form.fields["country"].initial = school.country

    create_class_form = ClassCreationForm()

    update_account_form = TeacherEditAccountForm(request.user)
    update_account_form.fields["title"].initial = teacher.title
    update_account_form.fields["first_name"].initial = request.user.first_name
    update_account_form.fields["last_name"].initial = request.user.last_name

    anchor = ""

    backup_tokens = check_backup_tokens(request)

    if request.method == "POST":
        if can_process_update_school_form(request, is_admin):
            anchor = "school-details"
            update_school_form = OrganisationForm(request.POST,
                                                  user=request.user,
                                                  current_school=school)
            anchor = process_update_school_form(request, school, anchor)

        elif "create_class" in request.POST:
            anchor = "new-class"
            create_class_form = ClassCreationForm(request.POST)
            if create_class_form.is_valid():
                created_class = create_class_new(create_class_form, teacher)
                messages.success(
                    request,
                    "The class '{className}' has been created successfully.".
                    format(className=created_class.name),
                )
                return HttpResponseRedirect(
                    reverse_lazy(
                        "view_class",
                        kwargs={"access_code": created_class.access_code}))

        else:
            anchor = "account"
            update_account_form = TeacherEditAccountForm(
                request.user, request.POST)
            changing_email, new_email, anchor = process_update_account_form(
                request, teacher, anchor)
            if changing_email:
                logout(request)
                messages.success(
                    request,
                    "Your account details have been successfully changed. Your email "
                    "will be changed once you have verified it, until then you can "
                    "still log in with your old email.",
                )
                return render(
                    request,
                    "portal/email_verification_needed.html",
                    {
                        "userprofile": teacher.user,
                        "email": new_email
                    },
                )

    classes = Class.objects.filter(teacher=teacher)

    return render(
        request,
        "portal/teach/dashboard.html",
        {
            "teacher": teacher,
            "classes": classes,
            "is_admin": is_admin,
            "coworkers": coworkers,
            "join_requests": join_requests,
            "requests": requests,
            "update_school_form": update_school_form,
            "create_class_form": create_class_form,
            "update_account_form": update_account_form,
            "anchor": anchor,
            "backup_tokens": backup_tokens,
        },
    )
예제 #8
0
def dashboard_teacher_view(request, is_admin):
    teacher = request.user.new_teacher
    school = teacher.school

    coworkers = Teacher.objects.filter(school=school).order_by(
        'new_user__last_name', 'new_user__first_name')

    join_requests = Teacher.objects.filter(
        pending_join_request=school).order_by('new_user__last_name',
                                              'new_user__first_name')
    requests = Student.objects.filter(pending_class_request__teacher=teacher)

    update_school_form = OrganisationForm(user=request.user,
                                          current_school=school)
    update_school_form.fields['name'].initial = school.name
    update_school_form.fields['postcode'].initial = school.postcode
    update_school_form.fields['country'].initial = school.country

    create_class_form = ClassCreationForm()

    update_account_form = TeacherEditAccountForm(request.user)
    update_account_form.fields['title'].initial = teacher.title
    update_account_form.fields['first_name'].initial = request.user.first_name
    update_account_form.fields['last_name'].initial = request.user.last_name

    anchor = ''

    backup_tokens = check_backup_tokens(request)

    if can_process_forms(request, is_admin):
        if 'update_school' in request.POST:
            anchor = 'school-details'
            update_school_form = OrganisationForm(request.POST,
                                                  user=request.user,
                                                  current_school=school)
            anchor = process_update_school_form(request, school, anchor)

        elif 'create_class' in request.POST:
            anchor = 'new-class'
            create_class_form = ClassCreationForm(request.POST)
            if create_class_form.is_valid():
                created_class = create_class_new(create_class_form, teacher)
                messages.success(
                    request,
                    "The class '{className}' has been created successfully.".
                    format(className=created_class.name))
                return HttpResponseRedirect(
                    reverse_lazy(
                        'view_class',
                        kwargs={'access_code': created_class.access_code}))

        else:
            anchor = 'account'
            update_account_form = TeacherEditAccountForm(
                request.user, request.POST)
            changing_email, new_email, anchor = process_update_account_form(
                request, teacher, anchor)
            if changing_email:
                logout(request)
                messages.success(
                    request,
                    'Your account details have been successfully changed. Your email will be changed once you have verified it, until then you can still log in with your old email.'
                )
                return render(request, 'portal/email_verification_needed.html',
                              {
                                  'userprofile': teacher.user,
                                  'email': new_email
                              })

    classes = Class.objects.filter(teacher=teacher)

    return render(
        request, 'portal/teach/dashboard.html', {
            'teacher': teacher,
            'classes': classes,
            'is_admin': is_admin,
            'coworkers': coworkers,
            'join_requests': join_requests,
            'requests': requests,
            'update_school_form': update_school_form,
            'create_class_form': create_class_form,
            'update_account_form': update_account_form,
            'anchor': anchor,
            'backup_tokens': backup_tokens,
        })
예제 #9
0
def dashboard_teacher_view(request, is_admin):
    teacher = request.user.new_teacher
    school = teacher.school

    coworkers = Teacher.objects.filter(school=school).order_by(
        'new_user__last_name', 'new_user__first_name')

    join_requests = Teacher.objects.filter(
        pending_join_request=school).order_by('new_user__last_name',
                                              'new_user__first_name')

    update_school_form = OrganisationForm(user=request.user,
                                          current_school=school)
    update_school_form.fields['name'].initial = school.name
    update_school_form.fields['postcode'].initial = school.postcode
    update_school_form.fields['country'].initial = school.country

    create_class_form = ClassCreationForm()

    update_account_form = TeacherEditAccountForm(request.user)
    update_account_form.fields['title'].initial = teacher.title
    update_account_form.fields['first_name'].initial = request.user.first_name
    update_account_form.fields['last_name'].initial = request.user.last_name

    anchor = ''

    if can_process_forms(request, is_admin):
        if 'update_school' in request.POST:
            anchor = 'school-details'
            update_school_form = OrganisationForm(request.POST,
                                                  user=request.user,
                                                  current_school=school)
            process_update_school_form(request, school)

        elif 'create_class' in request.POST:
            anchor = 'new-class'
            create_class_form = ClassCreationForm(request.POST)
            process_create_class_form(request, teacher)

        else:
            anchor = 'account'
            update_account_form = TeacherEditAccountForm(
                request.user, request.POST)
            changing_email, new_email = process_update_account_form(
                request, teacher)
            if changing_email:
                logout(request)
                messages.success(
                    request,
                    'Your account details have been successfully changed. Your email will be changed once you have verified it, until then you can still log in with your old email.'
                )
                return render(request,
                              'redesign/email_verification_needed_new.html', {
                                  'userprofile': teacher.user,
                                  'email': new_email
                              })

    classes = Class.objects.filter(teacher=teacher)

    return render(
        request, 'redesign/teach_new/dashboard.html', {
            'teacher': teacher,
            'classes': classes,
            'is_admin': is_admin,
            'coworkers': coworkers,
            'join_requests': join_requests,
            'update_school_form': update_school_form,
            'create_class_form': create_class_form,
            'update_account_form': update_account_form,
            'anchor': anchor,
        })