diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..eba74f4c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +venv/ \ No newline at end of file diff --git a/eCommerce/db.sqlite3 b/eCommerce/db.sqlite3 new file mode 100644 index 00000000..fb0b7b2a Binary files /dev/null and b/eCommerce/db.sqlite3 differ 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-310.pyc b/eCommerce/eCommerce/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 00000000..9db6ebc6 Binary files /dev/null and b/eCommerce/eCommerce/__pycache__/__init__.cpython-310.pyc differ diff --git a/eCommerce/eCommerce/__pycache__/settings.cpython-310.pyc b/eCommerce/eCommerce/__pycache__/settings.cpython-310.pyc new file mode 100644 index 00000000..bb3d7c3d Binary files /dev/null and b/eCommerce/eCommerce/__pycache__/settings.cpython-310.pyc differ diff --git a/eCommerce/eCommerce/__pycache__/urls.cpython-310.pyc b/eCommerce/eCommerce/__pycache__/urls.cpython-310.pyc new file mode 100644 index 00000000..2b4b8fa7 Binary files /dev/null and b/eCommerce/eCommerce/__pycache__/urls.cpython-310.pyc differ diff --git a/eCommerce/eCommerce/__pycache__/wsgi.cpython-310.pyc b/eCommerce/eCommerce/__pycache__/wsgi.cpython-310.pyc new file mode 100644 index 00000000..85979959 Binary files /dev/null and b/eCommerce/eCommerce/__pycache__/wsgi.cpython-310.pyc differ diff --git a/eCommerce/eCommerce/asgi.py b/eCommerce/eCommerce/asgi.py new file mode 100644 index 00000000..534f83bb --- /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..30316eeb --- /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-@mgm%rm+5a$!kcf=r$*u==ljbc=tqjt*8ca-$ww%psns90-$*q' + +# 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', + 'rest_framework', + 'eCommerce_app' +] + +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..964b5d0b --- /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('products/', include("eCommerce_app.urls")) +] diff --git a/eCommerce/eCommerce/wsgi.py b/eCommerce/eCommerce/wsgi.py new file mode 100644 index 00000000..18d60638 --- /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/eCommerce_app/__init__.py b/eCommerce/eCommerce_app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/eCommerce/eCommerce_app/__pycache__/__init__.cpython-310.pyc b/eCommerce/eCommerce_app/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 00000000..6432dd13 Binary files /dev/null and b/eCommerce/eCommerce_app/__pycache__/__init__.cpython-310.pyc differ diff --git a/eCommerce/eCommerce_app/__pycache__/admin.cpython-310.pyc b/eCommerce/eCommerce_app/__pycache__/admin.cpython-310.pyc new file mode 100644 index 00000000..e49d827e Binary files /dev/null and b/eCommerce/eCommerce_app/__pycache__/admin.cpython-310.pyc differ diff --git a/eCommerce/eCommerce_app/__pycache__/apps.cpython-310.pyc b/eCommerce/eCommerce_app/__pycache__/apps.cpython-310.pyc new file mode 100644 index 00000000..c6e793df Binary files /dev/null and b/eCommerce/eCommerce_app/__pycache__/apps.cpython-310.pyc differ diff --git a/eCommerce/eCommerce_app/__pycache__/models.cpython-310.pyc b/eCommerce/eCommerce_app/__pycache__/models.cpython-310.pyc new file mode 100644 index 00000000..136b6a2e Binary files /dev/null and b/eCommerce/eCommerce_app/__pycache__/models.cpython-310.pyc differ diff --git a/eCommerce/eCommerce_app/__pycache__/serializers.cpython-310.pyc b/eCommerce/eCommerce_app/__pycache__/serializers.cpython-310.pyc new file mode 100644 index 00000000..167f6680 Binary files /dev/null and b/eCommerce/eCommerce_app/__pycache__/serializers.cpython-310.pyc differ diff --git a/eCommerce/eCommerce_app/__pycache__/urls.cpython-310.pyc b/eCommerce/eCommerce_app/__pycache__/urls.cpython-310.pyc new file mode 100644 index 00000000..c276a487 Binary files /dev/null and b/eCommerce/eCommerce_app/__pycache__/urls.cpython-310.pyc differ diff --git a/eCommerce/eCommerce_app/__pycache__/views.cpython-310.pyc b/eCommerce/eCommerce_app/__pycache__/views.cpython-310.pyc new file mode 100644 index 00000000..07f6a9bf Binary files /dev/null and b/eCommerce/eCommerce_app/__pycache__/views.cpython-310.pyc differ diff --git a/eCommerce/eCommerce_app/admin.py b/eCommerce/eCommerce_app/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/eCommerce/eCommerce_app/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/eCommerce/eCommerce_app/apps.py b/eCommerce/eCommerce_app/apps.py new file mode 100644 index 00000000..85a23778 --- /dev/null +++ b/eCommerce/eCommerce_app/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class EcommerceAppConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'eCommerce_app' diff --git a/eCommerce/eCommerce_app/migrations/0001_initial.py b/eCommerce/eCommerce_app/migrations/0001_initial.py new file mode 100644 index 00000000..4ea72ab0 --- /dev/null +++ b/eCommerce/eCommerce_app/migrations/0001_initial.py @@ -0,0 +1,38 @@ +# Generated by Django 4.0.6 on 2022-07-31 18:53 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Brand', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=512)), + ('description', models.TextField()), + ('established_at', models.DateField()), + ('city', models.CharField(max_length=512)), + ], + ), + migrations.CreateModel( + name='Product', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=512)), + ('description', models.TextField()), + ('image_url', models.URLField()), + ('price', models.FloatField()), + ('quantity', models.IntegerField()), + ('is_active', models.BooleanField()), + ('brand', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='eCommerce_app.brand')), + ], + ), + ] diff --git a/eCommerce/eCommerce_app/migrations/__init__.py b/eCommerce/eCommerce_app/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/eCommerce/eCommerce_app/migrations/__pycache__/0001_initial.cpython-310.pyc b/eCommerce/eCommerce_app/migrations/__pycache__/0001_initial.cpython-310.pyc new file mode 100644 index 00000000..5b45d578 Binary files /dev/null and b/eCommerce/eCommerce_app/migrations/__pycache__/0001_initial.cpython-310.pyc differ diff --git a/eCommerce/eCommerce_app/migrations/__pycache__/__init__.cpython-310.pyc b/eCommerce/eCommerce_app/migrations/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 00000000..93ce8020 Binary files /dev/null and b/eCommerce/eCommerce_app/migrations/__pycache__/__init__.cpython-310.pyc differ diff --git a/eCommerce/eCommerce_app/models.py b/eCommerce/eCommerce_app/models.py new file mode 100644 index 00000000..7508a7f1 --- /dev/null +++ b/eCommerce/eCommerce_app/models.py @@ -0,0 +1,21 @@ +from statistics import mode +from django.db import models + +# 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() diff --git a/eCommerce/eCommerce_app/serializers.py b/eCommerce/eCommerce_app/serializers.py new file mode 100644 index 00000000..c074e0de --- /dev/null +++ b/eCommerce/eCommerce_app/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__" diff --git a/eCommerce/eCommerce_app/tests.py b/eCommerce/eCommerce_app/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/eCommerce/eCommerce_app/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/eCommerce/eCommerce_app/urls.py b/eCommerce/eCommerce_app/urls.py new file mode 100644 index 00000000..4db7107c --- /dev/null +++ b/eCommerce/eCommerce_app/urls.py @@ -0,0 +1,19 @@ +from django.urls import path +from . import views + +app_name = "eCommerce_app" + +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") +] diff --git a/eCommerce/eCommerce_app/views.py b/eCommerce/eCommerce_app/views.py new file mode 100644 index 00000000..eafb6fed --- /dev/null +++ b/eCommerce/eCommerce_app/views.py @@ -0,0 +1,221 @@ +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. + + +@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 + ) diff --git a/eCommerce/manage.py b/eCommerce/manage.py new file mode 100644 index 00000000..aa3d79c9 --- /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()