def checkout(): form = OrderForm() cart = get_cart() categories = get_categories() return render_template('checkout.html', form=form, categories=categories, cart=cart)
def home(request): now = timezone.now() user_info = Order.objects.filter(user=request.user.id).aggregate( spend_money_year=Sum( 'price', filter=Q(ticket__start_date__gte=now - timezone.timedelta(days=365)) ), bought_last_year=Count( 'id', filter=Q(ticket__start_date__gte=now - timezone.timedelta(days=365)) ), spend_money_month=Sum( 'price', filter=Q(ticket__start_date__gte=now - timezone.timedelta(weeks=4)) ), bought_last_month=Count( 'id', filter=Q(ticket__start_date__gte=now - timezone.timedelta(weeks=4)) ), spend_money_week=Sum( 'price', filter=Q(ticket__start_date__gte=now - timezone.timedelta(days=7)) ), bought_last_week=Count( 'id', filter=Q(ticket__start_date__gte=now - timezone.timedelta(days=7)) ) ) form = OrderForm(data=request.POST) if form.is_valid(): order = form.save(commit=False) order.user_id = request.user.id order.save() return redirect('ecommerce:home') return render(request, 'ecommerce/home.html', context={"form": form, 'user_info': user_info})
def make_order(request, pk): order_form = OrderForm() if request.method == 'POST': order_form = OrderForm(request.POST) if order_form.is_valid(): order = order_form.save(commit=False) order.user_id = pk context = {'order_form': order_form} return render(request=request, template_name='order/make_order.html', context=context)
def test_from_validation_on_email_field(self): """Form should validate user's email if it is filled.""" form = OrderForm(data=invalid_form_email) self.assertFalse(form.is_valid())
def test_valid_form(self): """Form is valid, if there all required fields are filled.""" form = OrderForm(data=required_fields) self.assertTrue(form.is_valid())
def test_filled_form_without_required_field(self): """Form is still not valid, if there are some required fields left unfilled.""" form = OrderForm(data=no_phone) self.assertFalse(form.is_valid())
def test_empty_form(self): """Empty form shouldn't be valid.""" form = OrderForm() self.assertFalse(form.is_valid())