def address_edit(request): if request.method == 'GET': # 接收参数 id = request.GET.get('id') # 获得地址 content = Addreses.objects.get(pk=id) # 准备渲染数据 context = {'edit': content} return render(request, 'address_edit.html', context) # post修改请求 else: data = request.POST # 修改数据 # 获取数据 # 获取ID try: id = data.get('id') except: # 检测防止非法用户篡改ID值 return redirect('user:gladdress') # 使用form表单验证数据 form = AddressForm(data) if form.is_valid(): # 清洗数据为True时 # 得到清洗后的数据 cleaned_data = form.cleaned_data Addreses.objects.filter(pk=id).update(**cleaned_data) # 更新数据 return redirect('user:gladdress') # 更新成功后跳转至列表页 else: # 清洗失败时,返回错误,并回显 content = Addreses.objects.get(pk=id) context = {'errors': form.errors, 'edit': content} return render(request, 'address_edit.html', context)
def user_site(request): if request.method == 'GET': user_id = request.session.get('user_id') user_address = UserAddress.objects.filter(user_id=user_id) activate = 'site' return render(request, 'user_center_site.html', { 'user_address': user_address, 'activate': activate }) if request.method == 'POST': form = AddressForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] address = form.cleaned_data['address'] postcode = form.cleaned_data['postcode'] mobile = form.cleaned_data['mobile'] user_id = request.session.get('user_id') UserAddress.objects.create(user_id=user_id, address=address, signer_name=username, signer_mobile=mobile, signer_postcode=postcode) return HttpResponseRedirect(reverse('user:user_site')) else: errors = form.errors return render(request, 'user_center_site.html', { 'errors': errors, })
def post(self, request): if request.session.get('id'): id = Members.objects.get(user_ID=request.session.get('id')).pk # 获取用户id data = request.POST form = AddressForm(data) # 清洗数据 if form.is_valid(): cleaned_data = form.cleaned_data # 得到清洗成功的后的数据 # 检测用户收货地址是否超过六条 if Addreses.objects.filter(add_user=id).count() < 6: # 将地址添加进数据库,持久保存 if cleaned_data.get('is_default'): # 如果设置为默认,就将全部的设置为False Members.objects.get(pk=id).addreses_set.filter(is_delete=False).update(is_default=False) # 重新设置当前的地址为默认的 Members.objects.get(pk=id).addreses_set.create(**cleaned_data) else: # 如果不是默认的,就直接保存 Members.objects.get(pk=id).addreses_set.create(**cleaned_data) else: context = {'error': '单个用户地址数不能大于6'} return render(request, 'address.html', context) return redirect('user:gladdress') else: context = {'errors': form.errors, 'data': data} return render(request, 'address.html', context) else: return redirect('user:login')
def post(self, request): # 接收参数,并转换成字典 data = request.POST.dict() # 字典保存用户 data['user_id'] = request.session.get("ID") # form自动转换功能 # 验证参数 form = AddressForm(data) if form.is_valid(): form.instance.user = User.objects.get(pk=data['user_id']) form.save() return JsonResponse(json_msg(0, '添加成功')) else: return JsonResponse(json_msg(1, '添加失败', data=form.errors))
def user_center_site(request): if request.method == 'GET': user_addr = UserAddress.objects.filter(user=request.user).first() return render(request, 'user_center_site.html',{'user_addr': user_addr}) if request.method == 'POST': form = AddressForm(request.POST) if form.is_valid(): user_addr = UserAddress.objects.create(user=request.user, signer_mobile=form.cleaned_data.get('phone'), signer_name=form.cleaned_data.get('addressee'), signer_postcode=form.cleaned_data.get('zip_code'), address=form.cleaned_data.get('detailed_address')) if user_addr: return render(request, 'user_center_site.html',{'user_addr': user_addr}) else: return render(request, 'user_center_site.html', {'errors': form.errors})
def post(self, request): json_data = request.body if not json_data: return to_json(errno=Code.PARAMERR, errmsg="参数错误") dict_data = json.loads(json_data) form = AddressForm(data=dict_data, user_id=request.user.id) if form.is_valid(): return to_json(errmsg="地址添加成功!") err_msg_list = [] for item in form.errors.values(): err_msg_list.append(item[0]) err_str = "/".join(err_msg_list) return to_json(errno=Code.PARAMERR, errmsg=err_str)
def add_address(request): if request.method == 'POST': form = AddressForm(request.POST) if form.is_valid(): form_data = form.cleaned_data Address.objects.create( country=form_data['country'], region=form_data['region'], city=form_data['city'], street=form_data['street'], bilding_type=form_data['bilding_type'], numberbild=form_data['numberbild'], kv=form_data['kv'], ) id_current_user = request.session['entry_user'] u = PhysicalUser.objects.get(id=id_current_user) ctx = {} form = StatementStateRegistrationForm() ctx['user'] = u ctx['form'] = form return render(request, 'statement_state_registration_form.html', ctx)
def create_property(request): if not request.user.userprofile.is_landlord: messages.error(request, "you cant do that") return redirect('rentals-home') else: if request.method == 'POST': form = PropertyForm(request.POST, request.FILES) address_form = AddressForm(request.POST) if form.is_valid() and address_form.is_valid(): a = address_form.save() p = Property.objects.create(address=a, posted_by=request.user.userprofile, price_per_day=form.cleaned_data.get('price_per_day'), property_description=form.cleaned_data.get('property_description'), thumbnail=form.cleaned_data.get('thumbnail'), ) print(f'created property: {p}') messages.success(request, f"{p}") return redirect('list-listings') else: messages.error(request, f" could not create property") print(f'there was a problem... af{address_form.is_valid()}, form:{form.is_valid()}') return redirect('prop-create') else: form = PropertyForm() address_form = AddressForm() context = { 'form': form, 'address_form': address_form } return render(request, 'rentals/create_property_posting.html', context=context)
def edit_listing(request, listing_pk): p = Property.objects.get(pk=listing_pk) if request.method == 'POST': prop = PropertyForm(request.POST, request.FILES) adr = AddressForm(request.POST) if prop.is_valid() and adr.is_valid(): p.address.street_address = adr.cleaned_data['street_address'] p.address.country = adr.cleaned_data['country'] p.address.city = adr.cleaned_data['city'] p.address.zip_code = adr.cleaned_data['zip_code'] p.address.state = adr.cleaned_data['state'] p.address.save() p.save() p.price_per_day = prop.cleaned_data.get('price_per_day') p.property_description = prop.cleaned_data.get('property_description') p.save() p.thumbnail = prop.cleaned_data.get('thumbnail') p.save() messages.success(request, f"saved") return redirect('list-listings') else: print('failed') messages.error(request, f"could not make reservation") return redirect('prop-detail', listing_pk) else: prop = PropertyForm(instance=p) adr = AddressForm(instance=p.address) context = { 'form': prop, 'address': adr, } return render(request, 'rentals/edit_listing.html', context=context)
def profile_view(request): user = request.user if not user.is_authenticated: return custom_redirect('anonymous_profile', order_id=request.GET.get('order_id'), success_pay=request.GET.get('success_pay')) if request.method == 'GET': if request.GET.get('method') == 'get_order': try: order = Order.objects.get(id=request.GET.get('order_id')) order_response = render_template('order_expand.html', {'order': order}, request) return JsonResponse({ 'order_response': order_response, 'result': 'success' }) except: return JsonResponse({'result': 'error'}) if request.GET.get('method') == 'order_confirm_delivery': try: order = Order.objects.get(id=request.GET.get('order_id')) order.send_confirm = True order.save() return JsonResponse({'result': 'success'}) except: return JsonResponse({'result': 'error'}) context = {} context['message_act'] = 'false' context['success_pay'] = 'False' order_id = request.GET.get('order_id') if str(order_id) != 'None' and str( request.GET.get('success_pay')) == 'None': context['order_id'] = order_id user_form = UserForm(instance=request.user) address_form = AddressForm(instance=request.user.profile) password_change_form = PasswordChangeForm() response = status_pay(order_id) url = reverse('profile') if response['ret_status'] != 'error': if response['status'] == 'authorized' or response[ 'status'] == 'acknowledged': url += '?success_pay=True&order_id=' + order_id context['success_pay'] = 'True' context.update({ 'redirect_url': url, 'user_form': user_form, 'address_form': address_form, 'with_save': "true", 'password_change_form': password_change_form }) getDataForNavBaR(context, request) return render(request, 'profile.html', context) elif str(order_id) != 'None' and str( request.GET.get('success_pay')) == 'True': pay_order = Order.objects.get(id=order_id) response = status_pay(pay_order.id) if response['ret_status'] != 'error': if pay_order.pay == False and ( response['status'] == 'authorized' or response['status'] == 'acknowledged' ) and pay_order.status == 'registered': pay_order.pay = True pay_order.incr_buy_for_items() pay_order.status = 'ready_to_send' pay_order.save() notify_admin(request, pay_order) context['message_act'] = 'true' context['message'] = message('Заказ совершен!', request) order_update(request) orders = Order.objects.filter(Q(user=user) & Q(pay=True)) user_form = UserForm(instance=request.user) address_form = AddressForm(instance=request.user.profile) password_change_form = PasswordChangeForm() context.update({ 'orders': orders, 'user_form': user_form, 'address_form': address_form, 'with_save': "true", 'password_change_form': password_change_form }) getDataForNavBaR(context, request) return render(request, 'profile.html', context) if request.method == 'POST': type_post = request.POST.get('type_post') if type_post == "user_form": user_form = UserForm(request.POST, instance=request.user) if user_form.is_valid(): user_form.save() user_change_form_response = render_template( 'user_change.html', {'user_form': user_form}, request) return JsonResponse({ 'user_change_form_response': user_change_form_response, 'result': 'success', 'message': message("Профиль изменен!", request) }) else: user_change_form_response = render_template( 'user_change.html', {'user_form': user_form}, request) return JsonResponse({ 'user_change_form_response': user_change_form_response, 'result': 'error' }) else: address_form = AddressForm(request.POST, instance=request.user.profile) if address_form.is_valid(): address_form.save() address_change_form_response = render_template( 'address_change.html', { 'address_form': address_form, 'with_save': "true" }, request) return JsonResponse({ 'address_change_form_response': address_change_form_response, 'result': 'success', 'message': message("Адрес изменен!", request) }) else: address_change_form_response = render_template( 'address_change.html', { 'address_form': address_form, 'with_save': "true" }, request) return JsonResponse({ 'address_change_form_response': address_change_form_response, 'result': 'error' })
def to_order_view(request): if request.method == 'GET': method = request.GET.get('method') if method == "check_form": address_form = AddressForm(request.GET) if address_form.is_valid(): info = address_form.cleaned_data normalize = pr_address_normalize(info['country'], info['region'], info['area'], info['city'], info['street'], info['house'], info['room'], info['index']) if normalize['ret_status'] == 'success': delivery_total_ret = pr_pre_calc( normalize['body']['index']) delivery_total = delivery_total_ret['total_nds'] delivery_max_day = delivery_total_ret['max-days'] delivery_min_day = delivery_total_ret['min-days'] address_change_form_response = render_template( 'address_change.html', { 'address_form': address_form, 'with_save': "true" }, request) cart = getUserCart(request) if cart.items.count() == 0: return JsonResponse({'result': 'error'}) address_form.set_readonly() sale_code = request.GET.get('sale_code') sale_result = 0 try: sale = ProductSale.objects.get(promo=sale_code) val = cart.price_all * Decimal(sale.sale) / 100 delivery_and_cart_price = cart.price_all - val + delivery_total sale_result = sale.sale except: delivery_and_cart_price = cart.price_all + delivery_total order_body_response = render_template( 'order_body.html', { 'delivery_max_day': delivery_max_day, 'delivery_min_day': delivery_min_day, "delivery_and_cart_price": delivery_and_cart_price, 'delivery_price': delivery_total, 'cart': cart, 'address_form': address_form, 'with_save': "false", 'sale': sale_result }, request) return JsonResponse({ 'result': 'success', 'address_change_form_response': address_change_form_response, 'order_body_response': order_body_response }) else: if request.user.is_authenticated: with_save = "true" else: with_save = "false" address_change_form_response = render_template( 'address_change.html', { 'address_form': address_form, 'with_save': with_save }, request) return JsonResponse({ 'address_change_form_response': address_change_form_response, 'result': 'error' }) else: if request.user.is_authenticated: with_save = "true" else: with_save = "false" address_change_form_response = render_template( 'address_change.html', { 'address_form': address_form, 'with_save': with_save }, request) return JsonResponse({ 'address_change_form_response': address_change_form_response, 'result': 'error' }) elif method == "order_confirm": address_form = AddressForm(request.GET) if address_form.is_valid(): info = address_form.cleaned_data normalize = pr_address_normalize(info['country'], info['region'], info['area'], info['city'], info['street'], info['house'], info['room'], info['index']) if normalize['ret_status'] == 'success': delivery_total_ret = pr_pre_calc( normalize['body']['index']) delivery_total = delivery_total_ret['total_nds'] cart = getUserCart(request) if cart.items.count() == 0: return JsonResponse({'result': 'error'}) order = Order() order.save() address_form = AddressForm(request.GET, instance=order) if not address_form.is_valid(): return JsonResponse({'result': 'error'}) address_form.save() order.order_from_cart(cart) order.delivery_price = delivery_total sale_code = request.GET.get('sale_code') try: sale = ProductSale.objects.get(promo=sale_code) val = order.price_all * Decimal(sale.sale) / 100 order.delivery_cart_price = delivery_total + order.price_all - val if sale.promo == 'delivery': order.delivery_cart_price = order.price_all order.sale = sale.sale order.promo = sale.promo except: order.delivery_cart_price = delivery_total + order.price_all order.save() cart.remove_all_from_cart(delete=False) if request.user.is_authenticated == False: from_ = str(request.session.session_key) order.sk = from_ order.save() user_id = order.sk else: user_id = order.user.id from_ = str(order.user.id) responce = register_pay(request, order.id, order.delivery_cart_price, user_id, order.name, order.email, order.phone) if responce['ret_status'] == 'success': order.status = 'registered' order.archive() order.save() return JsonResponse({ 'result': 'success', 'url': responce['redirect_url'] + "?session=" + responce['session'] }) else: cart.cart_from_order(order) order.delete() return JsonResponse({ 'result': 'error', 'message_act': 'true', 'message': message('Произошла ошибка, попробуйте еще раз!', request), }) else: return JsonResponse({'result': 'error'}) else: return JsonResponse({'result': 'error'}) elif method == "calc": address_form = AddressForm(request.GET) info = address_form.data normalize = pr_address_normalize(info['country'], info['region'], info['area'], info['city'], info['street'], info['house'], info['room'], info['index']) if normalize['ret_status'] == 'success': delivery_total_ret = pr_pre_calc(normalize['body']['index']) delivery_total = delivery_total_ret['total_nds'] return JsonResponse({ 'result': 'success', "delivery_total": delivery_total }) else: return JsonResponse({'result': 'error', "delivery_total": "0"}) elif method == "check_sale": sale_code = request.GET.get('sale_code') try: sale = ProductSale.objects.get(promo=sale_code) return JsonResponse({ 'result': 'success', "sale": str(sale.sale) }) except: return JsonResponse({'result': 'error', "sale": "0"})