diff --git a/accounts/__pycache__/views.cpython-39.pyc b/accounts/__pycache__/views.cpython-39.pyc index ec0b446..cf9e92c 100644 Binary files a/accounts/__pycache__/views.cpython-39.pyc and b/accounts/__pycache__/views.cpython-39.pyc differ diff --git a/accounts/views.py b/accounts/views.py index 9ded5a8..93054a2 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -1,5 +1,5 @@ -import accounts -from email import message + +from cart.views import _cart_id from django.contrib import messages, auth from django.contrib.auth.decorators import login_required from django.utils import http @@ -8,7 +8,6 @@ from .form import RegistrationForm from .models import Account from django.http import HttpResponse - # verfication mail from django.contrib.sites.shortcuts import get_current_site @@ -18,7 +17,9 @@ from django.utils.encoding import force_bytes from django.contrib.auth.tokens import default_token_generator from django.core.mail import EmailMessage - +from cart.views import _cart_id +from cart.views import CartItem, Cart +import requests # Create your views here. def register(request): @@ -64,9 +65,51 @@ def login(request): user = auth.authenticate(email = email, password = password) if user is not None: + try: + cart = Cart.objects.get(cart_id=_cart_id(request)) + is_cart_item_exists = CartItem.objects.filter(cart=cart).exists() + if is_cart_item_exists: + cart_item = CartItem.objects.filter(cart=cart) + variation_products = [] + for item in cart_item: + variation = item.variation.all() + variation_products.append(list(variation)) + + cart_item = CartItem.objects.filter(user=user) + ex_var_list = [] + id = [] + for item in cart_item: + existing_variation = item.variation.all() + ex_var_list.append(list(existing_variation)) + id.append(item.id) + + for pr in variation_products: + if pr in ex_var_list: + index = ex_var_list.index(pr) + item_id = id[index] + item = CartItem.objects.get(id= item_id) + item.quantity += 1 + item.user = user + item.save() + else: + cart_item = CartItem.objects.filter(cart = cart) + for item in cart_item: + item.user = user + item.save() + except: + print("Entering inside except block") + pass auth.login(request, user) messages.success(request, "You are now logged in") - return redirect('dashboard') + url = request.META.get('HTTP_REFERER') + try: + query = requests.utils.urlparse(url).query + params= dict(x.split('=') for x in query.split('&')) + if 'next' in params: + nextPage = params['next'] + return redirect(nextPage) + except: + return redirect('dashboard') else: messages.error(request, ' Invalid login credentials') return redirect('login') diff --git a/cart/__pycache__/context_processors.cpython-39.pyc b/cart/__pycache__/context_processors.cpython-39.pyc index 7c4aba0..2ddedd0 100644 Binary files a/cart/__pycache__/context_processors.cpython-39.pyc and b/cart/__pycache__/context_processors.cpython-39.pyc differ diff --git a/cart/__pycache__/models.cpython-39.pyc b/cart/__pycache__/models.cpython-39.pyc index dec6d35..d8be48e 100644 Binary files a/cart/__pycache__/models.cpython-39.pyc and b/cart/__pycache__/models.cpython-39.pyc differ diff --git a/cart/__pycache__/urls.cpython-39.pyc b/cart/__pycache__/urls.cpython-39.pyc index 55c32f2..dda0f95 100644 Binary files a/cart/__pycache__/urls.cpython-39.pyc and b/cart/__pycache__/urls.cpython-39.pyc differ diff --git a/cart/__pycache__/views.cpython-39.pyc b/cart/__pycache__/views.cpython-39.pyc index 100b0cb..04fc8c1 100644 Binary files a/cart/__pycache__/views.cpython-39.pyc and b/cart/__pycache__/views.cpython-39.pyc differ diff --git a/cart/context_processors.py b/cart/context_processors.py index 6c25b66..14c0b57 100644 --- a/cart/context_processors.py +++ b/cart/context_processors.py @@ -8,7 +8,10 @@ def counter(request): else: try: cart = Cart.objects.filter(cart_id= _cart_id(request)) - cart_items = CartItem.objects.all().filter(cart = cart[:1] ) + if request.user.is_authenticated: + cart_items = CartItem.objects.all().filter(user=request.user) + else: + cart_items = CartItem.objects.all().filter(cart = cart[:1] ) for cart_item in cart_items: cart_count += cart_item.quantity except Cart.DoesNotExist: diff --git a/cart/migrations/0004_cartitem_user.py b/cart/migrations/0004_cartitem_user.py new file mode 100644 index 0000000..a3d79e3 --- /dev/null +++ b/cart/migrations/0004_cartitem_user.py @@ -0,0 +1,21 @@ +# Generated by Django 3.2.7 on 2021-09-28 19:25 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('cart', '0003_cartitem_variation'), + ] + + operations = [ + migrations.AddField( + model_name='cartitem', + name='user', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/cart/migrations/__pycache__/0004_cartitem_user.cpython-39.pyc b/cart/migrations/__pycache__/0004_cartitem_user.cpython-39.pyc new file mode 100644 index 0000000..b01cb30 Binary files /dev/null and b/cart/migrations/__pycache__/0004_cartitem_user.cpython-39.pyc differ diff --git a/cart/models.py b/cart/models.py index d5fde2e..fedb562 100644 --- a/cart/models.py +++ b/cart/models.py @@ -1,6 +1,6 @@ from django.db import models from store.models import Product, Variation - +from accounts.models import Account # Create your models here. class Cart(models.Model): cart_id = models.CharField(max_length=250, blank=True) @@ -11,6 +11,7 @@ def __str__(self): class CartItem(models.Model): + user = models.ForeignKey(Account, on_delete=models.CASCADE, null=True) product = models.ForeignKey(Product, on_delete=models.CASCADE) variation = models.ManyToManyField(Variation, blank=True) cart = models.ForeignKey(Cart, on_delete=models.CASCADE) diff --git a/cart/urls.py b/cart/urls.py index 4def5b3..5403b28 100644 --- a/cart/urls.py +++ b/cart/urls.py @@ -7,4 +7,6 @@ path('add_cart//', views.add_cart, name='add_cart'), path('remove_cart///', views.remove_cart, name='remove_cart'), path('remove_cart_item//', views.remove_cart_item, name='remove_cart_item'), + + path('checkout/', views.checkout, name='checkout'), ] diff --git a/cart/views.py b/cart/views.py index 2f5b4a2..17332da 100644 --- a/cart/views.py +++ b/cart/views.py @@ -3,6 +3,7 @@ from .models import CartItem, Cart from django.core.exceptions import ObjectDoesNotExist from store.models import Product, Variation +from django.contrib.auth.decorators import login_required # Create your views here. @@ -14,77 +15,133 @@ def _cart_id(request): def add_cart(request, product_id): - product = Product.objects.get(id=product_id) - variation_products = [] - if request.method == 'POST': - for item in request.POST: - key = item - value = request.POST[key] - try: - variation = Variation.objects.get( - product=product, variation_category__iexact=key, variation_value__iexact=value) - variation_products.append(variation) - except: - pass - - product = Product.objects.get(id=product_id) - try: - cart = Cart.objects.get(cart_id=_cart_id(request)) - except Cart.DoesNotExist: - cart = Cart.objects.create( - cart_id=_cart_id(request) - ) - cart.save() - - is_cart_item_exists = CartItem.objects.filter( - product=product, cart=cart,).exists() - if is_cart_item_exists: - cart_item = CartItem.objects.filter(product=product, cart=cart) - # existing variation -> database - # correct varition -> product_database - # item_id -> database - ex_var_list = [] - id = [] - for item in cart_item: - existing_variation = item.variation.all() - ex_var_list.append(list(existing_variation)) - id.append(item.id) - - print(ex_var_list) - - if variation_products in ex_var_list: - # increase the cart item quantity - index = ex_var_list.index(variation_products) - item_id = id[index] - item = CartItem.objects.get(product=product, id=item_id) - item.quantity += 1 - item.save() - else: - item = CartItem.objects.create(product=product, quantity= 1, cart = cart) - if len(variation_products) > 0: - item.variation.clear() - item.variation.add(*variation_products) - # cart_item.quantity += 1 + current_user = request.user + + product = Product.objects.get(id=product_id) + # if user authenticate + if current_user.is_authenticated: + variation_products = [] + if request.method == 'POST': + for item in request.POST: + key = item + value = request.POST[key] + try: + variation = Variation.objects.get( + product=product, variation_category__iexact=key, variation_value__iexact=value) + variation_products.append(variation) + except: + pass + + is_cart_item_exists = CartItem.objects.filter( product=product, user=current_user,).exists() + if is_cart_item_exists: + cart_item = CartItem.objects.filter(product=product, user=current_user) + ex_var_list = [] + id = [] + for item in cart_item: + existing_variation = item.variation.all() + ex_var_list.append(list(existing_variation)) + id.append(item.id) + + if variation_products in ex_var_list: + # increase the cart item quantity + index = ex_var_list.index(variation_products) + item_id = id[index] + item = CartItem.objects.get(product=product, id=item_id) + item.quantity += 1 item.save() + else: + item = CartItem.objects.create(product=product, quantity= 1, user= current_user) + if len(variation_products) > 0: + item.variation.clear() + item.variation.add(*variation_products) + # cart_item.quantity += 1 + item.save() + else: + cart_item = CartItem.objects.create( + product=product, + quantity=1, + user= current_user, + ) + if len(variation_products) > 0: + cart_item.variation.clear() + cart_item.variation.add(*variation_products) + cart_item.save() + return redirect('cart') else: - cart_item = CartItem.objects.create( - product=product, - quantity=1, - cart=cart, + variation_products = [] + if request.method == 'POST': + for item in request.POST: + key = item + value = request.POST[key] + try: + variation = Variation.objects.get( + product=product, variation_category__iexact=key, variation_value__iexact=value) + variation_products.append(variation) + except: + pass + + product = Product.objects.get(id=product_id) + try: + cart = Cart.objects.get(cart_id=_cart_id(request)) + except Cart.DoesNotExist: + cart = Cart.objects.create( + cart_id=_cart_id(request) ) - if len(variation_products) > 0: - cart_item.variation.clear() - cart_item.variation.add(*variation_products) - cart_item.save() - return redirect('cart') + cart.save() + + is_cart_item_exists = CartItem.objects.filter( + product=product, cart=cart,).exists() + if is_cart_item_exists: + cart_item = CartItem.objects.filter(product=product, cart=cart) + # existing variation -> database + # correct varition -> product_database + # item_id -> database + ex_var_list = [] + id = [] + for item in cart_item: + existing_variation = item.variation.all() + ex_var_list.append(list(existing_variation)) + id.append(item.id) + + print(ex_var_list) + + if variation_products in ex_var_list: + # increase the cart item quantity + index = ex_var_list.index(variation_products) + item_id = id[index] + item = CartItem.objects.get(product=product, id=item_id) + item.quantity += 1 + item.save() + else: + item = CartItem.objects.create(product=product, quantity= 1, cart = cart) + if len(variation_products) > 0: + item.variation.clear() + item.variation.add(*variation_products) + # cart_item.quantity += 1 + item.save() + + else: + cart_item = CartItem.objects.create( + product=product, + quantity=1, + cart=cart, + ) + if len(variation_products) > 0: + cart_item.variation.clear() + cart_item.variation.add(*variation_products) + cart_item.save() + return redirect('cart') def remove_cart(request, product_id, cart_item_id): - cart = Cart.objects.get(cart_id=_cart_id(request)) product = get_object_or_404(Product, id=product_id) try: - cart_item = CartItem.objects.get(product=product, cart=cart, id = cart_item_id) + if request.user.is_authenticated: + cart_item = CartItem.objects.get(product=product, user= request.user, id = cart_item_id) + else: + cart = Cart.objects.get(cart_id=_cart_id(request)) + cart_item = CartItem.objects.get(product=product, cart=cart, id = cart_item_id) if cart_item.quantity > 1: cart_item.quantity -= 1 cart_item.save() @@ -96,9 +153,12 @@ def remove_cart(request, product_id, cart_item_id): def remove_cart_item(request, product_id, cart_item_id): - cart = Cart.objects.get(cart_id=_cart_id(request)) product = get_object_or_404(Product, id=product_id) - cart_item = CartItem.objects.get(product=product, cart=cart, id= cart_item_id) + if request.user.is_authenticated: + cart_item = CartItem.objects.get(product=product, user= request.user, id= cart_item_id) + else: + cart = Cart.objects.get(cart_id=_cart_id(request)) + cart_item = CartItem.objects.get(product=product, cart=cart, id= cart_item_id) cart_item.delete() return redirect('cart') @@ -107,8 +167,11 @@ def cart(request, total=0, quantity=0, cart_items=None): try: tax = 0 grand_total = 0 - cart = Cart.objects.get(cart_id=_cart_id(request)) - cart_items = CartItem.objects.filter(cart=cart, is_active=True) + if request.user.is_authenticated: + cart_items = CartItem.objects.filter(user = request.user, is_active = True) + else: + cart = Cart.objects.get(cart_id=_cart_id(request)) + cart_items = CartItem.objects.filter(cart=cart, is_active=True) for cart_item in cart_items: total += (cart_item.product.price * cart_item.quantity) quantity += cart_item.quantity @@ -124,3 +187,30 @@ def cart(request, total=0, quantity=0, cart_items=None): 'grand_total': grand_total, } return render(request, 'store/cart.html', context) + + +@login_required(login_url='login') +def checkout(request, total=0, quantity=0, cart_items=None): + try: + tax = 0 + grand_total = 0 + if request.user.is_authenticated: + cart_items = CartItem.objects.filter(user = request.user, is_active = True) + else: + cart = Cart.objects.get(cart_id=_cart_id(request)) + cart_items = CartItem.objects.filter(cart=cart, is_active=True) + for cart_item in cart_items: + total += (cart_item.product.price * cart_item.quantity) + quantity += cart_item.quantity + tax = (2*total)/100 + grand_total = total+tax + except ObjectDoesNotExist: + pass + context = { + 'total': total, + 'quantity': quantity, + 'cart_items': cart_items, + 'tax': tax, + 'grand_total': grand_total, + } + return render(request, 'store/checkout.html' , context) \ No newline at end of file diff --git a/db.sqlite3 b/db.sqlite3 index ab6167c..e78b233 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/media/photos/products/photo2.jpg b/media/photos/products/photo2.jpg new file mode 100644 index 0000000..abedddc Binary files /dev/null and b/media/photos/products/photo2.jpg differ diff --git a/media/photos/products/product2.jpg b/media/photos/products/product2.jpg new file mode 100644 index 0000000..c9af385 Binary files /dev/null and b/media/photos/products/product2.jpg differ diff --git a/media/photos/products/product5.jpg b/media/photos/products/product5.jpg new file mode 100644 index 0000000..47cdd2b Binary files /dev/null and b/media/photos/products/product5.jpg differ diff --git a/store/migrations/0005_alter_variation_variation_category.py b/store/migrations/0005_alter_variation_variation_category.py new file mode 100644 index 0000000..754e466 --- /dev/null +++ b/store/migrations/0005_alter_variation_variation_category.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.7 on 2021-09-28 19:25 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('store', '0004_alter_variation_variation_category'), + ] + + operations = [ + migrations.AlterField( + model_name='variation', + name='variation_category', + field=models.CharField(choices=[('color', 'color'), ('size', 'size')], max_length=100), + ), + ] diff --git a/store/migrations/__pycache__/0005_alter_variation_variation_category.cpython-39.pyc b/store/migrations/__pycache__/0005_alter_variation_variation_category.cpython-39.pyc new file mode 100644 index 0000000..bc0221f Binary files /dev/null and b/store/migrations/__pycache__/0005_alter_variation_variation_category.cpython-39.pyc differ diff --git a/templates/base.html b/templates/base.html index 6c394fa..e6260b5 100644 --- a/templates/base.html +++ b/templates/base.html @@ -32,7 +32,7 @@ /// some script // jquery ready start -$(document).ready(function() { +₹(document).ready(function() { // jQuery code }); diff --git a/templates/home.html b/templates/home.html index 6c7a8a0..d9ed6d7 100644 --- a/templates/home.html +++ b/templates/home.html @@ -17,7 +17,7 @@
- See all

Popular products

diff --git a/templates/store/cart.html b/templates/store/cart.html index c2df8a6..b81936b 100644 --- a/templates/store/cart.html +++ b/templates/store/cart.html @@ -73,8 +73,8 @@

Your Shopping Cart is Empty

- ${{cart_item.sub_total}} - ${{cart_item.product.price}} each + ₹{{cart_item.sub_total}} + ₹{{cart_item.product.price}} each
@@ -96,23 +96,23 @@

Your Shopping Cart is Empty

Total price:
-
${{total}}
+
₹{{total}}
Tax:
-
${{tax}}
+
₹{{tax}}
Total:
- ${{grand_total}} + ₹{{grand_total}}

- + Checkout Continue Shopping diff --git a/templates/store/checkout.html b/templates/store/checkout.html new file mode 100644 index 0000000..af39dc2 --- /dev/null +++ b/templates/store/checkout.html @@ -0,0 +1,135 @@ +{% extends 'base.html' %} {% load static %} {% block content %} +
+
+ +
+ + + + +
+ +
+ +
+{% endblock content %} \ No newline at end of file diff --git a/templates/store/product_detail.html b/templates/store/product_detail.html index d6f2bc5..c131c35 100644 --- a/templates/store/product_detail.html +++ b/templates/store/product_detail.html @@ -20,7 +20,7 @@

{{single_product.product_name}}

- ${{single_product.price}} + ₹{{single_product.price}}

{{single_product.description}}

diff --git a/templates/store/store.html b/templates/store/store.html index eb4bb4c..46bcce6 100644 --- a/templates/store/store.html +++ b/templates/store/store.html @@ -103,27 +103,27 @@
Price range
- +
@@ -166,7 +166,7 @@
Price range
>{{product.product_name}}
- ${{product.price}} + ₹{{product.price}}