def add_student(request, group_id=None): """Add a student.""" group = get_object_or_404(model.Group, id=group_id) if group_id else None if request.method == 'POST': form = forms.AddStudentForm( request.POST, elder=request.user.profile, group=group) if form.is_valid(): with xact.xact(): student = form.save() tracking.track(request, 'added student') return redirect_to_village(student, group) else: form = forms.AddStudentForm(elder=request.user.profile, group=group) return TemplateResponse( request, 'village/student_form/add_student.html', { 'form': form, 'group': group, 'code': group.code if group else request.user.profile.code, 'default_lang_code': settings.LANGUAGE_CODE, 'pyo_phone': formats.display_phone( request.user.profile.source_phone), 'group_just_created': group and request.GET.get('created', None), }, )
def invite_teacher_to_group(request, group_id): """Invite teacher to a group.""" group = get_object_or_404( model.Group.objects.filter(owner=request.user.profile), id=group_id) if request.method == 'POST': form = forms.InviteTeacherForm(request.POST, group=group) if form.is_valid(): with xact.xact(): teacher = form.save() tracking.track( request, 'invited teacher', invitedEmail=teacher.user.email, groupId=group_id, ) return redirect('group', group_id=group.id) else: form = forms.InviteTeacherForm(group=group) return TemplateResponse( request, 'village/invite_elder/teacher_to_group.html', { 'group': group, 'form': form, 'elders': model.contextualized_elders( group.all_elders).order_by('school_staff', 'name'), }, )
def pdf_parent_instructions(request, lang, group_id=None): """Render a PDF for sending home with parents.""" group = get_object_or_404( model.Group.objects.filter( owner=request.user.profile, id=group_id)) if group_id else None lang_verbose = dict(settings.LANGUAGES).get(lang, lang) filename = "Portfoliyo {0}{1}.pdf".format( lang_verbose, " - {0}".format(unidecode(group.name)) if group else u"", ) response = http.HttpResponse(content_type='application/pdf') response['Content-Disposition'] = ( 'attachment; filename="%s"' % filename.encode('utf-8')) pdf.generate_instructions_pdf( stream=response, lang=lang, name=request.user.profile.name or "Your Child's Teacher", code=group.code if group else request.user.profile.code or '', phone=request.user.profile.source_phone, group=group, ) tracking.track(request, 'downloaded signup pdf', language=lang) return response
def invite_family(request, student_id): """Invite family member to a student's village.""" rel = get_relationship_or_404(student_id, request.user.profile) group = get_querystring_group(request, rel.student) if request.method == 'POST': form = forms.InviteFamilyForm(request.POST, rel=rel) if form.is_valid(): with xact.xact(): form.save() tracking.track(request, 'invited family') return redirect_to_village(rel.student, group) else: phone = request.GET.get('phone', None) initial = {} if phone is not None: initial['phone'] = phone form = forms.InviteFamilyForm(initial=initial, rel=rel) return TemplateResponse( request, 'village/invite_elder/family.html', { 'group': group, 'student': rel.student, 'inviter': model.elder_in_context(rel), 'elders': model.contextualized_elders( rel.student.elder_relationships).order_by( 'school_staff', 'name'), 'form': form, }, )
def invite_teacher(request, student_id): """Invite teacher to a student's village.""" rel = get_relationship_or_404(student_id, request.user.profile) group = get_querystring_group(request, rel.student) if request.method == 'POST': form = forms.InviteTeacherForm(request.POST, rel=rel) if form.is_valid(): with xact.xact(): teacher = form.save() tracking.track( request, 'invited teacher', invitedEmail=teacher.user.email, studentId=student_id, ) return redirect_to_village(rel.student, group) else: form = forms.InviteTeacherForm(rel=rel) return TemplateResponse( request, 'village/invite_elder/teacher.html', { 'group': group, 'student': rel.student, 'form': form, 'elders': model.contextualized_elders( rel.student.elder_relationships).order_by( 'school_staff', 'name'), }, )
def edit_student(request, student_id): """Edit a student.""" rel = get_relationship_or_404(student_id, request.user.profile) group = get_querystring_group(request, rel.student) if request.method == 'POST': if request.POST.get('remove', False): with xact.xact(): rel.delete() messages.success(request, "Student '%s' removed." % rel.student) if group: return redirect('group', group_id=group.id) else: return redirect('all_students') form = forms.StudentForm( request.POST, instance=rel.student, elder=rel.elder) if form.is_valid(): with xact.xact(): student = form.save() tracking.track(request, 'edited student') return redirect_to_village(student, group) else: form = forms.StudentForm(instance=rel.student, elder=rel.elder) return TemplateResponse( request, 'village/student_form/edit_student.html', { 'form': form, 'student': rel.student, 'group': group, }, )
def edit_group(request, group_id): """Edit a group.""" group = get_object_or_404( model.Group.objects.select_related('owner'), pk=group_id) if group.owner != request.user.profile: raise http.Http404 if request.method == 'POST': if request.POST.get('remove', False): with xact.xact(): group.delete() messages.success(request, "Group '%s' removed." % group) return redirect(home.redirect_home(request.user)) form = forms.GroupForm(request.POST, instance=group) if form.is_valid(): with xact.xact(): group = form.save() tracking.track(request, 'edited group') return redirect('group', group_id=group.id) else: form = forms.GroupForm(instance=group) return TemplateResponse( request, 'village/group_form/edit_group.html', { 'form': form, 'group': group, }, )
def test_track(): """track() function records event data in session.""" request = mock.Mock() request.session = MockSession() tracking.track(request, 'some event', foo='bar') assert request.session['tracking'] == [('some event', {'foo': 'bar'})] assert request.session.modified
def register(request): if request.method == 'POST': form = forms.RegistrationForm(request.POST) if form.is_valid(): with xact.xact(): profile = form.save() user = profile.user user.backend = settings.AUTHENTICATION_BACKENDS[0] auth.login(request, user) token_generator = tokens.EmailConfirmTokenGenerator() invites.send_invite_email( profile, 'emails/welcome', token_generator=token_generator, ) messages.success( request, "Welcome to Portfoliyo! " "Grab your phone and add yourself as a parent " "to see how it works!" ) tracking.track( request, 'registered', email_notifications=( 'yes' if form.cleaned_data.get('email_notifications') else 'no' ), user_id=user.id, ) return redirect(redirect_home(user)) else: form = forms.RegistrationForm() return TemplateResponse( request, 'users/register.html', {'form': form}, )
def accept_email_invite(request, uidb36, token): response = auth_views.password_reset_confirm( request, uidb36=uidb36, token=token, template_name='users/accept_email_invite.html', set_password_form=auth_forms.SetPasswordForm, post_reset_redirect=reverse('login'), ) if response.status_code == 302: uid_int = base36_to_int(uidb36) model.User.objects.filter(id=uid_int).update(is_active=True) messages.success( request, u"Great, you've chosen a password! " u"Now log in using your email address and password " u"to see messages about your student.", ) tracking.track(request, 'accepted email invite') return response
def confirm_email(request, uidb36, token): """Confirm an email address.""" try: uid_int = base36_to_int(uidb36) user = model.User.objects.get(id=uid_int) except (ValueError, model.User.DoesNotExist): user = None token_generator = tokens.EmailConfirmTokenGenerator() if user is not None and token_generator.check_token(user, token): with xact.xact(): user.is_active = True user.save(force_update=True) profile = user.profile profile.email_confirmed = True profile.save(force_update=True) messages.success(request, "Email address %s confirmed!" % user.email) tracking.track(request, 'confirmed email') return redirect(redirect_home(user)) return TemplateResponse(request, 'users/confirmation_failed.html')
def add_group(request): """Add a group.""" if request.method == 'POST': form = forms.AddGroupForm(request.POST, owner=request.user.profile) if form.is_valid(): with xact.xact(): group = form.save() tracking.track(request, 'added group') if not group.students.exists(): return redirect( reverse('add_student', kwargs={'group_id': group.id}) + '?created=1') return redirect('group', group_id=group.id) else: form = forms.AddGroupForm(owner=request.user.profile) return TemplateResponse( request, 'village/group_form/add_group.html', { 'form': form, }, )
def register(request): if request.method == 'POST': form = forms.RegistrationForm(request.POST) if form.is_valid(): with xact.xact(): profile = form.save() user = profile.user user.backend = settings.AUTHENTICATION_BACKENDS[0] auth.login(request, user) token_generator = tokens.EmailConfirmTokenGenerator() invites.send_invite_email( profile, 'emails/welcome', token_generator=token_generator, ) messages.success( request, "Welcome to Portfoliyo! " "Grab your phone and add yourself as a parent " "to see how it works!") tracking.track( request, 'registered', email_notifications=( 'yes' if form.cleaned_data.get('email_notifications') else 'no'), user_id=user.id, ) return redirect(redirect_home(user)) else: form = forms.RegistrationForm() return TemplateResponse( request, 'users/register.html', {'form': form}, )