def organisation_toggle_admin(request, pk):
    teacher = get_object_or_404(Teacher, id=pk)
    user = request.user.userprofile.teacher

    # check user has authority to change
    if teacher.school != user.school or not user.is_admin:
        raise Http404

    # check not trying to change self
    if user == teacher:
        raise Http404

    teacher.is_admin = not teacher.is_admin
    teacher.save()

    if teacher.is_admin:
        messages.success(request, 'Administrator status has been given successfully.')
        emailMessage = emailMessages.adminGivenEmail(request, teacher.school.name)
    else:
        messages.success(request, 'Administrator status has been revoked successfully.')
        emailMessage = emailMessages.adminRevokedEmail(request, teacher.school.name)

    send_email(NOTIFICATION_EMAIL, [teacher.user.user.email], emailMessage['subject'], emailMessage['message'])

    return HttpResponseRedirect(reverse_lazy('organisation_manage'))
示例#2
0
def organisation_toggle_admin(request, pk):
    teacher = get_object_or_404(Teacher, id=pk)
    user = request.user.userprofile.teacher

    # check user has authority to change
    if teacher.school != user.school or not user.is_admin:
        raise Http404

    # check not trying to change self
    if user == teacher:
        raise Http404

    teacher.is_admin = not teacher.is_admin
    teacher.save()

    if teacher.is_admin:
        messages.success(request,
                         'Administrator status has been given successfully.')
        emailMessage = emailMessages.adminGivenEmail(request,
                                                     teacher.school.name)
    else:
        messages.success(
            request, 'Administrator status has been revoked successfully.')
        emailMessage = emailMessages.adminRevokedEmail(request,
                                                       teacher.school.name)

    send_email(NOTIFICATION_EMAIL, [teacher.user.user.email],
               emailMessage['subject'], emailMessage['message'])

    return HttpResponseRedirect(reverse_lazy('organisation_manage'))
def teacher_reject_student_request(request, pk):
    student = get_object_or_404(Student, id=pk)

    # check student is awaiting decision on request
    if not student.pending_class_request:
        raise Http404

    # check user (teacher) has authority to reject student
    if request.user.userprofile.teacher != student.pending_class_request.teacher:
        raise Http404

    emailMessage = emailMessages.studentJoinRequestRejectedEmail(
        request, student.pending_class_request.teacher.school.name,
        student.pending_class_request.access_code)
    send_email(NOTIFICATION_EMAIL, [student.user.user.email],
               emailMessage['subject'], emailMessage['message'])

    student.pending_class_request = None
    student.save()

    messages.success(
        request,
        'Request from external/independent student has been rejected successfully.'
    )

    return HttpResponseRedirect(reverse_lazy('teacher_classes'))
def student_join_organisation(request):
    increment_count = False
    limits = getattr(request, 'limits', { 'ip': [0] })
    captcha_limit = 5

    using_captcha = (limits['ip'][0] > captcha_limit)
    should_use_captcha = (limits['ip'][0] >= captcha_limit)

    StudentJoinOrganisationFormWithCaptcha = partial(create_form_subclass_with_recaptcha(StudentJoinOrganisationForm, recaptcha_client), request)
    InputStudentJoinOrganisationForm = StudentJoinOrganisationFormWithCaptcha if using_captcha else StudentJoinOrganisationForm
    OutputStudentJoinOrganisationForm = StudentJoinOrganisationFormWithCaptcha if should_use_captcha else StudentJoinOrganisationForm

    student = request.user.userprofile.student
    request_form = OutputStudentJoinOrganisationForm()

    # check student not managed by a school
    if student.class_field:
        raise Http404

    if request.method == 'POST':
        if 'class_join_request' in request.POST:
            increment_count = True
            request_form = InputStudentJoinOrganisationForm(request.POST)
            if request_form.is_valid():
                student.pending_class_request = request_form.klass
                student.save()

                emailMessage = emailMessages.studentJoinRequestSentEmail(request, request_form.klass.teacher.school.name, request_form.klass.access_code)
                send_email(NOTIFICATION_EMAIL, [student.user.user.email], emailMessage['subject'], emailMessage['message'])

                emailMessage = emailMessages.studentJoinRequestNotifyEmail(request, student.user.user.username, student.user.user.email, student.pending_class_request.access_code)
                send_email(NOTIFICATION_EMAIL, [student.pending_class_request.teacher.user.user.email], emailMessage['subject'], emailMessage['message'])

                messages.success(request, 'Your request to join a school has been received successfully.')

            else:
                request_form = OutputStudentJoinOrganisationForm(request.POST)

        elif 'revoke_join_request' in request.POST:
            student.pending_class_request = None
            student.save()
            # Check teacher hasn't since accepted rejection before posting success message
            if not student.class_field:
                messages.success(request, 'Your request to join a school has been cancelled successfully.')
            return HttpResponseRedirect(reverse_lazy('student_edit_account'))

    res = render(request, 'portal/play/student_join_organisation.html', {
        'request_form': request_form,
        'student': student }
    )

    res.count = increment_count
    return res
示例#5
0
def organisation_kick(request, pk):
    teacher = get_object_or_404(Teacher, id=pk)
    user = request.user.userprofile.teacher

    # check not trying to kick self
    if teacher == user:
        raise Http404

    # check authorised to kick teacher
    if teacher.school != user.school or not user.is_admin:
        raise Http404

    if request.method == 'POST':
        classes = Class.objects.filter(teacher=teacher)
        for klass in classes:
            teacher_id = request.POST.get(klass.access_code, None)
            if teacher_id:
                new_teacher = get_object_or_404(Teacher, id=teacher_id)
                klass.teacher = new_teacher
                klass.save()

    classes = Class.objects.filter(teacher=teacher)
    teachers = Teacher.objects.filter(school=teacher.school).exclude(
        id=teacher.id)

    if classes.exists():
        messages.info(
            request,
            'This teacher still has classes assigned to them. You must first move them to another teacher in your school or club.'
        )
        return render(
            request, 'portal/teach/teacher_move_all_classes.html', {
                'original_teacher': teacher,
                'classes': classes,
                'teachers': teachers,
                'submit_button_text': 'Remove teacher',
            })

    teacher.school = None
    teacher.save()

    messages.success(
        request,
        'The teacher has been successfully removed from your school or club.')

    emailMessage = emailMessages.kickedEmail(request, user.school.name)

    send_email(NOTIFICATION_EMAIL, [teacher.user.user.email],
               emailMessage['subject'], emailMessage['message'])

    return HttpResponseRedirect(reverse_lazy('organisation_manage'))
def contact(request):
    increment_count = False
    limits = getattr(request, 'limits', {'ip': [0]})
    captcha_limit = 5

    using_captcha = (limits['ip'][0] > captcha_limit)
    should_use_captcha = (limits['ip'][0] >= captcha_limit)

    ContactFormWithCaptcha = partial(
        create_form_subclass_with_recaptcha(ContactForm, recaptcha_client),
        request)
    InputContactForm = ContactFormWithCaptcha if using_captcha else ContactForm
    OutputContactForm = ContactFormWithCaptcha if should_use_captcha else ContactForm

    if request.method == 'POST':
        contact_form = InputContactForm(request.POST)
        increment_count = True

        if contact_form.is_valid():
            emailMessage = emailMessages.contactEmail(
                request, contact_form.cleaned_data['name'],
                contact_form.cleaned_data['telephone'],
                contact_form.cleaned_data['email'],
                contact_form.cleaned_data['message'],
                contact_form.cleaned_data['browser'])
            send_email(CONTACT_EMAIL, CONTACT_FORM_EMAILS,
                       emailMessage['subject'], emailMessage['message'])

            confirmedEmailMessage = emailMessages.confirmationContactEmailMessage(
                request, contact_form.cleaned_data['name'],
                contact_form.cleaned_data['telephone'],
                contact_form.cleaned_data['email'],
                contact_form.cleaned_data['message'])
            send_email(CONTACT_EMAIL, [contact_form.cleaned_data['email']],
                       confirmedEmailMessage['subject'],
                       confirmedEmailMessage['message'])

            messages.success(request, 'Your message was sent successfully.')
            return HttpResponseRedirect('.')

        else:
            contact_form = OutputContactForm(request.POST)

    else:
        contact_form = OutputContactForm()

    response = render(request, 'portal/contact.html', {'form': contact_form})

    response.count = increment_count
    return response
def organisation_deny_join(request, pk):
    teacher = get_object_or_404(Teacher, id=pk)
    user = request.user.userprofile.teacher

    # check user has authority to accept teacher
    if teacher.pending_join_request != user.school or not user.is_admin:
        raise Http404

    teacher.pending_join_request = None
    teacher.save()

    messages.success(request, 'The request to join your school or club has been successfully denied.')

    emailMessage = emailMessages.joinRequestDeniedEmail(request, request.user.userprofile.teacher.school.name)
    send_email(NOTIFICATION_EMAIL, [teacher.user.user.email], emailMessage['subject'], emailMessage['message'])

    return HttpResponseRedirect(reverse_lazy('organisation_manage'))
def send_new_users_report(request):
    new_users_count = User.objects.filter(date_joined__gte=timezone.now() - timedelta(days=7)).count()
    users_count = User.objects.count()
    school_count = School.objects.count()
    teacher_count = Teacher.objects.count()
    student_count = Student.objects.count()
    schools_countries = School.objects.values('country').annotate(nb_countries=Count('id')).order_by('-nb_countries')
    countries_count = "\n".join('{}: {}'.format(dict(countries)[k["country"]], k["nb_countries"]) for k in schools_countries)
    send_email(NOTIFICATION_EMAIL, CONTACT_FORM_EMAILS, "new users",
               'There are {new_users} new users this week!\n'
               'The total number of registered users is now: {users}\n'
               'Current number of schools: {schools}\n'
               'Current number of teachers: {teachers}\n'
               'Current number of students: {students}\n'
               'Schools per country:\n{countries_counter}'
               .format(new_users=new_users_count, users=users_count, schools=school_count, teachers=teacher_count,
                       students=student_count, countries_counter=countries_count))
    return HttpResponse('success')
示例#9
0
def contact(request):
    increment_count = False
    limits = getattr(request, 'limits', {'ip': [0]})
    captcha_limit = 5

    using_captcha = (limits['ip'][0] > captcha_limit)
    should_use_captcha = (limits['ip'][0] >= captcha_limit)

    ContactFormWithCaptcha = partial(
        create_form_subclass_with_recaptcha(ContactForm, recaptcha_client), request)
    InputContactForm = ContactFormWithCaptcha if using_captcha else ContactForm
    OutputContactForm = ContactFormWithCaptcha if should_use_captcha else ContactForm

    if request.method == 'POST':
        contact_form = InputContactForm(request.POST)
        increment_count = True

        if contact_form.is_valid():
            emailMessage = emailMessages.contactEmail(
                request, contact_form.cleaned_data['name'], contact_form.cleaned_data['telephone'],
                contact_form.cleaned_data['email'], contact_form.cleaned_data['message'],
                contact_form.cleaned_data['browser'])
            send_email(CONTACT_EMAIL, CONTACT_FORM_EMAILS, emailMessage['subject'],
                       emailMessage['message'])

            confirmedEmailMessage = emailMessages.confirmationContactEmailMessage(
                request, contact_form.cleaned_data['name'], contact_form.cleaned_data['telephone'],
                contact_form.cleaned_data['email'], contact_form.cleaned_data['message'])
            send_email(CONTACT_EMAIL, [contact_form.cleaned_data['email']],
                       confirmedEmailMessage['subject'], confirmedEmailMessage['message'])

            messages.success(request, 'Your message was sent successfully.')
            return HttpResponseRedirect('.')

        else:
            contact_form = OutputContactForm(request.POST)

    else:
        contact_form = OutputContactForm()

    response = render(request, 'portal/contact.html', {'form': contact_form})

    response.count = increment_count
    return response
def organisation_kick(request, pk):
    teacher = get_object_or_404(Teacher, id=pk)
    user = request.user.userprofile.teacher

    # check not trying to kick self
    if teacher == user:
        raise Http404

    # check authorised to kick teacher
    if teacher.school != user.school or not user.is_admin:
        raise Http404

    if request.method == 'POST':
        classes = Class.objects.filter(teacher=teacher)
        for klass in classes:
            teacher_id = request.POST.get(klass.access_code, None)
            if teacher_id:
                new_teacher = get_object_or_404(Teacher, id=teacher_id)
                klass.teacher = new_teacher
                klass.save()

    classes = Class.objects.filter(teacher=teacher)
    teachers = Teacher.objects.filter(school=teacher.school).exclude(id=teacher.id)

    if classes.exists():
        messages.info(request, 'This teacher still has classes assigned to them. You must first move them to another teacher in your school or club.')
        return render(request, 'portal/teach/teacher_move_all_classes.html', {
            'original_teacher': teacher,
            'classes': classes,
            'teachers': teachers,
            'submit_button_text': 'Remove teacher',
        })

    teacher.school = None
    teacher.save()

    messages.success(request, 'The teacher has been successfully removed from your school or club.')

    emailMessage = emailMessages.kickedEmail(request, user.school.name)

    send_email(NOTIFICATION_EMAIL, [teacher.user.user.email], emailMessage['subject'], emailMessage['message'])

    return HttpResponseRedirect(reverse_lazy('organisation_manage'))
示例#11
0
def teacher_reject_student_request(request, pk):
    student = get_object_or_404(Student, id=pk)

    # check student is awaiting decision on request
    if not student.pending_class_request:
        raise Http404

    # check user (teacher) has authority to reject student
    if request.user.userprofile.teacher != student.pending_class_request.teacher:
        raise Http404

    emailMessage = emailMessages.studentJoinRequestRejectedEmail(request, student.pending_class_request.teacher.school.name, student.pending_class_request.access_code)
    send_email(NOTIFICATION_EMAIL, [student.user.user.email], emailMessage['subject'], emailMessage['message'])

    student.pending_class_request = None
    student.save()

    messages.success(request, 'Request from external/independent student has been rejected successfully.')

    return HttpResponseRedirect(reverse_lazy('teacher_classes'))
示例#12
0
def organisation_deny_join(request, pk):
    teacher = get_object_or_404(Teacher, id=pk)
    user = request.user.userprofile.teacher

    # check user has authority to accept teacher
    if teacher.pending_join_request != user.school or not user.is_admin:
        raise Http404

    teacher.pending_join_request = None
    teacher.save()

    messages.success(
        request,
        'The request to join your school or club has been successfully denied.'
    )

    emailMessage = emailMessages.joinRequestDeniedEmail(
        request, request.user.userprofile.teacher.school.name)
    send_email(NOTIFICATION_EMAIL, [teacher.user.user.email],
               emailMessage['subject'], emailMessage['message'])

    return HttpResponseRedirect(reverse_lazy('organisation_manage'))
示例#13
0
def send_new_users_report(request):
    new_profiles_count = UserProfile.objects.filter(user__date_joined__gte=timezone.now() - timedelta(days=7)).count()
    send_email(NOTIFICATION_EMAIL, CONTACT_FORM_EMAILS, "new users",
               "There are %d new users this week!" % new_profiles_count)
    return HttpResponse('success')
示例#14
0
def organisation_create(request):
    increment_count = False
    limits = getattr(request, 'limits', {'ip': [0]})
    captcha_limit = 5

    using_captcha = (limits['ip'][0] > captcha_limit)
    should_use_captcha = (limits['ip'][0] >= captcha_limit)

    OrganisationJoinFormWithCaptcha = partial(
        create_form_subclass_with_recaptcha(OrganisationJoinForm,
                                            recaptcha_client), request)
    InputOrganisationJoinForm = OrganisationJoinFormWithCaptcha if using_captcha else OrganisationJoinForm
    OutputOrganisationJoinForm = OrganisationJoinFormWithCaptcha if should_use_captcha else OrganisationJoinForm

    teacher = request.user.userprofile.teacher

    create_form = OrganisationForm(user=request.user)
    join_form = OutputOrganisationJoinForm()

    if request.method == 'POST':
        if 'create_organisation' in request.POST:
            create_form = OrganisationForm(request.POST, user=request.user)
            if create_form.is_valid():
                data = create_form.cleaned_data
                name = data.get('name', '')
                postcode = data.get('postcode', '')
                country = data.get('country', '')

                error, town, lat, lng = '', '0', '0', '0'  #lookup_coord(postcode, country)

                school = School.objects.create(name=name,
                                               postcode=postcode,
                                               town=town,
                                               latitude=lat,
                                               longitude=lng,
                                               country=country)

                teacher.school = school
                teacher.is_admin = True
                teacher.save()

                messages.success(
                    request, "The school or club '" + teacher.school.name +
                    "' has been successfully added.")

                return HttpResponseRedirect(reverse_lazy('teacher_home'))

        elif 'join_organisation' in request.POST:
            increment_count = True
            join_form = InputOrganisationJoinForm(request.POST)
            if join_form.is_valid():
                school = get_object_or_404(
                    School, id=join_form.cleaned_data['chosen_org'])

                teacher.pending_join_request = school
                teacher.save()

                emailMessage = emailMessages.joinRequestPendingEmail(
                    request, teacher.user.user.email)

                for admin in Teacher.objects.filter(school=school,
                                                    is_admin=True):
                    send_email(NOTIFICATION_EMAIL, [admin.user.user.email],
                               emailMessage['subject'],
                               emailMessage['message'])

                emailMessage = emailMessages.joinRequestSentEmail(
                    request, school.name)
                send_email(NOTIFICATION_EMAIL, [teacher.user.user.email],
                           emailMessage['subject'], emailMessage['message'])

                messages.success(
                    request,
                    'Your request to join the school or club has been sent successfully.'
                )

            else:
                join_form = OutputOrganisationJoinForm(request.POST)

        elif 'revoke_join_request' in request.POST:
            teacher.pending_join_request = None
            teacher.save()

            messages.success(
                request,
                'Your request to join the school or club has been revoked successfully.'
            )

    res = render(request, 'portal/teach/organisation_create.html', {
        'create_form': create_form,
        'join_form': join_form,
        'teacher': teacher,
    })

    res.count = increment_count
    return res
示例#15
0
def student_join_organisation(request):
    increment_count = False
    limits = getattr(request, 'limits', {'ip': [0]})
    captcha_limit = 5

    using_captcha = (limits['ip'][0] > captcha_limit)
    should_use_captcha = (limits['ip'][0] >= captcha_limit)

    StudentJoinOrganisationFormWithCaptcha = partial(
        create_form_subclass_with_recaptcha(StudentJoinOrganisationForm,
                                            recaptcha_client), request)
    InputStudentJoinOrganisationForm = StudentJoinOrganisationFormWithCaptcha if using_captcha else StudentJoinOrganisationForm
    OutputStudentJoinOrganisationForm = StudentJoinOrganisationFormWithCaptcha if should_use_captcha else StudentJoinOrganisationForm

    student = request.user.userprofile.student
    request_form = OutputStudentJoinOrganisationForm()

    # check student not managed by a school
    if student.class_field:
        raise Http404

    if request.method == 'POST':
        if 'class_join_request' in request.POST:
            increment_count = True
            request_form = InputStudentJoinOrganisationForm(request.POST)
            if request_form.is_valid():
                student.pending_class_request = request_form.klass
                student.save()

                emailMessage = emailMessages.studentJoinRequestSentEmail(
                    request, request_form.klass.teacher.school.name,
                    request_form.klass.access_code)
                send_email(NOTIFICATION_EMAIL, [student.user.user.email],
                           emailMessage['subject'], emailMessage['message'])

                emailMessage = emailMessages.studentJoinRequestNotifyEmail(
                    request, student.user.user.username,
                    student.user.user.email,
                    student.pending_class_request.access_code)
                send_email(
                    NOTIFICATION_EMAIL,
                    [student.pending_class_request.teacher.user.user.email],
                    emailMessage['subject'], emailMessage['message'])

                messages.success(
                    request,
                    'Your request to join a school has been received successfully.'
                )

            else:
                request_form = OutputStudentJoinOrganisationForm(request.POST)

        elif 'revoke_join_request' in request.POST:
            student.pending_class_request = None
            student.save()
            # Check teacher hasn't since accepted rejection before posting success message
            if not student.class_field:
                messages.success(
                    request,
                    'Your request to join a school has been cancelled successfully.'
                )
            return HttpResponseRedirect(reverse_lazy('student_edit_account'))

    res = render(request, 'portal/play/student_join_organisation.html', {
        'request_form': request_form,
        'student': student
    })

    res.count = increment_count
    return res
def organisation_create(request):
    increment_count = False
    limits = getattr(request, 'limits', { 'ip': [0] })
    captcha_limit = 5

    using_captcha = (limits['ip'][0] > captcha_limit)
    should_use_captcha = (limits['ip'][0] >= captcha_limit)

    OrganisationJoinFormWithCaptcha = partial(create_form_subclass_with_recaptcha(OrganisationJoinForm, recaptcha_client), request)
    InputOrganisationJoinForm = OrganisationJoinFormWithCaptcha if using_captcha else OrganisationJoinForm
    OutputOrganisationJoinForm = OrganisationJoinFormWithCaptcha if should_use_captcha else OrganisationJoinForm

    teacher = request.user.userprofile.teacher

    create_form = OrganisationForm(user=request.user)
    join_form = OutputOrganisationJoinForm()

    if request.method == 'POST':
        if 'create_organisation' in request.POST:
            create_form = OrganisationForm(request.POST, user=request.user)
            if create_form.is_valid():
                data = create_form.cleaned_data
                name = data.get('name', '')
                postcode = data.get('postcode', '')
                country = data.get('country', '')

                error, town, lat, lng = '', '0', '0', '0' #lookup_coord(postcode, country)

                school = School.objects.create(
                    name=name,
                    postcode=postcode,
                    town = town,
                    latitude = lat,
                    longitude = lng,
                    country=country)

                teacher.school = school
                teacher.is_admin = True
                teacher.save()

                messages.success(request, "The school or club '" + teacher.school.name + "' has been successfully added.")

                return HttpResponseRedirect(reverse_lazy('teacher_home'))

        elif 'join_organisation' in request.POST:
            increment_count = True
            join_form = InputOrganisationJoinForm(request.POST)
            if join_form.is_valid():
                school = get_object_or_404(School, id=join_form.cleaned_data['chosen_org'])

                teacher.pending_join_request = school
                teacher.save()

                emailMessage = emailMessages.joinRequestPendingEmail(request, teacher.user.user.email)

                for admin in Teacher.objects.filter(school=school, is_admin=True):
                    send_email(NOTIFICATION_EMAIL, [admin.user.user.email], emailMessage['subject'], emailMessage['message'])

                emailMessage = emailMessages.joinRequestSentEmail(request, school.name)
                send_email(NOTIFICATION_EMAIL, [teacher.user.user.email], emailMessage['subject'], emailMessage['message'])

                messages.success(request, 'Your request to join the school or club has been sent successfully.')

            else:
                join_form = OutputOrganisationJoinForm(request.POST)

        elif 'revoke_join_request' in request.POST:
            teacher.pending_join_request = None
            teacher.save()

            messages.success(request, 'Your request to join the school or club has been revoked successfully.')

    res = render(request, 'portal/teach/organisation_create.html', {
        'create_form': create_form,
        'join_form': join_form,
        'teacher': teacher,
    })

    res.count = increment_count
    return res
示例#17
0
def send_new_users_report(request):
    new_profiles_count = UserProfile.objects.filter(user__date_joined__gte=timezone.now() - timedelta(days=7)).count()
    send_email(NOTIFICATION_EMAIL, CONTACT_FORM_EMAILS, "new users",
               "There are %d new users this week!" % new_profiles_count)
    return HttpResponse('success')