def test_user_update_form_email_taken(self): self.patient1 = User.objects.create_user('patient', '*****@*****.**', 'patientpassword', is_patient=True) form = UsersUpdateForm(data={'email': '*****@*****.**'}) self.assertFalse(form.is_valid()) self.assertEquals(len(form.errors), 1)
def post(self, request): o_form = PatientUpdateForm(request.POST, instance=request.user.userpatient) p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile) u_form = UsersUpdateForm(request.POST, instance=request.user) if p_form.is_valid() and o_form.is_valid() and u_form.is_valid(): p_form.save() o_form.save() u_form.save() return redirect('users:patient_profile') context = {'p_form': p_form, 'u_form': u_form, 'o_form': o_form} return render(request, self.template_name, context)
def get(self, request): p_form = ProfileUpdateForm(instance=request.user.profile) o_form = PatientUpdateForm(instance=request.user.userpatient) u_form = UsersUpdateForm(instance=request.user) context = {'o_form': o_form, 'p_form': p_form, 'u_form': u_form} return render(request, self.template_name, context)
def post(self, request): o_form = OfficeUpdateForm(request.POST, instance=request.user.useroffice) p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile) u_form = UsersUpdateForm(request.POST, instance=request.user) days = [ OfficeDay.objects.get(office=request.user.useroffice, day=i) for i in range(7) ] days_forms = [ OfficeDayUpdateForm(request.POST, instance=days[i], prefix=f'days{i}') for i in range(7) ] if p_form.is_valid() and o_form.is_valid() and u_form.is_valid(): p_form.save() o_form.save() u_form.save() for day_form in days_forms: if day_form.is_valid(): day_form.save() else: # show error as message due to complicated code which does not display for error message messages.warning( request, 'Najpóźniejsza godzina musi być większa od najwcześniejszej.' ) return redirect('users:office_profile') context = { 'p_form': p_form, 'u_form': u_form, 'o_form': o_form, 'd_forms': days_forms } return render(request, self.template_name, context)
def get(self, request): p_form = ProfileUpdateForm(instance=request.user.profile) o_form = OfficeUpdateForm(instance=request.user.useroffice) u_form = UsersUpdateForm(instance=request.user) days = [ OfficeDay.objects.get(office=request.user.useroffice, day=i) for i in range(7) ] days_forms = [ OfficeDayUpdateForm(instance=days[i], prefix=f'days{i}') for i in range(7) ] context = { 'o_form': o_form, 'p_form': p_form, 'u_form': u_form, 'd_forms': days_forms } return render(request, self.template_name, context)
def test_user_update_form_no_data(self): form = UsersUpdateForm(data={}) self.assertFalse(form.is_valid()) self.assertEquals(len(form.errors), 1)
def test_user_update_form_valid(self): form = UsersUpdateForm(data={'email': '*****@*****.**'}) self.assertTrue(form.is_valid())