diff --git a/code/ddice_online_shop/urls.py b/code/ddice_online_shop/urls.py index 822ab2c..476eb45 100644 --- a/code/ddice_online_shop/urls.py +++ b/code/ddice_online_shop/urls.py @@ -2,17 +2,17 @@ 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 @@ -20,25 +20,28 @@ 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/", views.product_computer, name = "view_product_computer"), - path("smartphone/product/", views.product_smartphone, name = "view_product_smartphone"), - path("headphone/product/", views.product_headphone, name = "view_product_headphone"), + path("computer/product/", views.product_computer, name = "view_product_computer"), + path("smartphone/product/", views.product_smartphone, name = "view_product_smartphone"), + path("headphone/product/", views.product_headphone, name = "view_product_headphone"), + path("cloth/product/", 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/", views.add_to_cart, name = "add_to_cart"), - path("product/removefromcart/", views.remove_from_cart, name = "remove_from_cart"), - path("product/removeonefromcart/", views.remove_single_item_from_cart, name = "remove_one_from_cart"), + path("product/addtocart/", views.add_to_cart, name = "add_to_cart"), + path("product/removefromcart/", views.remove_from_cart, name = "remove_from_cart"), + path("product/removeonefromcart/", 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) diff --git a/code/main/form/cloth_form.py b/code/main/form/cloth_form.py new file mode 100644 index 0000000..05df750 --- /dev/null +++ b/code/main/form/cloth_form.py @@ -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 \ No newline at end of file diff --git a/code/main/formview/cloth_formview.py b/code/main/formview/cloth_formview.py new file mode 100644 index 0000000..5874d94 --- /dev/null +++ b/code/main/formview/cloth_formview.py @@ -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 \ No newline at end of file diff --git a/code/main/get_first_request/get_all_cloth_data.py b/code/main/get_first_request/get_all_cloth_data.py new file mode 100644 index 0000000..811dfb6 --- /dev/null +++ b/code/main/get_first_request/get_all_cloth_data.py @@ -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 \ No newline at end of file diff --git a/code/main/views.py b/code/main/views.py index 07266a2..4d3f50b 100644 --- a/code/main/views.py +++ b/code/main/views.py @@ -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): @@ -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): @@ -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) diff --git a/code/templates/base.html b/code/templates/base.html index cac1e51..6fc0291 100644 --- a/code/templates/base.html +++ b/code/templates/base.html @@ -72,7 +72,7 @@