def form_valid(self, form): """ If the form is invalid, re-render the context data with the data-filled form and errors. """ transaction.set_autocommit(False) try: user = Users() user.username = form.cleaned_data['login_id'] user.email = Users.objects.normalize_email( form.cleaned_data['email']) user.set_password(form.cleaned_data['password']) user.user_group_id = int(form.cleaned_data['level']) irric_user = IrricUser() irric_user.name = form.cleaned_data['name'] irric_user.furigana = form.cleaned_data['furigana'] if form.cleaned_data["password_reset_next_login_flag"] is True: user.is_force_password_change = True now = timezone.now() user.created_user_id = self.request.user.id user.updated_user_id = self.request.user.id user.created_at = now user.updated_at = now irric_user.created_user_id = self.request.user.id irric_user.updated_user_id = self.request.user.id irric_user.created_at = now irric_user.updated_at = now irric_user.save() user.irric_user_id = irric_user.id user.save() if form.cleaned_data["send_mail_flag"] is True: # TODO IRRICユーザー作成後にメール送信 pass except Exception as e: logger.exception(e) transaction.rollback() return self.form_invalid(form) finally: transaction.commit() transaction.set_autocommit(True) return super().form_valid(form)
def test_saving_and_retrieving_user(self): user = Users() username = '******' email = '*****@*****.**' is_active = True is_staff = False user.username = username user.email = email user.is_active = is_active user.is_staff = is_staff user.save() saved_user = Users.objects.all() actual_user = saved_user[0] self.assertEqual(actual_user.username, username) self.assertEqual(actual_user.email, email) self.assertEqual(actual_user.is_active, is_active) self.assertEqual(actual_user.is_staff, is_staff)