def edit_student(request, class_id, id=None): try: school_class = SchoolClass.objects.get(id=class_id) student = LPUser.objects.get(id=id) if id else None except (SchoolClass.DoesNotExist, LPUser.DoesNotExist): return HttpResponse(status=400) instance = student.user if student else None avatar_form = AvatarForm(request.POST, request.FILES) form = StudentForm(request.POST or None, instance=instance) if form.is_valid(): user = form.save() user.set_password("password") user.save() if not id: user.groups.add(Group.objects.get(name='students')) lp_user = LPUser() lp_user.user = user lp_user.save() lp_user.school_class.add(school_class) else: lp_user = student if avatar_form.is_valid() and avatar_form.cleaned_data['avatar'] != 'avatars/default.png': lp_user.avatar = avatar_form.cleaned_data['avatar'] lp_user.save() return redirect(reverse('backoffice:my_students', kwargs={'class_id': class_id})) return render(request, 'backoffice/edit_student.html', {'avatar_form': avatar_form, 'student_form': form, 'school_class': school_class, 'student': student})
def test_missing_username(self): form = StudentForm(data=self.missing_username) self.assertFalse(form.is_valid())
def test_valid_form(self): form = StudentForm(data=self.valid_form_data) self.assertTrue(form.is_valid())