diff --git a/ecommerce/db.sqlite3 b/ecommerce/db.sqlite3 new file mode 100644 index 00000000..e69de29b diff --git a/ecommerce/ecommerce/__init__.py b/ecommerce/ecommerce/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ecommerce/ecommerce/__pycache__/__init__.cpython-39.pyc b/ecommerce/ecommerce/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 00000000..8cb3f731 Binary files /dev/null and b/ecommerce/ecommerce/__pycache__/__init__.cpython-39.pyc differ diff --git a/ecommerce/ecommerce/__pycache__/settings.cpython-39.pyc b/ecommerce/ecommerce/__pycache__/settings.cpython-39.pyc new file mode 100644 index 00000000..651829fd Binary files /dev/null and b/ecommerce/ecommerce/__pycache__/settings.cpython-39.pyc differ diff --git a/ecommerce/ecommerce/__pycache__/urls.cpython-39.pyc b/ecommerce/ecommerce/__pycache__/urls.cpython-39.pyc new file mode 100644 index 00000000..fe21d930 Binary files /dev/null and b/ecommerce/ecommerce/__pycache__/urls.cpython-39.pyc differ diff --git a/ecommerce/ecommerce/__pycache__/wsgi.cpython-39.pyc b/ecommerce/ecommerce/__pycache__/wsgi.cpython-39.pyc new file mode 100644 index 00000000..692a2c1f Binary files /dev/null and b/ecommerce/ecommerce/__pycache__/wsgi.cpython-39.pyc differ diff --git a/ecommerce/ecommerce/asgi.py b/ecommerce/ecommerce/asgi.py new file mode 100644 index 00000000..d3a9622b --- /dev/null +++ b/ecommerce/ecommerce/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for ecommerce project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ecommerce.settings') + +application = get_asgi_application() diff --git a/ecommerce/ecommerce/settings.py b/ecommerce/ecommerce/settings.py new file mode 100644 index 00000000..116658de --- /dev/null +++ b/ecommerce/ecommerce/settings.py @@ -0,0 +1,125 @@ +""" +Django settings for ecommerce project. + +Generated by 'django-admin startproject' using Django 4.0.6. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.0/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure--^t*+$v!fbeehil0mdlg&1zu#8%r4347d+mx58g6(+v54*j)08' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'product.apps.ProductConfig', + 'rest_framework', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'ecommerce.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'ecommerce.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/4.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.0/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/ecommerce/ecommerce/urls.py b/ecommerce/ecommerce/urls.py new file mode 100644 index 00000000..36dc6f26 --- /dev/null +++ b/ecommerce/ecommerce/urls.py @@ -0,0 +1,22 @@ +"""ecommerce URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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') +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') +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')) +""" +from django.contrib import admin +from django.urls import path , include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('product/' , include("product.urls")), +] diff --git a/ecommerce/ecommerce/wsgi.py b/ecommerce/ecommerce/wsgi.py new file mode 100644 index 00000000..08ec9c52 --- /dev/null +++ b/ecommerce/ecommerce/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for ecommerce project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ecommerce.settings') + +application = get_wsgi_application() diff --git a/ecommerce/manage.py b/ecommerce/manage.py new file mode 100644 index 00000000..f089920d --- /dev/null +++ b/ecommerce/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ecommerce.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/ecommerce/product/__init__.py b/ecommerce/product/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ecommerce/product/__pycache__/__init__.cpython-39.pyc b/ecommerce/product/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 00000000..9d913698 Binary files /dev/null and b/ecommerce/product/__pycache__/__init__.cpython-39.pyc differ diff --git a/ecommerce/product/__pycache__/admin.cpython-39.pyc b/ecommerce/product/__pycache__/admin.cpython-39.pyc new file mode 100644 index 00000000..7b0ad1d8 Binary files /dev/null and b/ecommerce/product/__pycache__/admin.cpython-39.pyc differ diff --git a/ecommerce/product/__pycache__/apps.cpython-39.pyc b/ecommerce/product/__pycache__/apps.cpython-39.pyc new file mode 100644 index 00000000..b32d9b32 Binary files /dev/null and b/ecommerce/product/__pycache__/apps.cpython-39.pyc differ diff --git a/ecommerce/product/__pycache__/models.cpython-39.pyc b/ecommerce/product/__pycache__/models.cpython-39.pyc new file mode 100644 index 00000000..92b42215 Binary files /dev/null and b/ecommerce/product/__pycache__/models.cpython-39.pyc differ diff --git a/ecommerce/product/__pycache__/serializers.cpython-39.pyc b/ecommerce/product/__pycache__/serializers.cpython-39.pyc new file mode 100644 index 00000000..7a931840 Binary files /dev/null and b/ecommerce/product/__pycache__/serializers.cpython-39.pyc differ diff --git a/ecommerce/product/__pycache__/urls.cpython-39.pyc b/ecommerce/product/__pycache__/urls.cpython-39.pyc new file mode 100644 index 00000000..e09bfc13 Binary files /dev/null and b/ecommerce/product/__pycache__/urls.cpython-39.pyc differ diff --git a/ecommerce/product/__pycache__/views.cpython-39.pyc b/ecommerce/product/__pycache__/views.cpython-39.pyc new file mode 100644 index 00000000..a4b09161 Binary files /dev/null and b/ecommerce/product/__pycache__/views.cpython-39.pyc differ diff --git a/ecommerce/product/admin.py b/ecommerce/product/admin.py new file mode 100644 index 00000000..ea5d68b7 --- /dev/null +++ b/ecommerce/product/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/ecommerce/product/apps.py b/ecommerce/product/apps.py new file mode 100644 index 00000000..ea89db86 --- /dev/null +++ b/ecommerce/product/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ProductConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'product' diff --git a/ecommerce/product/migrations/__init__.py b/ecommerce/product/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ecommerce/product/migrations/__pycache__/__init__.cpython-39.pyc b/ecommerce/product/migrations/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 00000000..f6b7dc45 Binary files /dev/null and b/ecommerce/product/migrations/__pycache__/__init__.cpython-39.pyc differ diff --git a/ecommerce/product/models.py b/ecommerce/product/models.py new file mode 100644 index 00000000..d28373e4 --- /dev/null +++ b/ecommerce/product/models.py @@ -0,0 +1,21 @@ +from django.db import models +from statistics import mode + +# Create your models here. + +class Brand(models.Model): + title = models.CharField(max_length=512) + description = models.TextField() + established_at = models.DateField() + city = models.CharField(max_length=512) + + +class Product(models.Model): + brand = models.ForeignKey(Brand, on_delete=models.CASCADE) + name = models.CharField(max_length=512) + description = models.TextField() + image_url = models.URLField() + price = models.FloatField() + quantity = models.IntegerField() + is_active = models.BooleanField() + \ No newline at end of file diff --git a/ecommerce/product/serializers.py b/ecommerce/product/serializers.py new file mode 100644 index 00000000..bab0fd3e --- /dev/null +++ b/ecommerce/product/serializers.py @@ -0,0 +1,15 @@ +from rest_framework import serializers + +from .models import Product, Brand + + +class ProductSerializer(serializers.ModelSerializer): + class Meta: + model = Product + fields = "__all__" + + +class BrandSerializer(serializers.ModelSerializer): + class Meta: + model = Brand + fields = "__all__" \ No newline at end of file diff --git a/ecommerce/product/tests.py b/ecommerce/product/tests.py new file mode 100644 index 00000000..de8bdc00 --- /dev/null +++ b/ecommerce/product/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/ecommerce/product/urls.py b/ecommerce/product/urls.py new file mode 100644 index 00000000..93799fc1 --- /dev/null +++ b/ecommerce/product/urls.py @@ -0,0 +1,18 @@ +from django.urls import path +from . import views + +app_name = "product_brand" + +urlpatterns = [ + path("create/", views.create_product, name="create_product"), + path("read/", views.read_products, name="read_products"), + path("update//", views.update_product, name="update_product"), + path("delete//", views.delete_product, name="delete_product"), + + path("brands/create/", views.create_brand, name="create_brand"), + path("brands/read/", views.read_brands, name="read_brands"), + path("brands/update//", views.update_brand, name="update_brand"), + path("brands/delete//", views.delete_brand, name="delete_brand"), + + path('brand_products//', views.read_brand_products, name="read_brand_products") +] \ No newline at end of file diff --git a/ecommerce/product/views.py b/ecommerce/product/views.py new file mode 100644 index 00000000..db883e85 --- /dev/null +++ b/ecommerce/product/views.py @@ -0,0 +1,229 @@ +from unittest import skip +from rest_framework.decorators import api_view +from rest_framework.request import Request +from rest_framework.response import Response +from rest_framework import status + +from .models import Product, Brand +from .serializers import ProductSerializer, BrandSerializer + +# Create your views here. +''' + +product + + +''' + +@api_view(["POST"]) +def create_product(request: Request): + product_serializer = ProductSerializer(data=request.data) + if product_serializer.is_valid(): + product_serializer.save() + else: + return Response( + { + "msg": "Product object invalid.", + "error": product_serializer.errors + }, + status=status.HTTP_403_FORBIDDEN + ) + + return Response( + { + "msg": "Product created." + }, + status=status.HTTP_201_CREATED + ) + + +@api_view(["GET"]) +def read_products(request: Request): + skip = int(request.query_params.get("skip", 0)) + get = int(request.query_params.get("get", 10)) + + all_products = Product.objects.all()[skip:get] + + return Response( + { + "msg": f'list of {len(all_products)} products, total products {Product.objects.count()}', + "products": ProductSerializer(instance=all_products, many=True).data + }, + status=status.HTTP_200_OK + ) + + +@api_view(["PUT"]) +def update_product(request: Request, product_id): + try: + product = Product.objects.get(id=product_id) + except Exception as e: + return Response( + { + "msg": f'product not found.', + }, + status=status.HTTP_404_NOT_FOUND + ) + product_serializer = ProductSerializer(instance=product, data=request.data) + if product_serializer.is_valid(): + product_serializer.save() + else: + return Response( + { + "msg": "Product object invalid.", + "error": product_serializer.errors + }, + status=status.HTTP_403_FORBIDDEN + ) + return Response( + { + "msg": f'product updated.', + }, + status=status.HTTP_200_OK + ) + + +@api_view(["DELETE"]) +def delete_product(request: Request, product_id): + try: + product = Product.objects.get(id=product_id) + product.delete() + except Exception as e: + return Response( + { + "msg": f'product not found.', + }, + status=status.HTTP_404_NOT_FOUND + ) + return Response( + { + "msg": f'{product.title} product deleted.', + }, + status=status.HTTP_200_OK + ) + + +''' + +Brand + + +''' + + +@api_view(["POST"]) +def create_brand(request: Request): + brand_serializer = BrandSerializer(data=request.data) + if brand_serializer.is_valid(): + brand_serializer.save() + else: + return Response( + { + "msg": "Brand object invalid.", + "error": brand_serializer.errors + }, + status=status.HTTP_403_FORBIDDEN + ) + + return Response( + { + "msg": "Brand created." + }, + status=status.HTTP_201_CREATED + ) + + +@api_view(["GET"]) +def read_brands(request: Request): + skip = int(request.query_params.get("skip", 0)) + get = int(request.query_params.get("get", 10)) + + if "search" in request.query_params: + search_phrase = request.query_params["search"] + all_products = Brand.objects.filter( + title__startswith=search_phrase)[skip:get] + else: + all_products = Brand.objects.all()[skip:get] + + return Response( + { + "msg": f'list of {len(all_products)} brands', + "brands": BrandSerializer(instance=all_products, many=True).data + }, + status=status.HTTP_200_OK + ) + + +@api_view(["PUT"]) +def update_brand(request: Request, brand_id): + try: + brand = Brand.objects.get(id=brand_id) + except Exception as e: + return Response( + { + "msg": f'brand not found.', + }, + status=status.HTTP_404_NOT_FOUND + ) + brand_serializer = BrandSerializer(instance=brand, data=request.data) + if brand_serializer.is_valid(): + brand_serializer.save() + else: + return Response( + { + "msg": "Brand object invalid.", + "error": brand_serializer.errors + }, + status=status.HTTP_403_FORBIDDEN + ) + return Response( + { + "msg": f'brand updated.', + }, + status=status.HTTP_200_OK + ) + + +@api_view(["DELETE"]) +def delete_brand(request: Request, brand_id): + try: + brand = Brand.objects.get(id=brand_id) + brand.delete() + except Exception as e: + return Response( + { + "msg": f'brand not found.', + }, + status=status.HTTP_404_NOT_FOUND + ) + return Response( + { + "msg": f'{brand.title} brand deleted.', + }, + status=status.HTTP_200_OK + ) + + +@api_view(["GET"]) +def read_brand_products(request: Request, brand_id): + skip = int(request.query_params.get("skip", 0)) + get = int(request.query_params.get("get", 10)) + + try: + brand = Brand.objects.get(id=brand_id) + except Exception as e: + return Response( + { + "msg": f'brand not found.', + }, + status=status.HTTP_404_NOT_FOUND + ) + + all_products = Product.objects.filter(brand=brand_id)[skip:get] + return Response( + { + "msg": f'list of {len(all_products)} products branded {brand.title}', + "products": ProductSerializer(instance=all_products, many=True).data + }, + status=status.HTTP_200_OK + ) \ No newline at end of file