def form_valid(self, form): firstname=form.cleaned_data["first_name"] lastname=form.cleaned_data['last_name'] firstname=firstname.replace(" ","") lastname=lastname.replace(" ","") teacher_name=form.cleaned_data["teacher_name"] user_name=teacher_name.replace(" ","").replace(".","") new_user=User.objects.create_user( username=user_name.lower(), first_name=firstname.title(), last_name=lastname.title(), email=form.cleaned_data['email'], ) new_user.set_password(form.cleaned_data["password1"]) new_user.save() new_staff=School_Staff(user=new_user, teacher_name=teacher_name, allow_contact=form.cleaned_data['allow_contact'], ) new_staff.save() staff_group=Group.objects.get(name='Staff_Group') new_user.groups.add(staff_group) return HttpResponseRedirect(reverse('staff-setup-view',))
def form_valid(self, form): firstname = form.cleaned_data["first_name"] lastname = form.cleaned_data["last_name"] firstname = firstname.replace(" ", "") lastname = lastname.replace(" ", "") teacher_name = form.cleaned_data["teacher_name"] # checks for duplicate teacher names # if duplicate teacher names adds a number at end i = 1 while True: if not School_Staff.objects.filter(teacher_name=teacher_name): break teacher_name = teacher_name + str(i) i = i + 1 username = teacher_name.replace(" ", "").replace(".", "") new_user = User.objects.create_user( username=username.lower(), first_name=firstname.title(), last_name=lastname.title(), email=form.cleaned_data["email"], ) new_user.set_password(form.cleaned_data["password1"]) new_user.save() new_staff = School_Staff(user=new_user, teacher_name=teacher_name, allow_contact=True) new_staff.save() staff_group = Group.objects.get(name="Staff_Group") new_user.groups.add(staff_group) user = authenticate(username=new_user.username, password=form.cleaned_data["password1"]) if user is not None: login(self.request, user) return HttpResponseRedirect(reverse("welcome-view"))