def post_for_guardian(self, **kwargs): context = self.get_context_data(**kwargs) guardian = context['guardian'] form = GuardianForm( self.request.POST, instance=guardian ) user_form = CDCModelForm( self.request.POST, instance=guardian.user ) if form.is_valid() and user_form.is_valid(): form.save() user_form.save() messages.success( self.request, 'Profile information saved.' ) return redirect('account_home') else: messages.error( self.request, 'There was an error. Please try again.' ) context['form'] = form context['user_form'] = user_form return render(self.request, 'account/home_guardian.html', context)
def get_for_guardian(self, **kwargs): context = self.get_context_data(**kwargs) context['form'] = GuardianForm(instance=context['guardian']) context['user_form'] = CDCModelForm(instance=context['guardian'].user) return render(self.request, 'account/guardian/home.html', context)
def get_context_data(self, **kwargs): context = super(WelcomeView, self).get_context_data(**kwargs) user = self.request.user mentor = kwargs.get('mentor') account = False role = getattr(user, 'role', False) context['role'] = role context['next_url'] = kwargs['next_url'] if mentor: account = mentor context['form'] = MentorForm(instance=account) if role == 'guardian': guardian = get_object_or_404(Guardian, user=user) account = guardian if not account.phone or not account.zip: context['form'] = GuardianForm(instance=account) else: context['add_student'] = True context['form'] = StudentForm( initial={'guardian': guardian.pk}) if account.user.first_name and account.get_students(): context['students'] = account.get_students().count() context['account'] = account return context
def update_account(self, request, account, next_url): if isinstance(account, Mentor): form = MentorForm(request.POST, instance=account) role = 'mentor' else: form = GuardianForm(request.POST, instance=account) role = 'guardian' if form.is_valid(): form.save() messages.success(request, 'Profile information saved.') if next_url: if 'enroll' in request.GET: next_url = f"{next_url}?enroll=True" else: next_url = 'dojo' if isinstance(account, Mentor) else 'welcome' return redirect(next_url) return render(request, self.template_name, { 'form': form, 'role': role, 'account': account, 'next_url': next_url })
def dojo_guardian(request, template_name='guardian/dojo.html'): highlight = ( request.GET['highlight'] if 'highlight' in request.GET else False ) context = { 'user': request.user, 'highlight': highlight, } guardian = get_object_or_404( Guardian, user=request.user ) students = Student.objects.filter( is_active=True, guardian=guardian, ) student_orders = Order.objects.filter( student__in=students, ) upcoming_orders = student_orders.filter( is_active=True, session__end_date__gte=timezone.now(), ).order_by('session__start_date') past_orders = student_orders.filter( is_active=True, session__end_date__lte=timezone.now(), ).order_by('session__start_date') if request.method == 'POST': form = GuardianForm( request.POST, instance=guardian ) user_form = CDCModelForm( request.POST, instance=guardian.user ) if form.is_valid() and user_form.is_valid(): form.save() user_form.save() messages.success( request, 'Profile information saved.' ) return redirect('dojo') else: messages.error( request, 'There was an error. Please try again.' ) else: form = GuardianForm(instance=guardian) user_form = CDCModelForm(instance=guardian.user) context['students'] = students context['upcoming_orders'] = upcoming_orders context['past_orders'] = past_orders context['guardian'] = guardian context['form'] = form context['user_form'] = user_form return render(request, template_name, context)