def organisation_deny_join(request, pk): teacher = get_object_or_404(Teacher, id=pk) user = request.user.new_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 = email_messages.joinRequestDeniedEmail( request, request.user.new_teacher.school.name ) send_email( NOTIFICATION_EMAIL, [teacher.new_user.email], emailMessage["subject"], emailMessage["message"], ) return HttpResponseRedirect(reverse_lazy("dashboard"))
def process_join_organisation_form(request_form, request, student): if request_form.is_valid(): student.pending_class_request = request_form.klass student.save() email_message = email_messages.studentJoinRequestSentEmail( request, request_form.klass.teacher.school.name, request_form.klass.access_code, ) send_email( NOTIFICATION_EMAIL, [student.new_user.email], email_message["subject"], email_message["message"], ) email_message = email_messages.studentJoinRequestNotifyEmail( request, student.new_user.username, student.new_user.email, student.pending_class_request.access_code, ) send_email( NOTIFICATION_EMAIL, [student.pending_class_request.teacher.new_user.email], email_message["subject"], email_message["message"], ) messages.success( request, "Your request to join a school has been received successfully.", )
def organisation_deny_join(request, pk): teacher = get_object_or_404(Teacher, id=pk) user = request.user.new_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 = email_messages.joinRequestDeniedEmail( request, request.user.new_teacher.school.name) send_email( NOTIFICATION_EMAIL, [teacher.new_user.email], emailMessage["subject"], emailMessage["message"], ) return HttpResponseRedirect(reverse_lazy("dashboard"))
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() active_users = User.objects.filter(last_login__gte=timezone.now() - timedelta(days=7)).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') nb_countries = schools_countries.count() countries_count = "\n".join( '{}: {}'.format(dict(countries)[k["country"]], k["nb_countries"]) for k in schools_countries[:3]) 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' 'Number of users that last logged in during the last week: {active_users}\n' 'Number of countries with registered schools: {countries}\n' 'Top 3 - 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=nb_countries, active_users=active_users, countries_counter=countries_count)) return HttpResponse('success')
def organisation_toggle_admin(request, pk): teacher = get_object_or_404(Teacher, id=pk) user = request.user.new_teacher check_teacher_is_authorised(teacher, user) teacher.is_admin = not teacher.is_admin teacher.save() if teacher.is_admin: messages.success(request, "Administrator status has been given successfully.") emailMessage = email_messages.adminGivenEmail(request, teacher.school.name) else: messages.success( request, "Administrator status has been revoked successfully.") emailMessage = email_messages.adminRevokedEmail( request, teacher.school.name) send_email( NOTIFICATION_EMAIL, [teacher.new_user.email], emailMessage["subject"], emailMessage["message"], ) return HttpResponseRedirect(reverse_lazy("dashboard"))
def organisation_toggle_admin(request, pk): teacher = get_object_or_404(Teacher, id=pk) user = request.user.new_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.new_user.email], emailMessage['subject'], emailMessage['message']) return HttpResponseRedirect(reverse_lazy('organisation_manage'))
def process_independent_student_signup_form(request, data): email = data["email"] independent_students = Student.objects.filter(class_field=None) if email and independent_students.filter(new_user__email=email).exists(): email_message = email_messages.userAlreadyRegisteredEmail(request, email) send_email( NOTIFICATION_EMAIL, [email], email_message["subject"], email_message["message"], ) return render(request, "portal/email_verification_needed.html") student = Student.objects.independentStudentFactory( username=data["username"], name=data["name"], email=data["email"], password=data["password"], ) if _newsletter_ticked(data): user = student.new_user add_to_salesforce(user.first_name, user.last_name, user.email) send_verification_email(request, student.new_user) return render( request, "portal/email_verification_needed.html", {"user": student.new_user} )
def process_join_form(request, teacher, InputOrganisationJoinForm, OutputOrganisationJoinForm): 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() email_message = emailMessages.joinRequestPendingEmail( request, teacher.new_user.email) send_pending_requests_emails(school, email_message) email_message = emailMessages.joinRequestSentEmail( request, school.name) send_email(NOTIFICATION_EMAIL, [teacher.new_user.email], email_message['subject'], email_message['message']) messages.success( request, 'Your request to join the school or club has been sent successfully.' ) return render(request, 'portal/teach/onboarding_school.html', { 'school': school, 'teacher': teacher }) else: join_form = OutputOrganisationJoinForm(request.POST)
def process_signup_form(request, data): email = data["teacher_email"] teacher = None if email and Teacher.objects.filter(new_user__email=email).exists(): email_message = email_messages.userAlreadyRegisteredEmail(request, email) send_email( NOTIFICATION_EMAIL, [email], email_message["subject"], email_message["message"], ) else: teacher = Teacher.objects.factory( title=data["teacher_title"], first_name=data["teacher_first_name"], last_name=data["teacher_last_name"], email=data["teacher_email"], password=data["teacher_password"], ) if _newsletter_ticked(data): user = teacher.user.user add_to_dotmailer(user.first_name, user.last_name, user.email) send_verification_email(request, teacher.user.user) if teacher: return render( request, "portal/email_verification_needed.html", {"user": teacher.user.user}, ) else: return render(request, "portal/email_verification_needed.html")
def process_signup_form(request, data): email = data["teacher_email"] teacher = None if email and Teacher.objects.filter(new_user__email=email).exists(): email_message = email_messages.userAlreadyRegisteredEmail(request, email) send_email( NOTIFICATION_EMAIL, [email], email_message["subject"], email_message["message"], ) else: teacher = Teacher.objects.factory( title=data["teacher_title"], first_name=data["teacher_first_name"], last_name=data["teacher_last_name"], email=data["teacher_email"], password=data["teacher_password"], ) if _newsletter_ticked(data): user = teacher.user.user add_to_salesforce(user.first_name, user.last_name, user.email) send_verification_email(request, teacher.user.user) if teacher: return render( request, "portal/email_verification_needed.html", {"user": teacher.user.user}, ) else: return render(request, "portal/email_verification_needed.html")
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.new_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.new_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 process_signup_form(request, data): email = data['teacher_email'] teacher = None if email and Teacher.objects.filter(new_user__email=email).exists(): email_message = emailMessages.userAlreadyRegisteredEmail( request, email) send_email(NOTIFICATION_EMAIL, [email], email_message['subject'], email_message['message']) else: teacher = Teacher.objects.factory( title=data['teacher_title'], first_name=data['teacher_first_name'], last_name=data['teacher_last_name'], email=data['teacher_email'], password=data['teacher_password']) if _newsletter_ticked(data): user = teacher.user.user add_to_salesforce(user.first_name, user.last_name, user.email) send_verification_email(request, teacher.user.user) if teacher: return render(request, 'portal/email_verification_needed.html', {'user': teacher.user.user}) else: return render(request, 'portal/email_verification_needed.html')
def organisation_toggle_admin(request, pk): teacher = get_object_or_404(Teacher, id=pk) user = request.user.new_teacher check_is_toggle_authorised(teacher, user) if request.method == 'POST': 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.new_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.new_teacher != student.pending_class_request.teacher: raise Http404 emailMessage = email_messages.studentJoinRequestRejectedEmail( request, student.pending_class_request.teacher.school.name, student.pending_class_request.access_code, ) send_email( NOTIFICATION_EMAIL, [student.new_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("dashboard"))
def process_independent_student_signup_form(request, data): email = data["email"] independent_students = Student.objects.filter(class_field=None) if email and independent_students.filter(new_user__email=email).exists(): email_message = email_messages.userAlreadyRegisteredEmail( request, email) send_email( NOTIFICATION_EMAIL, [email], email_message["subject"], email_message["message"], ) return render(request, "portal/email_verification_needed.html") student = Student.objects.independentStudentFactory( username=data["username"], name=data["name"], email=data["email"], password=data["password"], ) if _newsletter_ticked(data): user = student.new_user add_to_salesforce(user.first_name, user.last_name, user.email) send_verification_email(request, student.new_user) return render(request, "portal/email_verification_needed.html", {"user": student.new_user})
def send_pending_requests_emails(school, email_message): for admin in Teacher.objects.filter(school=school, is_admin=True): send_email( NOTIFICATION_EMAIL, [admin.new_user.email], email_message["subject"], email_message["message"], )
def student_join_organisation(request): increment_count = False student = request.user.new_student request_form = StudentJoinOrganisationForm() # 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 = StudentJoinOrganisationForm(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.new_user.email], emailMessage['subject'], emailMessage['message']) emailMessage = emailMessages.studentJoinRequestNotifyEmail( request, student.new_user.username, student.new_user.email, student.pending_class_request.access_code) send_email( NOTIFICATION_EMAIL, [student.pending_class_request.teacher.new_user.email], emailMessage['subject'], emailMessage['message']) messages.success( request, 'Your request to join a school has been received successfully.' ) else: request_form = StudentJoinOrganisationForm(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 contact(request): increment_count = False should_use_captcha = captcha.CAPTCHA_ENABLED anchor = '' if request.method == 'POST': contact_form = ContactForm(request.POST) if not should_use_captcha: remove_captcha_from_forms(contact_form) increment_count = True if contact_form.is_valid(): anchor = "top" email_message = 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, app_settings.CONTACT_FORM_EMAILS, email_message['subject'], email_message['message']) confirmed_email_message = 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']], confirmed_email_message['subject'], confirmed_email_message['message']) messages.success(request, 'Your message was sent successfully.') return render(request, 'portal/help-and-support.html', { 'form': contact_form, 'anchor': anchor }) else: contact_form = ContactForm(request.POST) anchor = "contact" else: contact_form = ContactForm() if not should_use_captcha: remove_captcha_from_forms(contact_form) response = render( request, 'portal/help-and-support.html', { 'form': contact_form, 'anchor': anchor, 'captcha': should_use_captcha, 'settings': app_settings }) response.count = increment_count return response
def email_new_custom_level(teacher_email, moderate_url, level_url, home_url, student_name, class_name): # email teacher when a new custom level is created by a pupil, so it can be moderated ASAP send_email( NOTIFICATION_EMAIL, [teacher_email], level_creation_email_subject(), level_creation_email_text_content().format(moderate_url=moderate_url, level_url=level_url, student_name=student_name, class_name=class_name, home_url=home_url))
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 anchor = '' if request.method == 'POST': contact_form = InputContactForm(request.POST) increment_count = True if contact_form.is_valid(): email_message = 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, email_message['subject'], email_message['message']) confirmed_email_message = 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']], confirmed_email_message['subject'], confirmed_email_message['message']) messages.success(request, 'Your message was sent successfully.') return HttpResponseRedirect('.') else: contact_form = OutputContactForm(request.POST) anchor = "contact" else: contact_form = OutputContactForm() response = render(request, 'portal/help-and-support.html', { 'form': contact_form, 'anchor': anchor }) response.count = increment_count return response
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) anchor = '' if request.method == 'POST': contact_form = ContactForm(request.POST) increment_count = True contact_form.view_options['is_recaptcha_visible'] = should_use_captcha contact_form.view_options['is_recaptcha_valid'] = check_recaptcha( request) if using_captcha else False if contact_form.is_valid(): email_message = 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, app_settings.CONTACT_FORM_EMAILS, email_message['subject'], email_message['message']) confirmed_email_message = 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']], confirmed_email_message['subject'], confirmed_email_message['message']) messages.success(request, 'Your message was sent successfully.') return HttpResponseRedirect('.') else: contact_form = ContactForm(request.POST) anchor = "contact" else: contact_form = ContactForm() response = render( request, 'portal/help-and-support.html', { 'form': contact_form, 'anchor': anchor, 'captcha': should_use_captcha, 'settings': app_settings }) response.count = increment_count return response
def organisation_kick(request, pk): teacher = get_object_or_404(Teacher, id=pk) user = request.user.new_teacher check_teacher_is_authorised(teacher, user) 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 = email_messages.kickedEmail(request, user.school.name) send_email( NOTIFICATION_EMAIL, [teacher.new_user.email], emailMessage["subject"], emailMessage["message"], ) return HttpResponseRedirect(reverse_lazy("dashboard"))
def invite_teacher(request): if request.method == 'POST': invite_teacher_form = InviteTeacherForm(data=request.POST) if invite_teacher_form.is_valid(): email_address = invite_teacher_form.cleaned_data['email'] email_message = emailMessages.inviteTeacherEmail(request) send_email(INVITE_FROM, [email_address], email_message['subject'], email_message['message']) return render(request, 'portal/email_invitation_sent.html') return render(request, 'portal/teach/invite.html', {'invite_form': InviteTeacherForm()})
def organisation_kick(request, pk): teacher = get_object_or_404(Teacher, id=pk) user = request.user.new_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.new_user.email], emailMessage['subject'], emailMessage['message']) return HttpResponseRedirect(reverse_lazy('organisation_manage'))
def organisation_kick(request, pk): teacher = get_object_or_404(Teacher, id=pk) user = request.user.new_teacher check_teacher_is_authorised(teacher, user) 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 = email_messages.kickedEmail(request, user.school.name) send_email( NOTIFICATION_EMAIL, [teacher.new_user.email], emailMessage["subject"], emailMessage["message"], ) return HttpResponseRedirect(reverse_lazy("dashboard"))
def invite_teacher(request): if request.method == "POST": invite_teacher_form = InviteTeacherForm(data=request.POST) if invite_teacher_form.is_valid(): email_address = invite_teacher_form.cleaned_data["email"] email_message = email_messages.inviteTeacherEmail(request) send_email( INVITE_FROM, [email_address], email_message["subject"], email_message["message"], ) return render(request, "portal/email_invitation_sent.html") return render(request, "portal/teach/invite.html", {"invite_form": InviteTeacherForm()})
def email_new_custom_level( teacher_email, moderate_url, level_url, home_url, student_name, class_name ): # email teacher when a new custom level is created by a pupil, so it can be moderated ASAP send_email( NOTIFICATION_EMAIL, [teacher_email], level_creation_email_subject(), level_creation_email_text_content().format( moderate_url=moderate_url, level_url=level_url, student_name=student_name, class_name=class_name, home_url=home_url, ), )
def invite_teacher(request): if request.method == "POST": invite_teacher_form = InviteTeacherForm(data=request.POST) if invite_teacher_form.is_valid(): email_address = invite_teacher_form.cleaned_data["email"] email_message = email_messages.inviteTeacherEmail(request) send_email( INVITE_FROM, [email_address], email_message["subject"], email_message["message"], ) return render(request, "portal/email_invitation_sent.html") return render( request, "portal/teach/invite.html", {"invite_form": InviteTeacherForm()} )
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() active_users = User.objects.filter( last_login__gte=timezone.now() - timedelta(days=7) ).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") ) nb_countries = schools_countries.count() countries_count = "\n".join( "{}: {}".format(dict(countries)[k["country"]], k["nb_countries"]) for k in schools_countries[:3] ) 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" "Number of users that last logged in during the last week: {active_users}\n" "Number of countries with registered schools: {countries}\n" "Top 3 - 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=nb_countries, active_users=active_users, countries_counter=countries_count, ), ) return HttpResponse("success")
def organisation_allow_join(request, pk): teacher = get_object_or_404(Teacher, id=pk) user = request.user.new_teacher # check user has authority to accept teacher if teacher.pending_join_request != user.school or not user.is_admin: raise Http404 teacher.school = teacher.pending_join_request teacher.pending_join_request = None teacher.is_admin = False teacher.save() messages.success(request, 'The teacher has been added to your school or club.') emailMessage = emailMessages.joinRequestAcceptedEmail( request, teacher.school.name) send_email(NOTIFICATION_EMAIL, [teacher.new_user.email], emailMessage['subject'], emailMessage['message']) return HttpResponseRedirect(reverse_lazy('organisation_manage'))
def process_join_form( request, teacher, InputOrganisationJoinForm, OutputOrganisationJoinForm ): 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() email_message = email_messages.joinRequestPendingEmail( request, teacher.new_user.email ) send_pending_requests_emails(school, email_message) email_message = email_messages.joinRequestSentEmail(request, school.name) send_email( NOTIFICATION_EMAIL, [teacher.new_user.email], email_message["subject"], email_message["message"], ) messages.success( request, "Your request to join the school or club has been sent successfully.", ) return render( request, "portal/teach/onboarding_school.html", {"school": school, "teacher": teacher}, ) else: join_form = OutputOrganisationJoinForm(request.POST)
def organisation_toggle_admin(request, pk): teacher = get_object_or_404(Teacher, id=pk) user = request.user.new_teacher check_teacher_is_authorised(teacher, user) teacher.is_admin = not teacher.is_admin teacher.save() if teacher.is_admin: messages.success(request, "Administrator status has been given successfully.") emailMessage = email_messages.adminGivenEmail(request, teacher.school.name) else: messages.success(request, "Administrator status has been revoked successfully.") emailMessage = email_messages.adminRevokedEmail(request, teacher.school.name) send_email( NOTIFICATION_EMAIL, [teacher.new_user.email], emailMessage["subject"], emailMessage["message"], ) return HttpResponseRedirect(reverse_lazy("dashboard"))
def student_join_organisation(request): increment_count = False student = request.user.new_student request_form = StudentJoinOrganisationForm() # 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 = StudentJoinOrganisationForm(request.POST) if request_form.is_valid(): student.pending_class_request = request_form.klass student.save() emailMessage = email_messages.studentJoinRequestSentEmail( request, request_form.klass.teacher.school.name, request_form.klass.access_code, ) send_email( NOTIFICATION_EMAIL, [student.new_user.email], emailMessage["subject"], emailMessage["message"], ) emailMessage = email_messages.studentJoinRequestNotifyEmail( request, student.new_user.username, student.new_user.email, student.pending_class_request.access_code, ) send_email( NOTIFICATION_EMAIL, [student.pending_class_request.teacher.new_user.email], emailMessage["subject"], emailMessage["message"], ) messages.success( request, "Your request to join a school has been received successfully.", ) else: request_form = StudentJoinOrganisationForm(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 contact(request): increment_count = False should_use_captcha = captcha.CAPTCHA_ENABLED anchor = "" if request.method == "POST": contact_form = ContactForm(request.POST) if not should_use_captcha: remove_captcha_from_forms(contact_form) increment_count = True if contact_form.is_valid(): anchor = "top" email_message = email_messages.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, app_settings.CONTACT_FORM_EMAILS, email_message["subject"], email_message["message"], ) confirmed_email_message = email_messages.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"]], confirmed_email_message["subject"], confirmed_email_message["message"], ) messages.success(request, "Your message was sent successfully.") return render( request, "portal/help-and-support.html", { "form": contact_form, "anchor": anchor }, ) else: contact_form = ContactForm(request.POST) anchor = "contact" else: contact_form = ContactForm() if not should_use_captcha: remove_captcha_from_forms(contact_form) response = render( request, "portal/help-and-support.html", { "form": contact_form, "anchor": anchor, "captcha": should_use_captcha, "settings": app_settings, }, ) response.count = increment_count return response
def contact(request): increment_count = False should_use_captcha = captcha.CAPTCHA_ENABLED anchor = "" if request.method == "POST": contact_form = ContactForm(request.POST) if not should_use_captcha: remove_captcha_from_forms(contact_form) increment_count = True if contact_form.is_valid(): anchor = "top" email_message = email_messages.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, app_settings.CONTACT_FORM_EMAILS, email_message["subject"], email_message["message"], ) confirmed_email_message = email_messages.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"]], confirmed_email_message["subject"], confirmed_email_message["message"], ) messages.success(request, "Your message was sent successfully.") return render( request, "portal/help-and-support.html", {"form": contact_form, "anchor": anchor}, ) else: contact_form = ContactForm(request.POST) anchor = "contact" else: contact_form = ContactForm() if not should_use_captcha: remove_captcha_from_forms(contact_form) response = render( request, "portal/help-and-support.html", { "form": contact_form, "anchor": anchor, "captcha": should_use_captcha, "settings": app_settings, }, ) response.count = increment_count return response
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.new_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.new_user.email) for admin in Teacher.objects.filter(school=school, is_admin=True): send_email(NOTIFICATION_EMAIL, [admin.new_user.email], emailMessage['subject'], emailMessage['message']) emailMessage = emailMessages.joinRequestSentEmail( request, school.name) send_email(NOTIFICATION_EMAIL, [teacher.new_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