diff --git a/eCommerce/brand/__init__.py b/eCommerce/brand/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/eCommerce/brand/__pycache__/__init__.cpython-310.pyc b/eCommerce/brand/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 00000000..06296c6f Binary files /dev/null and b/eCommerce/brand/__pycache__/__init__.cpython-310.pyc differ diff --git a/eCommerce/brand/__pycache__/admin.cpython-310.pyc b/eCommerce/brand/__pycache__/admin.cpython-310.pyc new file mode 100644 index 00000000..47f83cb4 Binary files /dev/null and b/eCommerce/brand/__pycache__/admin.cpython-310.pyc differ diff --git a/eCommerce/brand/__pycache__/apps.cpython-310.pyc b/eCommerce/brand/__pycache__/apps.cpython-310.pyc new file mode 100644 index 00000000..df3a9af9 Binary files /dev/null and b/eCommerce/brand/__pycache__/apps.cpython-310.pyc differ diff --git a/eCommerce/brand/__pycache__/models.cpython-310.pyc b/eCommerce/brand/__pycache__/models.cpython-310.pyc new file mode 100644 index 00000000..57bfb884 Binary files /dev/null and b/eCommerce/brand/__pycache__/models.cpython-310.pyc differ diff --git a/eCommerce/brand/__pycache__/serializers.cpython-310.pyc b/eCommerce/brand/__pycache__/serializers.cpython-310.pyc new file mode 100644 index 00000000..2bf02547 Binary files /dev/null and b/eCommerce/brand/__pycache__/serializers.cpython-310.pyc differ diff --git a/eCommerce/brand/__pycache__/urls.cpython-310.pyc b/eCommerce/brand/__pycache__/urls.cpython-310.pyc new file mode 100644 index 00000000..21cf882a Binary files /dev/null and b/eCommerce/brand/__pycache__/urls.cpython-310.pyc differ diff --git a/eCommerce/brand/__pycache__/views.cpython-310.pyc b/eCommerce/brand/__pycache__/views.cpython-310.pyc new file mode 100644 index 00000000..74a7c753 Binary files /dev/null and b/eCommerce/brand/__pycache__/views.cpython-310.pyc differ diff --git a/eCommerce/brand/admin.py b/eCommerce/brand/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/eCommerce/brand/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/eCommerce/brand/apps.py b/eCommerce/brand/apps.py new file mode 100644 index 00000000..cb0a8d86 --- /dev/null +++ b/eCommerce/brand/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class BrandConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'brand' diff --git a/eCommerce/brand/migrations/0001_initial.py b/eCommerce/brand/migrations/0001_initial.py new file mode 100644 index 00000000..da8ca3cc --- /dev/null +++ b/eCommerce/brand/migrations/0001_initial.py @@ -0,0 +1,38 @@ +# Generated by Django 4.0.6 on 2022-08-01 19:33 + +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=128)), + ('description', models.TextField()), + ('image_url', models.URLField()), + ('price', models.DecimalField(decimal_places=10, max_digits=10)), + ('quantity', models.IntegerField()), + ('is_active', models.BooleanField()), + ('brand', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='brand.brand')), + ], + ), + ] diff --git a/eCommerce/brand/migrations/0002_alter_product_price.py b/eCommerce/brand/migrations/0002_alter_product_price.py new file mode 100644 index 00000000..210e8bdf --- /dev/null +++ b/eCommerce/brand/migrations/0002_alter_product_price.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.6 on 2022-08-01 20:13 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('brand', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='product', + name='price', + field=models.DecimalField(decimal_places=2, max_digits=10), + ), + ] diff --git a/eCommerce/brand/migrations/__init__.py b/eCommerce/brand/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/eCommerce/brand/migrations/__pycache__/0001_initial.cpython-310.pyc b/eCommerce/brand/migrations/__pycache__/0001_initial.cpython-310.pyc new file mode 100644 index 00000000..d75dff33 Binary files /dev/null and b/eCommerce/brand/migrations/__pycache__/0001_initial.cpython-310.pyc differ diff --git a/eCommerce/brand/migrations/__pycache__/0002_alter_product_price.cpython-310.pyc b/eCommerce/brand/migrations/__pycache__/0002_alter_product_price.cpython-310.pyc new file mode 100644 index 00000000..3a0908e7 Binary files /dev/null and b/eCommerce/brand/migrations/__pycache__/0002_alter_product_price.cpython-310.pyc differ diff --git a/eCommerce/brand/migrations/__pycache__/__init__.cpython-310.pyc b/eCommerce/brand/migrations/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 00000000..e70c36ba Binary files /dev/null and b/eCommerce/brand/migrations/__pycache__/__init__.cpython-310.pyc differ diff --git a/eCommerce/brand/models.py b/eCommerce/brand/models.py new file mode 100644 index 00000000..dcf0bdfd --- /dev/null +++ b/eCommerce/brand/models.py @@ -0,0 +1,19 @@ +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=128) + description = models.TextField() + image_url = models.URLField() + price = models.DecimalField(max_digits=10, decimal_places=2) + quantity = models.IntegerField() + is_active = models.BooleanField() \ No newline at end of file diff --git a/eCommerce/brand/serializers.py b/eCommerce/brand/serializers.py new file mode 100644 index 00000000..6cfc2f55 --- /dev/null +++ b/eCommerce/brand/serializers.py @@ -0,0 +1,10 @@ +from rest_framework import serializers + +from .models import Product + + +class ProductSerializer(serializers.ModelSerializer): + + class Meta: + model = Product + fields = '__all__' \ No newline at end of file diff --git a/eCommerce/brand/tests.py b/eCommerce/brand/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/eCommerce/brand/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/eCommerce/brand/urls.py b/eCommerce/brand/urls.py new file mode 100644 index 00000000..c2789ab3 --- /dev/null +++ b/eCommerce/brand/urls.py @@ -0,0 +1,19 @@ +from django.urls import path + +from . import views + +app_name = "brand" + +urlpatterns = [ + path("add/", views.add_brand, name="add_brand"), + path("all/", views.list_brands, name="list_brands"), + path("update//", views.update_brand, name="update_brand"), + path("delete//", views.delete_brand, name="delete_brand"), + + path("products/add/", views.add_product, name="add_product"), + path("products/all/", views.list_products, name="list_products"), + path("products/update//", views.update_product, name="update_product"), + path("products/delete//", views.delete_product, name="delete_product"), + path("products/get//", views.get_product, name="get_product"), + +] diff --git a/eCommerce/brand/views.py b/eCommerce/brand/views.py new file mode 100644 index 00000000..ec60bf1e --- /dev/null +++ b/eCommerce/brand/views.py @@ -0,0 +1,138 @@ +from hashlib import new +from rest_framework.decorators import api_view +from rest_framework.response import Response +from rest_framework.request import Request + +from rest_framework import status + +from .models import Brand, Product +from .serializers import ProductSerializer + + +@api_view(["POST"]) +def add_brand(request : Request): + + title = request.data["title"] + description = request.data["description"] + established_at = request.data["established_at"] + city = request.data["city"] + + new_brand = Brand(title=title, description=description, established_at=established_at, city=city) + new_brand.save() + + res_data = { + "msg" : "Created Brand Successfully" + } + + return Response(res_data) + + +@api_view(["GET"]) +def list_brands(request : Request): + all_brands=Brand.objects.all() + all_brands_list = [{"id" : brand.id, "title" : brand.title, "description": brand.description, "city": brand.city} for brand in all_brands] + + res_data = { + "msg" : "A list of All Brands", + "books" : all_brands_list + } + return Response(res_data, status=status.HTTP_200_OK) + + + +@api_view(['PUT']) +def update_brand(request : Request, brand_id): + + brand=Brand.objects.get(id=brand_id) + + brand.title = request.data["title"] + brand.description = request.data["description"] + brand.established_at = request.data["established_at"] + brand.city = request.data["city"] + brand.save() + + return Response({"msg" : "Your brand is updated !"}) + + + +@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" : "The brand is not Found!"}) + + return Response({"msg" : f"delete the following brand {brand.title}"}) + + + +@api_view(['POST']) +def add_product(request : Request): + + product_serializer = ProductSerializer(data=request.data) + + if product_serializer.is_valid(): + product_serializer.save() + else: + return Response({"msg" : "couldn't create a product", "errors" : product_serializer.errors}, status=status.HTTP_403_FORBIDDEN) + + return Response({"msg" : "Product Added Successfully!"}, status=status.HTTP_201_CREATED) + + + +@api_view(['GET']) +def list_products(request : Request): + + products = Product.objects.all() + products_data = ProductSerializer(instance=products, many=True).data + + return Response({"msg" : "list of all products", "products" : products_data}) + + +@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" : "This product is 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" : "couldn't update", "errors" : product_serializer.errors}) + + return Response({"msg" : "Product updated successfully"}) + + + +@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" : "The product is not Found!"}) + + return Response({"msg" : f"delete the following product {product.name}"}) + + + +@api_view(['GET']) +def get_product(request : Request, product_id): + + try: + product = Product.objects.get(id=product_id) + product_serializer = ProductSerializer(instance=product, data=request.data) + return Response({"msg" : "Product updated successfully", "product": product_serializer}) + except Exception as e: + return Response({"msg" : "This product is not found"}, status=status.HTTP_404_NOT_FOUND) + + + + diff --git a/eCommerce/db.sqlite3 b/eCommerce/db.sqlite3 new file mode 100644 index 00000000..f0e8aacf 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..c6eaeb4d 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..d04fcc40 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..1a4b5f0d 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..970db9ce 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..a466476d --- /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. + +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-%yc8@ya&nr+5$*w)n%^1^efv7ih!tc6iu311prc(e$ap+6%hcj' + +# 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', + 'brand', +] + +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..687ad1a5 --- /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('brands/', include("brand.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/manage.py b/eCommerce/manage.py new file mode 100755 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()