def post(self, request, *args, **kwargs): """ 用户注册 """ form = CreateUserForm(request.data) if not form.is_valid(): return Response({'Detail': form.errors}, status=status.HTTP_400_BAD_REQUEST) cld = form.cleaned_data is_valid, error_message = self.is_request_data_valid(**cld) if not is_valid: return Response({'Detail': error_message}, status=status.HTTP_400_BAD_REQUEST) is_valid, error_message = verify_identifying_code(cld) if not is_valid: return Response({'Detail': error_message}, status=status.HTTP_400_BAD_REQUEST) init_data = self.get_perfect_request_data(**cld) try: user = User.objects.create_user(**init_data) except Exception as e: return Response({'Detail': e.args}, status=status.HTTP_400_BAD_REQUEST) # 置用户为登录状态 _token = Oauth2AccessToken().get_token(user) return Response(_token, status=status.HTTP_201_CREATED)
def post(self, request): user_form = CreateUserForm(request.POST) profile_form = CreateProfileForm(request.POST) print(user_form.errors) print(user_form.cleaned_data) if user_form.is_valid() and profile_form.is_valid(): user = User(**user_form.cleaned_data) user.save() print('User表OK,,,,') user_profile = Profile() user_profile.user = user user_profile.name = profile_form.cleaned_data['name'] user_profile.lnvalid_date = profile_form.cleaned_data[ 'lnvalid_date'] user_profile.phone = profile_form.cleaned_data['phone'] user_profile.weixin = profile_form.cleaned_data['weixin'] user_profile.info = request.POST.getlist('info', None)[0] user_profile.save() print('userprofile表OK,,,,') if request.POST.get('groups', None): user.groups.set(request.POST.getlist('groups')) register_email = RegisterEmail() register_email.user = user register_email.type_code = 0 register_email.code = random_str() register_email.save() contnet = """ <p>你好 %s: </p> <p>恭喜您,您的账号已经创建成功 </p> <p>用户名: %s </p> <p><a href='%s'>请点击这里设置密码</a> </p> """ % (user.username, user_profile.name, settings.HOST_URL + reverse('user_create_password') + '?code=' + str(register_email.code)) try: send_mail(settings.EMAIL_USER, settings.EMAIL_PASSWORD, user.email, settings.EMAIL_TITLE, contnet, settings.EMAIL_HOST, settings.EMAIL_PORT) print('邮件发送OK...') except Exception as e: print(e) print('邮件发送失败...') return redirect(reverse('user_list'))
def test_create_user_valid_data(self): form = CreateUserForm( data={ 'username': '******', 'birthday': datetime.datetime.today(), 'password1': 'Test!2#4', 'password2': 'Test!2#4', }) self.assertTrue(form.is_valid())
def post(self, request): user_form = CreateUserForm(request.POST or None) if user_form.is_valid(): Users.create_or_update(**user_form.cleaned_data) data = { 'status': 'ok', } return JsonResponse(data=data) else: data = {'status': 'error', 'errors': user_form.errors} return JsonResponse(data=data)
def create(request): if request.method == 'POST': form = CreateUserForm(request.POST) if form.is_valid(): new_user = form.save() new_user = authenticate(username=form.cleaned_data['username'], password=form.cleaned_data['password1']) login(request, new_user) return redirect('index') else: form = CreateUserForm() return render(request, 'users/create.html', {'form': form})
def signup(request): if request.is_ajax(): if request.POST: form = CreateUserForm(request.POST) if form.is_valid(): form.save() user = auth.authenticate( username=form.cleaned_data["username"], password=form.cleaned_data["password1"] ) auth.login(request, user) msg = json.dumps({"status": "ok"}) else: msg = json.dumps({"status": "error", "errors": form.errors}) return HttpResponse(msg) else: raise Http404
def post(self, request, *args, **kwargs): """ 创建管理员用户 """ form = CreateUserForm(request.data) if not form.is_valid(): return Response({'Detail': form.errors}, status=status.HTTP_400_BAD_REQUEST) cld = form.cleaned_data does_username_exist = self.does_username_exist(cld['username']) if does_username_exist: return Response({'Detail': 'The phone number does exist.'}) try: user = User.objects.create_superuser(**cld) except Exception as e: return Response({'Detail': e.args}, status=status.HTTP_400_BAD_REQUEST) serializer = UserSerializer(user) return Response(serializer.data, status=status.HTTP_201_CREATED)
def post(self, request, *args, **kwargs): """ 用户注册 """ form = CreateUserForm(request.data) if not form.is_valid(): return Response({'Detail': form.errors}, status=status.HTTP_400_BAD_REQUEST) cld = form.cleaned_data result = verify_identifying_code(cld) if isinstance(result, Exception): return Response({'Detail': result.args}, status=status.HTTP_400_BAD_REQUEST) try: user = ConsumerUser.objects.create_user(**cld) except Exception as e: return Response({'Detail': e.args}, status=status.HTTP_400_BAD_REQUEST) serializer = UserInstanceSerializer(user) return Response(serializer.data, status=status.HTTP_201_CREATED)
def post(self, request): user_form = CreateUserForm(request.POST) profile_form = CreateProfileForm(request.POST) uid = request.POST.get('uid', None) user = User.objects.get(pk=uid) groups = Group.objects.all() if user_form.is_valid() and profile_form.is_valid(): print(user_form.cleaned_data) user.username = user_form.cleaned_data['username'] user.email = user_form.cleaned_data['email'] user.is_superuser = user_form.cleaned_data['is_superuser'] user.save() print('用户表更新完成OK...') profile = Profile.objects.filter(user=user) profile.update( name=profile_form.cleaned_data['name'], lnvalid_date=profile_form.cleaned_data['lnvalid_date'], phone=profile_form.cleaned_data['phone'], weixin=profile_form.cleaned_data['weixin'], info=request.POST.getlist('info', None)[0]) print('Profile表更新完成OK...') if request.POST.getlist('groups', None): user.groups.set(request.POST.getlist('groups')) else: user.groups.clear() print('用户组更新完成OK....') return render(request, 'user/user_modify.html', { "user": user, "groups": groups })
def signup(request): if request.method == 'POST': form = CreateUserForm(request.POST) if form.is_valid(): form.save() login(request, form.instance) return redirect('posts_list') else: form = CreateUserForm() return render(request, 'users/signup.html', {'form': form})
def register(request): if request.method == 'GET': user_form = CreateUserForm() return render(request, 'register.html', { 'form': user_form, 'student_role': RoleChoices.STUDENT }) else: user_form = CreateUserForm(request.POST.copy()) user_role = user_form['role'].value() if not user_role: user_form.data['role'] = RoleChoices.STUDENT.name if user_form.is_valid(): user_form.save() return redirect('login') else: return render(request, 'register.html', {'form': user_form})
def create_user(request): forms = CreateUserForm() if request.method == 'POST': forms = CreateUserForm(request.POST) if forms.is_valid(): username = forms.cleaned_data['username'] password = forms.cleaned_data['password'] re_password = forms.cleaned_data['re_password'] if password == re_password: user_obj = User.objects.create(username=username, password=password) new_user = forms.save(commit=False) new_user.user = user_obj new_user.save() return redirect('create-employee') context = {"forms":forms} return render(request,'pages/createuser.html',context)
def test_create_user_no_data(self): form = CreateUserForm(data={}) self.assertFalse(form.is_valid()) self.assertEquals(len(form.errors), 4)
def FunctionHomePage(request): if request.method == "GET": form = CreateUserForm() context = {'form': form} return render(request, 'templates/home_fora_do_webapp/index.html', context) if request.method == 'POST': username = request.POST.get('username_login') password = request.POST.get('password_login') user = authenticate(request, password=password, username=username) if user is not None: login(request, user) return redirect(reverse('app_home')) form = CreateUserForm(request.POST) if form.is_valid(): user = form.save(commit=False) user.backend = "django.contrib.auth.backends.ModelBackend" form.save() login(request, user) conta_inicial = Contas_bancarias(None, user.id, "Carteira", 0) conta_inicial.save() categorias_despesa = [ 'Mercado', 'Restaurante', 'Lazer', 'Combustível', 'Cartão de Crédito', 'Curso', 'Academia', ] categorias_receita = [ 'Salário', 'Vendas', 'Aluguel Terceiro', 'Investimentos', ] categoria_transferência = ["Transferência", "Portabilidade"] for categoria in categorias_receita: categoria_inicial = Categoria_transacao(id=None, user_id_id=user.id, categoria=categoria, ativo=1, classe_transacao=1) categoria_inicial.save() for categoria in categorias_despesa: categoria_inicial = Categoria_transacao(id=None, user_id_id=user.id, categoria=categoria, ativo=1, classe_transacao=2) categoria_inicial.save() #Categoria Transferência categoria_inicial = Categoria_transacao( id=None, user_id_id=user.id, categoria=categoria_transferência, ativo=1, classe_transacao=3) categoria_inicial.save() return redirect(reverse("app_home")) else: return render(request, "templates/home_fora_do_webapp/index.html", {'form': form})
def test_substitute_form_validity_with_different_datas(self): """Test form.is_valid() for different configurations of receive datas""" request = {'username': '******', 'email': '*****@*****.**', 'password1': 'sdfuhosd', 'password2': 'sdfuhosd'} form = CreateUserForm(data=request) self.assertTrue(form.is_valid()) form = CreateUserForm(data={'username': '******', 'password1': 'sdfuhosd', 'password2': 'sdfuhosd'}) self.assertTrue(form.is_valid()) form = CreateUserForm(data={'username': '******', 'password1': 'sdfuhosd'}) self.assertFalse(form.is_valid()) form = CreateUserForm(data={'username': '******', 'email': '*****@*****.**'}) self.assertFalse(form.is_valid()) form = CreateUserForm(data={'email': '*****@*****.**', 'password1': 'sdfuhosd', 'password2': 'sdfuhosd'}) self.assertFalse(form.is_valid())