Skip to content

Commit 39c7d00

Browse files
Created Coupons, Cart and Orders apps.
1 parent 53b1180 commit 39c7d00

39 files changed

+932
-336
lines changed

.env

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ DB_USER=eCommerce
33
DB_PASSWORD=0592876798
44
DB_HOST=localhost
55
REDIS_HOST=localhost
6-
REDIS_PORT=6379
6+
REDIS_PORT=6379
7+
EMAIL_HOST_USER=[email protected]
8+
EMAIL_HOST_PASSWORD=ctdk hqrt yuog vpdq
9+
DEFAULT_FROM_EMAIL=Hypex Store <[email protected]>

Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Pull official base Python Docker image
2+
FROM python:3.12.3
3+
# Set environment variables
4+
ENV PYTHONDONTWRITEBYTECODE=1
5+
ENV PYTHONUNBUFFERED=1
6+
# Set work directory
7+
WORKDIR /code
8+
# Install dependencies
9+
RUN pip install --upgrade pip
10+
COPY requirements.txt .
11+
RUN pip install -r requirements.txt
12+
# Copy the Django project
13+
COPY . .

api/migrations/0001_initial.py

Lines changed: 0 additions & 79 deletions
This file was deleted.

api/migrations/0002_remove_product_id_product_product_id.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

api/migrations/0003_rename_picture_url_profile_picture.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

api/migrations/0004_trigram_ext.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

api/migrations/0005_order_orderitem_alter_category_options_and_more.py

Lines changed: 0 additions & 110 deletions
This file was deleted.

api/migrations/0006_order_status.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

api/urls.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from django.urls import path, include
2+
3+
app_name = 'api-v1'
4+
5+
urlpatterns = [
6+
path('', include('shop.urls')),
7+
path('', include('orders.urls')),
8+
path('', include('cart.urls')),
9+
path('', include('coupons.urls')),
10+
]

cart/cart.py

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from django.conf import settings
44

5+
from coupons.models import Coupon
56
from shop.models import Product
67

78

@@ -23,6 +24,8 @@ def __init__(self, request):
2324
# save an empty cart in the session
2425
cart = self.session[settings.CART_SESSION_ID] = {}
2526
self.cart = cart
27+
# store current applied coupon
28+
self.coupon_id = self.session.get('coupon_id')
2629

2730
def add(self, product, quantity=1, override_quantity=False):
2831
"""
@@ -34,7 +37,7 @@ def add(self, product, quantity=1, override_quantity=False):
3437
override_quantity: If True, replace the current quantity with the given quantity.
3538
If False, increment the current quantity by the given amount.
3639
"""
37-
product_id = str(product.id)
40+
product_id = str(product.product_id)
3841
if product_id not in self.cart:
3942
self.cart[product_id] = {
4043
'quantity': 0,
@@ -53,7 +56,7 @@ def remove(self, product):
5356
Args:
5457
product: The product object to remove from the cart.
5558
"""
56-
product_id = str(product.id)
59+
product_id = str(product.product_id)
5760
if product_id in self.cart:
5861
del self.cart[product_id]
5962
self.save()
@@ -66,10 +69,10 @@ def __iter__(self):
6669
"""
6770
product_ids = self.cart.keys()
6871
# get the product objects and add them to the cart
69-
products = Product.objects.filter(id__in=product_ids)
72+
products = Product.objects.filter(product_id__in=product_ids)
7073
cart = self.cart.copy()
7174
for product in products:
72-
cart[str(product.id)]['product'] = product
75+
cart[str(product.product_id)]['product'] = product
7376
for item in cart.values():
7477
item['price'] = Decimal(item['price'])
7578
item['total_price'] = item['price'] * item['quantity']
@@ -108,3 +111,42 @@ def clear(self):
108111
"""
109112
del self.session[settings.CART_SESSION_ID]
110113
self.save()
114+
115+
@property
116+
def coupon(self):
117+
"""
118+
Retrieve the currently applied coupon.
119+
120+
Returns:
121+
Coupon: The applied coupon object if it exists and is valid.
122+
None: If no coupon is applied or the coupon does not exist.
123+
"""
124+
if self.coupon_id:
125+
try:
126+
return Coupon.objects.get(id=self.coupon_id)
127+
128+
except Coupon.DoesNotExist:
129+
pass
130+
return None
131+
132+
def get_discount(self):
133+
"""
134+
Calculate the discount amount based on the applied coupon.
135+
136+
Returns:
137+
Decimal: The discount amount to be subtracted from the total price.
138+
"""
139+
if self.coupon:
140+
return (
141+
self.coupon.discount / Decimal(100)
142+
) * self.get_total_price()
143+
return Decimal(0)
144+
145+
def get_total_price_after_discount(self):
146+
"""
147+
Calculate the total price of the cart after applying the discount.
148+
149+
Returns:
150+
Decimal: The total price after the discount is applied.
151+
"""
152+
return self.get_total_price() - self.get_discount()

0 commit comments

Comments
 (0)