示例#1
0
def register_user(request):
    user = request.user
    if not user.is_anonymous:
        return redirect('login:home')
    warning = ""
    Userform = RegisterForm(request.POST or None)
    if request.method == "POST":
        if Userform.is_valid(
        ):  #i am using Javascript to check for confirm password and password matching
            try:
                User.objects.get(email__iexact=form.cleaned_data['email'])
                warning = "User with following email exists"
            except Exception as e:
                ob = Userform.save()
                cart = Cart()
                cart.user = ob
                cart.save()
                otp = Otp()
                otp.user = ob
                otp.save()
                log_in(request, ob)
                return redirect('login:verify_user')
    return render(request, "login/register.html", {
        'form1': Userform,
        'warning': warning
    })
示例#2
0
def add_cart_to_database(user, size, tshirt):
    size = SizeVariant.objects.get(size=size, tshirt=tshirt)
    existing = Cart.objects.filter(user=user, sizeVariant=size)

    if len(existing) > 0:
        obj = existing[0]
        obj.quantity = obj.quantity + 1
        obj.save()

    else:
        c = Cart()
        c.user = user
        c.sizeVariant = size
        c.quantity = 1
        c.save()
示例#3
0
    def post(self , request):
        add = request.session.get('plusItem')
        form = CustomerAuthForm(data=request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            user = authenticate(username=username, password=password)
            if user:
                loginuser(request, user)

                session_cart = request.session.get('cart')
                if session_cart is None:
                    session_cart = []
                else:
                    for c in session_cart:
                        size = c.get('size')
                        tshirt_id = c.get('tshirt')
            
                        quantity = c.get('quantity')+1
                        cart_obj = Cart()
                        cart_obj.sizeVariant = SizeVariant.objects.get(
                            size=size, tshirt=tshirt_id)
                        cart_obj.quantity = quantity
                        cart_obj.user = user
                        cart_obj.save()

                # { size , tshirt , quantiti }
                cart = Cart.objects.filter(user=user)
                session_cart = []
                for c in cart:
                    obj = {
                        'size': c.sizeVariant.size,
                        'tshirt': c.sizeVariant.tshirt.id,
                        'quantity': c.quantity
                    }
                    session_cart.append(obj)

                request.session['cart'] = session_cart
                next_page = request.session.get('next_page')
                if next_page is None:
                    next_page = 'homepage'
                return redirect(next_page)
        else:
            return render(request,
                          template_name='login.html',
                          context={'form': form})