def patch(self, request): promote_form = PromoteForm(request.PATCH) if not promote_form.is_valid(): return HttpResponseBadRequest(get_error_json(promote_form), content_type='application/json') num = request.PATCH.get('num', '') if int(num) < 0: return HttpResponseBadRequest( '{"status":"error", "error":1003, "msg":"算力提升不能为负数"}', content_type='application/json') mining = Mining.objects.get(user=request.user) mining.force += int(num) mining.save() return HttpResponse('{"status": "success"}', content_type='application/json')
def post(self, request): login_form = LoginForm(request.POST) if not login_form.is_valid(): return HttpResponseBadRequest(get_error_json(login_form), content_type='application/json') user_name = request.POST.get("username", "") pass_word = request.POST.get("password", "") # 成功返回user对象,失败返回null user = authenticate(username=user_name, password=pass_word) if user is None: return HttpResponseBadRequest('{"status":"error", "error":1003, "msg":"用户名或者密码错误"}', content_type='application/json') login(request, user) return HttpResponse('{"status":"success"}', content_type='application/json')
def patch(self, request): extract_form = ExtractForm(request.PATCH) if not extract_form.is_valid(): return HttpResponseBadRequest(get_error_json(extract_form), content_type='application/json') wallet = Wallet.objects.get(user=request.user) num = request.PATCH.get('num', '') if wallet.money < float(num) or float(num) <= 0: return HttpResponseBadRequest( '{"status":"error", "error":1003, "msg":"提取不能为负数或者钱包余额不足"}', content_type='application/json') wallet.money -= float(num) wallet.save() return HttpResponse('{"status": "success"}', content_type='application/json')
def post(self, request): register_form = RegisterForm(request.POST) if not register_form.is_valid(): # 不通过验证 return HttpResponseBadRequest(get_error_json(register_form), content_type='application/json') user_name = request.POST.get("username", "") if UserProfile.objects.filter(username=user_name): # 此用户已经存在 return HttpResponseBadRequest('{"status":"error", "error":1002, "msg":"此用户已经存在"}', content_type='application/json') email = request.POST.get("email", "") pass_word = request.POST.get("password", "") name = request.POST.get("name", "") user_profile = UserProfile() user_profile.username = user_name user_profile.email = email user_profile.password = make_password(pass_word) user_profile.name = name user_profile.save() wallet = create_wallet(user_profile) create_mining(user_profile, wallet) return HttpResponse('{"status":"success"}', content_type='application/json')
def patch(self, request): transfer_form = TransferForm(request.PATCH) if not transfer_form.is_valid(): return HttpResponseBadRequest(get_error_json(transfer_form), content_type='application/json') send_wallet = Wallet.objects.get(user=request.user) # 转出钱包,当前登录用户的钱包 address = request.PATCH.get('receive', '') receives = Wallet.objects.filter(address=address) if not receives: # 如果接收钱包不存在 return HttpResponseBadRequest( '{"status":"error", "error":1001, "msg":"钱包填写错误"}', content_type='application/json') receive_wallet = receives[0] # 接收钱包 num = request.PATCH.get('num', '') if send_wallet.money < float(num) or float(num) <= 0: return HttpResponseBadRequest( '{"status":"error", "error":1003, "msg":"转账不能为负数或者钱包余额不足"}', content_type='application/json') send_wallet.money -= float(num) send_wallet.save() receive_wallet.money += float(num) receive_wallet.save() return HttpResponse('{"status": "success"}', content_type='application/json')