Skip to content

Commit

Permalink
add cloth to product shelves
Browse files Browse the repository at this point in the history
  • Loading branch information
kritserv committed Oct 2, 2023
1 parent 27c45ed commit a26e375
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 21 deletions.
43 changes: 23 additions & 20 deletions code/ddice_online_shop/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,46 @@
URL configuration for ddice_online_shop project.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.2/topics/http/urls/
https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path
from main import views
from main.formview.computer_formview import ComputerFormView
from main.formview.smartphone_formview import SmartphoneFormView
from main.formview.headphone_formview import HeadphoneFormView
from main.formview.cloth_formview import ClothFormView
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
path('admin/', admin.site.urls),
path("", views.home, name = "home"),
path('admin/', admin.site.urls),
path("", views.home, name = "home"),

path("computer/product/<int:id>", views.product_computer, name = "view_product_computer"),
path("smartphone/product/<int:id>", views.product_smartphone, name = "view_product_smartphone"),
path("headphone/product/<int:id>", views.product_headphone, name = "view_product_headphone"),
path("computer/product/<int:id>", views.product_computer, name = "view_product_computer"),
path("smartphone/product/<int:id>", views.product_smartphone, name = "view_product_smartphone"),
path("headphone/product/<int:id>", views.product_headphone, name = "view_product_headphone"),
path("cloth/product/<int:id>", views.product_cloth, name = "view_product_cloth"),

path('computer/', ComputerFormView.as_view(), name='view_computer'),
path('smartphone/', SmartphoneFormView.as_view(), name='view_smartphone'),
path('headphone/', HeadphoneFormView.as_view(), name='view_headphone'),
path('computer/', ComputerFormView.as_view(), name='view_computer'),
path('smartphone/', SmartphoneFormView.as_view(), name='view_smartphone'),
path('headphone/', HeadphoneFormView.as_view(), name='view_headphone'),
path('cloth/', ClothFormView.as_view(), name='view_cloth'),

path("product/addtocart/<str:title>", views.add_to_cart, name = "add_to_cart"),
path("product/removefromcart/<str:title>", views.remove_from_cart, name = "remove_from_cart"),
path("product/removeonefromcart/<str:title>", views.remove_single_item_from_cart, name = "remove_one_from_cart"),
path("product/addtocart/<str:title>", views.add_to_cart, name = "add_to_cart"),
path("product/removefromcart/<str:title>", views.remove_from_cart, name = "remove_from_cart"),
path("product/removeonefromcart/<str:title>", views.remove_single_item_from_cart, name = "remove_one_from_cart"),

path('accounts/', include('allauth.urls')),
path('accounts/', include('allauth.urls')),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
26 changes: 26 additions & 0 deletions code/main/form/cloth_form.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django import forms
from products.models import Cloth
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit

class ClothForm(forms.Form):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.form_method = 'POST'
self.helper.add_input(Submit('submit', 'Submit'))

QUERY_FIELDS = ['is_in_stock', 'is_on_sale', 'color', 'gender', 'cloth_type', 'size']

FORM = []

for q in QUERY_FIELDS:

CHOICES = []
for x in Cloth.objects.values(q).distinct().order_by(q):
CHOICES.append((x[q], x[q]))

FORM.append(forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=CHOICES, required=False))

product_available, on_sale, color, gender, cloth_type, size = FORM
130 changes: 130 additions & 0 deletions code/main/formview/cloth_formview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
from django.shortcuts import render
from django.views.generic.edit import FormView
from products.models import Cloth
from main.form.cloth_form import ClothForm
from math import floor

class ClothFormView(FormView):

form_class = ClothForm
template_name = 'store/product.html'
success_url = '/cloth'

def form_valid(self, form):

filtered_cloth = Cloth.objects.all()

self.request.session['cloth_data'] = []
all_is_in_stock_query, all_is_on_sale_query, all_color_query, all_gender_query, all_cloth_type_query, all_size_query = [], [], [], [], [], []
at_least_1_query = False

for query in form.cleaned_data['product_available']:
if query:
at_least_1_query = True
for x in filtered_cloth.filter(is_in_stock=query).values_list('id'):
all_is_in_stock_query.append(x[0])
if form.cleaned_data['product_available']:
filtered_cloth = filtered_cloth.filter(id__in=all_is_in_stock_query)
query = None

for query in form.cleaned_data['on_sale']:
if query:
at_least_1_query = True
for x in filtered_cloth.filter(is_on_sale=query).values_list('id'):
all_is_on_sale_query.append(x[0])
if form.cleaned_data['on_sale']:
filtered_cloth = filtered_cloth.filter(id__in=all_is_on_sale_query)
query = None

for query in form.cleaned_data['color']:
if query:
at_least_1_query = True
for x in filtered_cloth.filter(color=query).values_list('id'):
all_color_query.append(x[0])
if form.cleaned_data['color']:
filtered_cloth = filtered_cloth.filter(id__in=all_color_query)
query = None

for query in form.cleaned_data['gender']:
if query:
at_least_1_query = True
for x in filtered_cloth.filter(is_on_sale=query).values_list('id'):
all_gender_query.append(x[0])
if form.cleaned_data['gender']:
filtered_cloth = filtered_cloth.filter(id__in=all_gender_query)
query = None

for query in form.cleaned_data['cloth_type']:
if query:
at_least_1_query = True
for x in filtered_cloth.filter(cloth_type=query).values_list('id'):
all_cloth_type_query.append(x[0])
if form.cleaned_data['cloth_type']:
filtered_cloth = filtered_cloth.filter(id__in=all_cloth_type_query)
query = None

for query in form.cleaned_data['size']:
if query:
at_least_1_query = True
for x in filtered_cloth.filter(size=query).values_list('id'):
all_size_query.append(x[0])
if form.cleaned_data['size']:
filtered_cloth = filtered_cloth.filter(id__in=all_size_query)
query = None

if at_least_1_query == False:
filtered_cloth = Cloth.objects.all()

cloth_title = [x[0] for x in filtered_cloth.values_list('title')]
cloth_link = ['/cloth/product/'+str(x[0]) for x in filtered_cloth.values_list('id')]
cloth_onsale = [x[0] for x in filtered_cloth.values_list('is_on_sale')]
cloth_og_price = [x[0] for x in filtered_cloth.values_list('og_price')]
cloth_price = [x[0] for x in filtered_cloth.values_list('price')]
cloth_is_in_stock = [x[0] for x in filtered_cloth.values_list('is_in_stock')]
cloth_in_stocks = [x[0] for x in filtered_cloth.values_list('in_stocks')]
cloth_is_recommend = [x[0] for x in filtered_cloth.values_list('is_recommend')]
cloth_img = filtered_cloth.values_list('image')
cloth_img = [x[0].replace(' ','%20') for x in cloth_img]
cloth_price = [str(x[0]) for x in filtered_cloth.values_list('price')]
cloth_star = [str(x[0]) for x in filtered_cloth.values_list('stars')]

cloth_data = []
for i in range(len(cloth_title)):

if cloth_onsale[i] == False:
cloth_og_price[i] = ''
fullstar = "★" * floor(float(cloth_star[i]))

if cloth_onsale[i] == True:
cloth_onsale[i] = 'Sale'
else:
cloth_onsale[i] = ''

if cloth_is_in_stock[i] == False:
cloth_is_in_stock[i] = 'Out of Stock'
else:
cloth_is_in_stock[i] = 'In Stocks'

if cloth_is_recommend[i] == True:
cloth_is_recommend[i] = 'Recommend'
else:
cloth_is_recommend[i] = ''

if len(cloth_title[i] + 'Recommend') > 37:
cloth_title[i] = cloth_title[i][0:30] + '...'


cloth_data.append({'title':cloth_title[i], 'onsale':cloth_onsale[i],
'ogprice':cloth_og_price[i], 'price':cloth_price[i],'im':cloth_img[i],
'instock':cloth_is_in_stock[i], 'available': cloth_in_stocks[i], 'link': cloth_link[i],
'recommend':cloth_is_recommend[i], 'star':fullstar, 'star_num':' ('+cloth_star[i]+')'})

self.request.session['cloth_data'] = cloth_data

return super().form_valid(form)

def get_context_data(self, **kwargs):

context = super().get_context_data(**kwargs)
context['product_data'] = self.request.session.get('cloth_data', [])
return context
52 changes: 52 additions & 0 deletions code/main/get_first_request/get_all_cloth_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from products.models import Cloth
from math import floor

def GetAllClothData():

filtered_cloth = Cloth.objects.all()

cloth_title = [x[0] for x in filtered_cloth.values_list('title')]
cloth_link = ['/cloth/product/'+str(x[0]) for x in filtered_cloth.values_list('id')]
cloth_onsale = [x[0] for x in filtered_cloth.values_list('is_on_sale')]
cloth_og_price = [x[0] for x in filtered_cloth.values_list('og_price')]
cloth_price = [x[0] for x in filtered_cloth.values_list('price')]
cloth_is_in_stock = [x[0] for x in filtered_cloth.values_list('is_in_stock')]
cloth_in_stocks = [x[0] for x in filtered_cloth.values_list('in_stocks')]
cloth_is_recommend = [x[0] for x in filtered_cloth.values_list('is_recommend')]
cloth_img = filtered_cloth.values_list('image')
cloth_img = [x[0].replace(' ','%20') for x in cloth_img]
cloth_price = [str(x[0]) for x in filtered_cloth.values_list('price')]
cloth_star = [str(x[0]) for x in filtered_cloth.values_list('stars')]

cloth_data = []
for i in range(len(cloth_title)):

if cloth_onsale[i] == False:
cloth_og_price[i] = ''
fullstar = "★" * floor(float(cloth_star[i]))

if cloth_onsale[i] == True:
cloth_onsale[i] = 'Sale'
else:
cloth_onsale[i] = ''

if cloth_is_in_stock[i] == False:
cloth_is_in_stock[i] = 'Out of Stock'
else:
cloth_is_in_stock[i] = 'In Stocks'

if cloth_is_recommend[i] == True:
cloth_is_recommend[i] = 'Recommend'
else:
cloth_is_recommend[i] = ''

if len(cloth_title[i] + 'Recommend') > 37:
cloth_title[i] = cloth_title[i][0:30] + '...'


cloth_data.append({'title':cloth_title[i], 'onsale':cloth_onsale[i],
'ogprice':cloth_og_price[i], 'price':cloth_price[i],'im':cloth_img[i],
'instock':cloth_is_in_stock[i], 'available': cloth_in_stocks[i], 'link': cloth_link[i],
'recommend':cloth_is_recommend[i], 'star':fullstar, 'star_num':' ('+cloth_star[i]+')'})

return cloth_data
9 changes: 9 additions & 0 deletions code/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .get_first_request.get_all_computer_data import GetAllComputerData
from .get_first_request.get_all_smartphone_data import GetAllSmartphoneData
from .get_first_request.get_all_headphone_data import GetAllHeadphoneData
from .get_first_request.get_all_cloth_data import GetAllClothData

# Create your views here.
def home(request):
Expand All @@ -22,6 +23,9 @@ def home(request):
if 'headphone_data' not in request.session:
request.session['headphone_data'] = GetAllHeadphoneData()

if 'cloth_data' not in request.session:
request.session['cloth_data'] = GetAllClothData()

return render(request, "homepage.html")

def product_computer(request, id):
Expand All @@ -39,6 +43,11 @@ def product_headphone(request, id):
fullstar = "★" * floor(product.stars)
return render(request, "store/product/headphone.html", {'data': product, 'fullstar': fullstar})

def product_cloth(request, id):
product = Cloth.objects.get(id=id)
fullstar = "★" * floor(product.stars)
return render(request, "store/product/cloth.html", {'data': product, 'fullstar': fullstar})

@login_required
def add_to_cart(request, title):
prod_item = get_object_or_404(ProductItem, title=title)
Expand Down
2 changes: 1 addition & 1 deletion code/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<li class="nav-item">
<div data-aos="flip-right">
<div class="d-flex align-items-center">
<a class="nav-link" href="#" tabindex="-1" aria-disabled="true">
<a class="nav-link" href="/cloth" tabindex="-1" aria-disabled="true">
<svg xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 640 512"><!--! Font Awesome Free 6.4.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path d="M211.8 0c7.8 0 14.3 5.7 16.7 13.2C240.8 51.9 277.1 80 320 80s79.2-28.1 91.5-66.8C413.9 5.7 420.4 0 428.2 0h12.6c22.5 0 44.2 7.9 61.5 22.3L628.5 127.4c6.6 5.5 10.7 13.5 11.4 22.1s-2.1 17.1-7.8 23.6l-56 64c-11.4 13.1-31.2 14.6-44.6 3.5L480 197.7V448c0 35.3-28.7 64-64 64H224c-35.3 0-64-28.7-64-64V197.7l-51.5 42.9c-13.3 11.1-33.1 9.6-44.6-3.5l-56-64c-5.7-6.5-8.5-15-7.8-23.6s4.8-16.6 11.4-22.1L137.7 22.3C155 7.9 176.7 0 199.2 0h12.6z"/></svg>
<p>Cloths</p>
</a>
Expand Down
Loading

0 comments on commit a26e375

Please sign in to comment.