def test_reuse(self): self.coupon.redeemed_at = timezone.now() self.coupon.save() form_data = {'code': self.coupon.code} form = CouponForm(data=form_data, user=self.user) self.assertFalse(form.is_valid())
def test_expired(self): self.coupon.valid_until = timezone.localtime( timezone.now()) - timedelta(1) self.coupon.save() form_data = {'code': self.coupon.code} form = CouponForm(data=form_data, user=self.user) self.assertFalse(form.is_valid())
def test_reuse(self): self.coupon.redeem() self.coupon.save() form_data = {'code': self.coupon.code} form = CouponForm(data=form_data, user=self.user) self.assertFalse(form.is_valid())
def test_form_without_user(self): """ This should fail since the coupon is bound to an user, but we do not provide any user. """ form = CouponForm(data={'code': self.coupon.code}) self.assertFalse(form.is_valid()) self.assertEquals( form.errors, {'code': ['This code is not valid for your account.']})
def test_redeem_with_user_twice(self): self.test_redeem_with_user() # try to redeem again with form form = CouponForm(data={'code': self.coupon.code}, user=self.user) self.assertFalse(form.is_valid()) self.assertEquals(form.errors, {'code': ['This code has already been used.']})
def test_form_with_invalid_user(self): """ This should fail since the coupon is bound to an user different to the one provided. """ form = CouponForm(data={'code': self.coupon.code}, user=User.objects.create(username="******")) self.assertFalse(form.is_valid()) self.assertEquals( form.errors, {'code': ['This code is not valid for your account.']})
def test_user_bound(self): self.user = User(username="******") self.user.save() CouponUser.objects.create(coupon=self.coupon, user=self.user) form_data = {'code': self.coupon.get_bulk_code(5)} form = CouponForm(data=form_data, user=self.user) self.assertTrue(form.is_valid())
def test_form_without_user(self): """ This should fail since the coupon is bound to an user, but we do not provide any user. """ form = CouponForm(data={'code': self.coupon.code}) self.assertFalse(form.is_valid()) self.assertEquals( form.errors, {'code': ['This code is not valid for your account.']} )
def test_form_without_user(self): """ This should fail since we cannot track single use of a coupon without an user. """ form = CouponForm(data={'code': self.coupon.code}) self.assertFalse(form.is_valid()) self.assertEquals( form.errors, {'code': ['The server must provide an user to this form to allow you to use this code. Maybe you need to sign in?']} )
def test_form_without_user(self): """ This should pass even though the coupon is bound to a user but we have no way of knowing if this user is part of that set because the form has no user. """ form = CouponForm(data={'code': self.coupon.code}) self.assertTrue(form.is_valid())
def test_already_used_fails(self): code = self.coupon.get_bulk_code(1) CouponUser.objects.create(coupon=self.coupon, code=code, redeemed_at=timezone.now()) form_data = {'code': code} form = CouponForm(data=form_data) self.assertFalse(form.is_valid())
def test_redeem_with_user_twice(self): self.test_redeem_with_user() # try to redeem again with form form = CouponForm(data={'code': self.coupon.code}, user=self.user) self.assertFalse(form.is_valid()) self.assertEquals( form.errors, {'code': ['This code has already been used.']} )
def test_redeem_same_code_twice(self): self.test_redeem_with_user() # try to redeem again with form user = User.objects.create(username="******") form = CouponForm(data={'code': self.bulk_code}, user=user) self.assertFalse(form.is_valid()) self.assertEquals( form.errors, {'code': ['This code has already been used.']} )
def test_none_coupon_user(self): CouponUser.objects.create(coupon=self.coupon) self.assertTrue(self.coupon.users.count(), 1) self.assertIsNone(self.coupon.users.first().user) self.assertIsNone(self.coupon.users.first().redeemed_at) form_data = {'code': self.coupon.code} form = CouponForm(data=form_data, user=self.user) self.assertTrue(form.is_valid())
def test_form_without_user(self): """ This should fail since the coupon is bound to an user, but we do not provide any user. """ form = CouponForm(data={'code': self.coupon.code}) self.assertFalse(form.is_valid()) self.assertEquals( form.errors, { 'code': [ 'This user bound code requires a user instance to check against.' ] })
def test_form_without_user(self): """ This should fail since we cannot track single use of a coupon without an user. """ form = CouponForm(data={'code': self.coupon.code}) self.assertFalse(form.is_valid()) self.assertEquals( form.errors, { 'code': [ 'The server must provide an user to this form to allow you to use this code. Maybe you need to sign in?' ] })
def test_redeem_via_form_without_user(self): form = CouponForm(data={'code': self.coupon.code}) # form should be valid self.assertTrue(form.is_valid()) # perform redeem self.coupon.redeem() # coupon should be redeemed properly now self.assertTrue(self.coupon.is_redeemed) self.assertEquals(self.coupon.users.count(), 1) self.assertIsInstance(self.coupon.users.first().redeemed_at, datetime) self.assertIsNone(self.coupon.users.first().user) # form should be invalid after redeem self.assertTrue(form.is_valid())
def new_coupon_page(request): if request.method == 'POST': # POST form = CouponForm(request.POST) if form.is_valid(): # save to DB form.save() return render(request, 'coupons/coupon_created.html', { 'form': form, }) else: # GET # display coupon new_coupon = Coupon.objects.create_coupon() form = CouponForm(instance=new_coupon) return render(request, 'coupons/new_coupon.html', { 'form': form, })
def apply_coupon(request): now = timezone.now() coupon_form = CouponForm(request.POST) if coupon_form.is_valid(): code = coupon_form.cleaned_data['code'] try: coupon = get_object_or_404( Coupon, code=code, is_actived=True, valid_from__lt=now, valid_to__gt=now, ) request.session['coupon_id'] = coupon.id except Http404: messages.warning(request, f"优惠劵代码错误/已过期/已失效") request.session['coupon_id'] = None return redirect('cart:cart_detail')
def cart_detail(request): cart = Cart(request) for item in cart: item['update_product_quantity'] = CartForm(initial={ 'quantity': item['quantity'], 'update': True }) form = CouponForm() return render(request, 'cart_detail.html', {'cart': cart, 'form': form})
def cart_detail(request): cart = Cart(request) for item in cart: item["update_quantity_form"] = CartAddProductForm( initial={ "quantity": item["quantity"], "update": True }) coupon_apply_form = CouponForm() return render(request, "cart/detail.html", { "cart": cart, "coupon_apply_form": coupon_apply_form })
def cart_detail(request): cart = Cart(request) for item in cart: item['update_quantity_form'] = CartAddProductForm( initial={ 'quantity': item['quantity'], 'update': True }) coupon_apply_form = CouponForm() return render(request, 'cart/cart.html', { 'cart': cart, 'coupon_apply_form': coupon_apply_form, })
def detail_panier(request): panier = Panier(request) next_lang = strip_language(request.path) if not request.user.is_authenticated: return redirect('accounts:login') for item in panier: item['form_update_quantite'] = AjouterProduitPanierForm( initial={ 'quantite': item['quantite'], 'update': True }) # On ajout le coupon au panier appliquer_le_coupon = CouponForm() context = { 'panier': panier, 'appliquer_le_coupon': appliquer_le_coupon, 'next': next_lang } template = 'panier/panier_detail.html' return render(request, template, context)
def test_coupon_form(self): form_data = {'code': '2zzx'} form = CouponForm(data=form_data) self.assertTrue(form.is_valid())
def test_expired(self): self.coupon.valid_until = timezone.now()-timedelta(1) self.coupon.save() form_data = {'code': self.coupon.code} form = CouponForm(data=form_data, user=self.user) self.assertFalse(form.is_valid())
def test_form_is_valid(self): form = CouponForm(data=self.data) self.assertTrue(form.is_valid())
def test_user(self): other_user = User(username="******") other_user.save() form_data = {'code': self.coupon.code} form = CouponForm(data=form_data, user=other_user) self.assertFalse(form.is_valid())
def test_types(self): form_data = {'code': self.coupon.code} form = CouponForm(data=form_data, user=self.user, types=('percentage',)) self.assertFalse(form.is_valid())
def test_wrong_code(self): form_data = {'code': 'foo'} form = CouponForm(data=form_data) self.assertFalse(form.is_valid())
def test_redeem_with_multiple_users(self): for i in range(100): user = User.objects.create(username="******" % (i)) form = CouponForm(data={'code': self.coupon.code}, user=user) self.assertTrue(form.is_valid())
def test_one_usage_okay(self): form_data = {'code': self.coupon.get_bulk_code(5)} form = CouponForm(data=form_data) self.assertTrue(form.is_valid())
def checkout(request): cart = Cart(request) if len(cart) == 0 or not cart: messages.warning(request, _("Your cart is empty")) return redirect("items:home") user = request.user def_billing_address = ( def_shipping_address ) = shipping_initial = billing_initial = None if request.user.is_authenticated: try: def_billing_address = Address.objects.default_billing(user)[0] def_shipping_address = Address.objects.default_shipping(user)[0] shipping_initial = def_shipping_address.__dict__ billing_initial = def_billing_address.__dict__ except (ObjectDoesNotExist, IndexError): pass if request.method == "POST": billing_form = BillingAddressForm( request.POST, prefix="billing", user=user, initial=billing_initial ) shipping_form = ShippingAddressForm( request.POST, prefix="shipping", user=user, initial=shipping_initial ) if billing_form.is_valid() and shipping_form.is_valid(): billing = billing_form.save(commit=False) shipping = shipping_form.save(commit=False) order = Order() if user.is_authenticated: billing.user = shipping.user = order.user = user if def_billing_address and not billing_form.has_changed(): order.billing_address = def_billing_address else: billing.save() order.billing_address = billing if def_shipping_address and not shipping_form.has_changed(): order.shipping_address = def_shipping_address else: shipping.save() order.shipping_address = shipping order.session = request.session.session_key if cart.coupon: order.coupon = cart.coupon order.discount = cart.get_discount() order.save() for item in cart: OrderItem.objects.create( order=order, item=item["item"], price=item["price"], quantity=item["quantity"], size=item["size"], ) return redirect("payments:order_payment", order_id=order.id) else: coupon_form = CouponForm() context = { "cart": cart, "shipping_form": ShippingAddressForm( prefix="shipping", initial=shipping_initial, user=user ), "billing_form": BillingAddressForm( prefix="billing", initial=billing_initial, user=user ), "coupon_form": coupon_form, } return render(request, "checkout.html", context)
def test_form_without_user(self): """ This should fail since we cannot track single use of a coupon without an user. """ form = CouponForm(data={'code': self.coupon.code}) self.assertFalse(form.is_valid()) self.assertEquals(form.errors, {'code': ['Please sign in to use this coupon.']})
def test_types(self): form_data = {'code': self.coupon.code} form = CouponForm(data=form_data, user=self.user, types=('percentage', )) self.assertFalse(form.is_valid())
def test_right_code(self): form_data = {'code': self.coupon.code} form = CouponForm(data=form_data, user=self.user) self.assertTrue(form.is_valid())
def test_form_without_user(self): """ This should pass, anonymous users can use coupon too if it has no limit """ form = CouponForm(data={'code': self.coupon.code}) self.assertTrue(form.is_valid())
def test_inactive_code(self): form_data = {'code': self.coupon.code} self.coupon.active = False self.coupon.save() form = CouponForm(data=form_data) self.assertFalse(form.is_valid())