Skip to content

Commit

Permalink
Update apps to be pep8 compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
Awoyalejohn committed Aug 30, 2022
1 parent 144f39d commit f504a73
Show file tree
Hide file tree
Showing 39 changed files with 894 additions and 580 deletions.
36 changes: 19 additions & 17 deletions cart/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,44 @@
from django.shortcuts import get_object_or_404
from products.models import Product


def cart_contents(request):
cart_items = []
subtotal = 0
product_count = 0
cart = request.session.get('cart',{})
cart = request.session.get("cart", {})

for slug, quantity in cart.items():
product = get_object_or_404(Product, slug=slug)
subtotal += product.price
product_count += quantity
cart_items.append({
'slug': slug,
'quantity': quantity,
'product': product,
})
cart_items.append(
{
"slug": slug,
"quantity": quantity,
"product": product,
}
)

if subtotal > settings.DISCOUNT_THRESHOLD:
discount = subtotal * Decimal(settings.DISCOUNT_PERCENTAGE / 100)
discount_delta = 0

else:
discount = 0
discount_delta = settings.DISCOUNT_THRESHOLD - subtotal

total = subtotal - discount

context = {
'cart_items': cart_items,
'subtotal': subtotal,
'product_count': product_count,
'discount': discount,
'discount_delta': discount_delta,
'discount_threshold': settings.DISCOUNT_THRESHOLD,
'discount_percentage': settings.DISCOUNT_PERCENTAGE,
'total': total,

"cart_items": cart_items,
"subtotal": subtotal,
"product_count": product_count,
"discount": discount,
"discount_delta": discount_delta,
"discount_threshold": settings.DISCOUNT_THRESHOLD,
"discount_percentage": settings.DISCOUNT_PERCENTAGE,
"total": total,
}

return context
return context
12 changes: 8 additions & 4 deletions cart/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
from cart.views import DisplayCartView, AddToCart, RemoveFromCart

urlpatterns = [
path('', DisplayCartView.as_view(), name='display_cart'),
path('add/<slug:slug>/', AddToCart.as_view(), name='add_to_cart'),
path('remove/<slug:slug>/', RemoveFromCart.as_view(), name='remove_from_cart'),
]
path("", DisplayCartView.as_view(), name="display_cart"),
path("add/<slug:slug>/", AddToCart.as_view(), name="add_to_cart"),
path(
"remove/<slug:slug>/",
RemoveFromCart.as_view(),
name="remove_from_cart"
),
]
57 changes: 32 additions & 25 deletions cart/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,62 @@
from django.views.generic import View
from django.views.generic import TemplateView

# Create your views here.

class DisplayCartView(TemplateView):
""" A view that displays the contents of the cart """
"""A view that displays the contents of the cart"""

template_name = "cart/cart.html"


class AddToCart(View):
""" A view to add items to the cart page """
"""A view to add items to the cart page"""

def post(self, request, slug):
product = get_object_or_404(Product, slug=slug)
quantity = 1
#gets the redirect url
redirect_url = request.POST.get('redirect_url')
#checks if the cart is in the session and then creates an empty dict if it isn't
cart = request.session.get('cart', {})

#checks if the slug is in the cart dictionary
# gets the redirect url
redirect_url = request.POST.get("redirect_url")
# checks if the cart is in the session
# and then creates an empty dict if it isn't
cart = request.session.get("cart", {})

# checks if the slug is in the cart dictionary
if slug in list(cart.keys()):
messages.error(request, f"You already added {product.name} to your cart!")
messages.error(
request,
f"You already added {product.name} to your cart!"
)
return redirect(redirect_url)

else:
cart[slug] = quantity
messages.success(request, f'Added {product.name} to your cart')
messages.success(
request,
f"Added {product.name} to your cart"
)

# Adds the amount of that item to its value in the dictionary
request.session["cart"] = cart

#Adds the amount of that item to its value in the dictionary
request.session['cart'] = cart

return redirect(redirect_url)


class RemoveFromCart(View):
""" A view to remove an item from the cart page """
"""A view to remove an item from the cart page"""

def post(self, request, slug):
try:
product = get_object_or_404(Product, slug=slug)
#checks if the cart is in the session and then creates an empty dict if it isn't
cart = request.session.get('cart', {})
# checks if the cart is in the session
# and then creates an empty dict if it isn't
cart = request.session.get("cart", {})

#deletes the item in the list
# deletes the item in the list
cart.pop(slug)
messages.success(request, f'Removed {product.name} from your cart')
messages.success(request, f"Removed {product.name} from your cart")

request.session['cart'] = cart
request.session["cart"] = cart
return HttpResponse(status=200)
except Exception as e:
messages.error(request, f'Error removing item: {e}')
messages.error(request, f"Error removing item: {e}")
return HttpResponse(status=500)


60 changes: 42 additions & 18 deletions checkout/admin.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,55 @@
from django.contrib import admin
from .models import Order, OrderLineItem

# Register your models here.class OrderAdmin(admin.ModelAdmin):

class OrderLineItemAdminInline(admin.TabularInline):
model = OrderLineItem
readonly_fields = ('lineitem_subtotal',)
readonly_fields = ("lineitem_subtotal",)


class OrderAdmin(admin.ModelAdmin):
inlines = (OrderLineItemAdminInline,)

readonly_fields = ('order_number', 'date',
'discount', 'order_subtotal',
'total','original_cart',
'stripe_pid')

fields = ('order_number', 'user_profile', 'date', 'full_name',
'email', 'phone_number', 'country',
'postcode', 'town_or_city', 'street_address1',
'street_address2', 'county', 'discount',
'order_subtotal', 'total', 'original_cart',
'stripe_pid')

list_display = ('order_number', 'date', 'full_name',
'order_subtotal', 'discount',
'total',)
readonly_fields = (
"order_number",
"date",
"discount",
"order_subtotal",
"total",
"original_cart",
"stripe_pid",
)

fields = (
"order_number",
"user_profile",
"date",
"full_name",
"email",
"phone_number",
"country",
"postcode",
"town_or_city",
"street_address1",
"street_address2",
"county",
"discount",
"order_subtotal",
"total",
"original_cart",
"stripe_pid",
)

list_display = (
"order_number",
"date",
"full_name",
"order_subtotal",
"discount",
"total",
)

ordering = ("-date",)

ordering = ('-date',)

admin.site.register(Order, OrderAdmin)
42 changes: 24 additions & 18 deletions checkout/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
class OrderForm(forms.ModelForm):
class Meta:
model = Order
fields = ('full_name', 'email', 'phone_number',
'street_address1', 'street_address2',
'town_or_city', 'postcode', 'country',
'county',)
fields = (
"full_name",
"email",
"phone_number",
"street_address1",
"street_address2",
"town_or_city",
"postcode",
"country",
"county",
)

def __init__(self, *args, **kwargs):
"""
Expand All @@ -17,23 +24,22 @@ def __init__(self, *args, **kwargs):
"""
super().__init__(*args, **kwargs)
placeholders = {
'full_name': 'Full Name',
'email': 'Email Address',
'phone_number': 'Phone Number',
'postcode': 'Postal Code',
'town_or_city': 'Town or City',
'street_address1': 'Street Address 1',
'street_address2': 'Street Address 2',
'county': 'County, State or Locality',
"full_name": "Full Name",
"email": "Email Address",
"phone_number": "Phone Number",
"postcode": "Postal Code",
"town_or_city": "Town or City",
"street_address1": "Street Address 1",
"street_address2": "Street Address 2",
"county": "County, State or Locality",
}

self.fields['full_name'].widget.attrs['autofocus'] = True
self.fields["full_name"].widget.attrs["autofocus"] = True
for field in self.fields:
if field != 'country':
if field != "country":
if self.fields[field].required:
placeholder = f'{placeholders[field]} *'
placeholder = f"{placeholders[field]} *"
else:
placeholder = placeholders[field]
self.fields[field].widget.attrs['placeholder'] = placeholder
# self.fields[field].widget.attrs['class'] = 'stripe-style-input'
self.fields[field].label = False
self.fields[field].widget.attrs["placeholder"] = placeholder
self.fields[field].label = False
Loading

0 comments on commit f504a73

Please sign in to comment.