def create(self, value): password = value.pop('password') value['display_name'] = value['account'] oc_user = User( **value ) oc_user.password = make_password(password) oc_user.save() return oc_user
def register(): if request.method == 'GET': return render_template('register.html') if request.method == 'POST': re_dict = request.form moble = re_dict.get('mobile') password = re_dict.get('password') password2 = re_dict.get('password2') # all(列表)如果有空字符串返回False if not all([moble, password, password2]): return jsonify(USER_REGISTER_PARAMS_ERROR) if not re.match(r'^1[34578]\d{9}$', moble): return jsonify(USER_REGISTER_MOBILE_ERROR) if User.query.filter(User.phone == moble).count(): return jsonify(USER_REGISTER_MOBILE_IS_EXSITS) if password != password2: return jsonify(status_code.USER_REGISTER_PASSWORD_IS_ERROR) users = User() users.phone = moble users.name = moble users.password = password try: users.add_update() return jsonify(status_code.SUCCESS) except: return jsonify(status_code.DATABASE_ERROR)
def post(self, request): password = request.POST.get('password', '') telephone = request.POST.get('telephone', '') action = request.POST.get('action', '') # if not (password or telephone or action): # msg = message(msg='请求信息不全!') # return JsonResponse(msg) # 登陆 if action == 'login': password = make_security(password.encode('utf8')) user = User.objects.filter(telephone=telephone, password=password).first() if user: user_json = user.tojson_except_evaluation() user_json['msg'] = "登录成功!" user_json['status'] = 'success' user_json['access_token'] = user.access_token return JsonResponse(user_json) else: msg = message(msg='账户或密码错误!') return JsonResponse(msg) # 注册 elif action == 'register': password = make_security(password.encode('utf8')) access_token = make_security((telephone + password).encode('utf8')) user = User(name=telephone[:2] + telephone[9:], password=password, telephone=telephone, access_token=access_token) try: user.save() except IntegrityError as ie: print(ie) msg = message(msg='手机重复!') return JsonResponse(msg) msg = message(msg='注册成功!', status='success') return JsonResponse(msg) # 修改 elif action == 'alter': access_token = request.META.get(ACCESS_TOKEN, '') if access_token: user = User.objects.filter(access_token=access_token).first() name = request.POST.get('name', '') if name: user.name = name user.save() else: user_form = UserForm(request.POST, request.FILES, instance=user) if user_form.is_valid(): user_form.save() msg = message(msg='修改成功!', status='success') return JsonResponse(msg) msg = message(msg='请求信息不全!') return JsonResponse(msg)
def post(self,request): try: data = request.data serializer = RegisterSerializer(data=data) is_valid = serializer.is_valid(raise_exception=True) if is_valid: if(User.objects.filter(Q(username=request.data["email"])| Q(phone_number=request.data["phone_number"]))): return Response({"status": "failure", "message": "User Already Exists"},status=status.HTTP_400_BAD_REQUEST) else: user = User(username=request.data["email"],phone_number=request.data["phone_number"],) if(request.data["confirm_password"] == request.data["password"]): user.set_password(request.data["password"]) user.save() return Response({"status": "success", "message": "User Registration Successful"}) else: return Response({"status": "failure", "message": "enter correct password"},status=status.HTTP_400_BAD_REQUEST) except Exception as e: print('Exception {}'.format(e.args)) try: error_message = e.args if not isinstance(e.args, tuple) and not isinstance(e.args, list) else e.args[0] if isinstance(error_message, list): error_message = error_message[0] if isinstance(error_message, dict): error = list(error_message.values())[0] if isinstance(error, list): error_message = error[0] else: error_message = error except Exception: error_message = 'Please check the values' return Response({'status': 'failure', 'message': error_message}, status=status.HTTP_400_BAD_REQUEST)
def register(request): username = request.POST.get("username") hobby = request.POST.getlist("hobby") realname = request.POST.get("realname") password = request.POST.get("password") gender = request.POST.get("gender") # "1" "0" birth = request.POST.get("birth") # 规范日期格式 salt = ''.join(random.sample(string.ascii_letters + string.digits, 8)) pwd = make_password(password, salt) # 传入明文,返回密文(盐=随机的字符串) userPicFile = request.FILES.get('user_pic') # 接收文件 # print("文件原始名为:", userPicFile.name, " 文件类型为:", userPicFile.content_type) # 2.将数据存入数据库 h = "" for i in hobby: h += i + " " if (userPicFile): User(username=username, hobby=h, realname=realname, password=pwd, salt=salt, gender=gender, birth=birth, pic=userPicFile).save() else: User(username=username, hobby=h, realname=realname, password=pwd, salt=salt, gender=gender, birth=birth).save() # 响应 print("注册成功~~~~") return redirect("user:loginp")
def register(request): if request.method == 'POST': loginobj = Login() loginobj.username = request.POST.get('uname') loginobj.password = request.POST.get('pwd') loginobj.save() if Login.objects.filter(username=request.POST.get('uname')).exists(): user = User() user.name = request.POST.get('name') user.dob = request.POST.get('dob') user.email = request.POST.get('email') user.mobile = request.POST.get('mob') user.loginid = loginobj user.save() if User.objects.filter(loginid=loginobj).exists(): categories = Category.objects.all().filter(status=1) packages = Package.objects.all()[:10] bestsellers = Package.objects.order_by('-sold_package')[:10] return render(request, "user/login.html", context={ 'categories': categories, 'bestsellers': bestsellers, 'packages': packages }) return render(request, "user/register.html", context={'error': 'Registration failed'}) categories = Category.objects.all().filter(status=1) bestsellers = Package.objects.order_by('-sold_package')[:10] return render(request, "user/register.html", context={ 'categories': categories, 'bestsellers': bestsellers })
def register(postvars=None, method='GET'): if method == 'POST': username = postvars.get(b'username')[0].decode("utf-8") email = postvars.get(b'email')[0].decode("utf-8") country = postvars.get(b'country')[0].decode("utf-8") password = postvars.get(b'password')[0].decode("utf-8") with session_scope() as session: q = session.query(User).filter_by(username=username).first() session.commit() if not q: user = User(username=username, email=email, password=password, country=country, active=True) session.add(user) session.commit() return (301, '/list', False) template = settings.template_env.get_template('register.html') return (200, template.render(), False)
def post_page(request, pk): post = Post.objects.get(pk=pk) if request.method == 'POST': if not session_logged_in(request): return HttpResponseRedirect('/') if request.POST.get('comment-id'): comment = Comment.objects.get(pk=request.POST['comment-id']) comment.delete() return HttpResponseRedirect(request.path_info) else: comment = Comment.objects.create(user=User.get_user(request), post=post, date=datetime.datetime.now(), content=request.POST['content']) comment.save() return HttpResponseRedirect(request.path_info) return render(request, 'post_page.html', { 'post': post, 'session': request.session })
def upload_form(request): if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): file_name = name_save_file(request.FILES['file']) file_path = os.path.join(IMAGES_DIR, file_name) if not is_image(file_path): os.remove(file_path) messages.add_message(request, messages.INFO, 'File is not an image') formset = UploadFileForm(request.POST) return render(request, 'upload_form.html', { 'formset': formset, 'session': request.session }) S3.upload_file(file_path, S3_BUCKET, 'media/' + file_name) new_post = Post.objects.new_post(request.POST['header'], file_name, User.get_user(request)) new_post.save() messages.add_message(request, messages.INFO, 'You have successfully uploaded an image') return HttpResponseRedirect('/') else: return HttpResponse('Form is not valid') else: formset = UploadFileForm(request.POST) return render(request, 'upload_form.html', { 'formset': formset, 'session': request.session })
def get_queryset(self): return super().get_queryset().filter(user=User.get_user(self.request))
def post(self, request): form = UserRegForm(request.POST) if form.is_valid(): password = form.cleaned_data['password'] cpassword = form.cleaned_data['confirm_password'] if (password == cpassword): password = make_password(password) cpassword = make_password(cpassword) user = User() user.name = form.cleaned_data['name'] user.username = form.cleaned_data['username'] user.password = password user.confirm_password = cpassword user.email = form.cleaned_data['email'] print(user.email) user.gender = form.cleaned_data['gender'] user.tags = form.cleaned_data['tags'] user.save() return redirect('/login/') else: return render(request, 'userruserreg.html', {'form': form}) else: return (request, 'userreg.html', {'form': form})
def setUp(self): self.users = User('The widget')