Skip to content

Commit

Permalink
add new features
Browse files Browse the repository at this point in the history
  • Loading branch information
imhimansu28 committed Oct 14, 2021
1 parent 0b9e62d commit 3f04ae7
Show file tree
Hide file tree
Showing 24 changed files with 411 additions and 98 deletions.
Binary file modified accounts/__pycache__/views.cpython-39.pyc
Binary file not shown.
53 changes: 48 additions & 5 deletions accounts/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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')
Expand Down
Binary file modified cart/__pycache__/context_processors.cpython-39.pyc
Binary file not shown.
Binary file modified cart/__pycache__/models.cpython-39.pyc
Binary file not shown.
Binary file modified cart/__pycache__/urls.cpython-39.pyc
Binary file not shown.
Binary file modified cart/__pycache__/views.cpython-39.pyc
Binary file not shown.
5 changes: 4 additions & 1 deletion cart/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 21 additions & 0 deletions cart/migrations/0004_cartitem_user.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
Binary file not shown.
3 changes: 2 additions & 1 deletion cart/models.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions cart/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
path('add_cart/<int:product_id>/', views.add_cart, name='add_cart'),
path('remove_cart/<int:product_id>/<int:cart_item_id>/', views.remove_cart, name='remove_cart'),
path('remove_cart_item/<int:product_id>/<int:cart_item_id>', views.remove_cart_item, name='remove_cart_item'),

path('checkout/', views.checkout, name='checkout'),
]
Loading

0 comments on commit 3f04ae7

Please sign in to comment.