diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..d75edeae --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +venv +__pycache__ \ No newline at end of file diff --git a/eStore/db.sqlite3 b/eStore/db.sqlite3 new file mode 100644 index 00000000..788472e5 Binary files /dev/null and b/eStore/db.sqlite3 differ diff --git a/eStore/eStore/__init__.py b/eStore/eStore/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/eStore/eStore/asgi.py b/eStore/eStore/asgi.py new file mode 100644 index 00000000..97d26811 --- /dev/null +++ b/eStore/eStore/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for eStore 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', 'eStore.settings') + +application = get_asgi_application() diff --git a/eStore/eStore/settings.py b/eStore/eStore/settings.py new file mode 100644 index 00000000..4a654958 --- /dev/null +++ b/eStore/eStore/settings.py @@ -0,0 +1,125 @@ +""" +Django settings for eStore 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-$4o+%5x98t4zjwikt^-=iyg4l%=jd$2-59yd0vci%sns62&9ve' + +# 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', + 'estore_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 = 'eStore.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 = 'eStore.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/eStore/eStore/urls.py b/eStore/eStore/urls.py new file mode 100644 index 00000000..db109538 --- /dev/null +++ b/eStore/eStore/urls.py @@ -0,0 +1,22 @@ +"""eStore 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('store/', include("estore_app.urls")), +] diff --git a/eStore/eStore/wsgi.py b/eStore/eStore/wsgi.py new file mode 100644 index 00000000..52890a2d --- /dev/null +++ b/eStore/eStore/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for eStore 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', 'eStore.settings') + +application = get_wsgi_application() diff --git a/eStore/estore_app/__init__.py b/eStore/estore_app/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/eStore/estore_app/admin.py b/eStore/estore_app/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/eStore/estore_app/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/eStore/estore_app/apps.py b/eStore/estore_app/apps.py new file mode 100644 index 00000000..d67af4af --- /dev/null +++ b/eStore/estore_app/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class EstoreAppConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'estore_app' diff --git a/eStore/estore_app/migrations/0001_initial.py b/eStore/estore_app/migrations/0001_initial.py new file mode 100644 index 00000000..2a31695c --- /dev/null +++ b/eStore/estore_app/migrations/0001_initial.py @@ -0,0 +1,38 @@ +# Generated by Django 4.0.6 on 2022-07-31 16:40 + +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=128)), + ], + ), + 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='estore_app.brand')), + ], + ), + ] diff --git a/eStore/estore_app/migrations/__init__.py b/eStore/estore_app/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/eStore/estore_app/models.py b/eStore/estore_app/models.py new file mode 100644 index 00000000..243e7831 --- /dev/null +++ b/eStore/estore_app/models.py @@ -0,0 +1,20 @@ +from django.db import models + + +class Brand(models.Model): + #attributes + title = models.CharField(max_length=512) + description = models.TextField() + established_at = models.DateField() + city = models.CharField(max_length=128) + + +class Product(models.Model): + #attributes + 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/eStore/estore_app/serializers.py b/eStore/estore_app/serializers.py new file mode 100644 index 00000000..5f735b13 --- /dev/null +++ b/eStore/estore_app/serializers.py @@ -0,0 +1,17 @@ +from rest_framework import serializers + +from .models import Brand +from .models import Product + + +class BrandSerializer(serializers.ModelSerializer): + + class Meta: + model = Brand + fields = '__all__' + +class ProductSerializer(serializers.ModelSerializer): + + class Meta: + model = Product + fields = '__all__' \ No newline at end of file diff --git a/eStore/estore_app/tests.py b/eStore/estore_app/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/eStore/estore_app/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/eStore/estore_app/urls.py b/eStore/estore_app/urls.py new file mode 100644 index 00000000..c553fd4f --- /dev/null +++ b/eStore/estore_app/urls.py @@ -0,0 +1,25 @@ +from django.urls import path +from . import views + +app_name = "estore_app" + +urlpatterns = [ + #Brand + path("add/brand", views.new_brand, name="new_brand"), + path("all/brands", views.read_brand, name="read_brand"), + path("update/brand/", views.update_brand, name="update_brand"), + path("delete/brand/", views.delete_brand, name="delete_brand"), + #Product + path("add/product", views.new_product, name="new_product"), + path("all/products", views.read_product, name="read_product"), + path("update/product/", views.update_product, name="update_product"), + path("delete/product/", views.delete_product, name="delete_product"), + #Spicific + path("products/brand/",views.products_of_brand,name="products_of_brand"), + path("limited/brands/",views.limited_list_brand,name="limited_list_brand"), + path("limited/products/",views.limited_list_product,name="limited_list_product"), + + #Search + path("search/",views.search,name="search"), + +] \ No newline at end of file diff --git a/eStore/estore_app/views.py b/eStore/estore_app/views.py new file mode 100644 index 00000000..8abe4d1d --- /dev/null +++ b/eStore/estore_app/views.py @@ -0,0 +1,200 @@ +from hashlib import new +from urllib import response +from django.shortcuts import render +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 BrandSerializer,ProductSerializer + +# Brand... +# Add new brand +@api_view(['POST']) +def new_brand(request:Request): + + Brand_serializer = BrandSerializer(data=request.data) + + if Brand_serializer.is_valid(): + Brand_serializer.save() + else: + return Response({"Message" : "Couldn't create a brand", "errors" : Brand_serializer.errors}, status=status.HTTP_403_FORBIDDEN) + + return Response({"Message" : "Brand added successfully"}, status=status.HTTP_201_CREATED) + +# Get all brands +@api_view(['GET']) +def read_brand(request:Request): + + all_brands = Brand.objects.all() + brand_data = BrandSerializer(instance=all_brands, many=True).data + + response_data = { + "Message" : "All Brands List", + "Brand" : brand_data + } + + return Response(response_data, status=status.HTTP_200_OK) + + +# Update brand by id +@api_view(['PUT']) +def update_brand(request:Request, brand_id): + + try: + brand = Brand.objects.get(id = brand_id) + except Exception as e: + return Response({"Message" : "This brand is 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({"Message" : "Couldn't update", "errors" : brand_serializer.errors}) + + return Response({"Message" : "Brand updated successfully"}) + +# Delete brand by id +@api_view(['DELETE']) +def delete_brand(request:Request, brand_id): + + brand = Brand.objects.get(id = brand_id) + brand.delete() + + response_data = { + "Message" : "Brand Information Deleted!", + } + return Response(response_data, status=status.HTTP_200_OK) + +# Limited list of brands +@api_view(["GET"]) +def limited_list_brand(request : Request): + + skip = int(request.query_params.get("skip", 0)) + get = int(request.query_params.get("get", 2)) + + brands_list = Brand.objects.all()[skip:get] + + brands = BrandSerializer(instance=brands_list, many=True).data + + res_data = { + "msg" : "Limited list of Brands", + "Brand" : brands + } + + return Response(res_data, status=status.HTTP_200_OK) + +# ***************************************************** # + +# Product... +# Add new brand +@api_view(['POST']) +def new_product(request:Request): + + Product_serializer = ProductSerializer(data=request.data) + if Product_serializer.is_valid(): + Product_serializer.save() + + else: + return Response({"Message" : "Couldn't create a product", "errors" : Product_serializer.errors}, status=status.HTTP_403_FORBIDDEN) + + return Response({ "Message" : "Added a Product Successfully"}, status=status.HTTP_201_CREATED) + +# Get all products +@api_view(['GET']) +def read_product(request:Request): + + all_products = Product.objects.all() + product_data = ProductSerializer(instance=all_products, many=True).data + + response_data = { + "Message" : "All Products List", + "Brand" : product_data + } + + return Response(response_data, status=status.HTTP_200_OK) + +# Update product by id +@api_view(['PUT']) +def update_product(request:Request,product_id): + + try: + product = Product.objects.get(id = product_id) + except Exception as e: + return Response({"Message" : "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({"Message" : "Couldn't update", "errors" : product_serializer.errors}) + + return Response({"Message" : "Product updated successfully"}) + +# Delete product by id +@api_view(['DELETE']) +def delete_product(request:Request, product_id): + + product = Product.objects.get(id = product_id) + product.delete() + + response_data = { + "Message" : "Product Information Deleted!", + } + return Response(response_data, status=status.HTTP_200_OK) + +# Limited list of products +@api_view(["GET"]) +def limited_list_product(request : Request): + + skip = int(request.query_params.get("skip", 0)) + get = int(request.query_params.get("get", 2)) + + products_list = Product.objects.all()[skip:get] + + products = ProductSerializer(instance=products_list, many=True).data + + res_data = { + "msg" : "Limited list of Products", + "Products" : products + } + + return Response(res_data, status=status.HTTP_200_OK) + +# Get all products of spcific brand +@api_view(['GET']) +def products_of_brand(request:Request, brand_id): + + brand = Brand.objects.get(id= brand_id) + products = Product.objects.filter(brand= brand_id) + product_data = ProductSerializer(instance=products, many=True).data + + response_data = { + "Brand" : brand.title, + "Products" : product_data + } + + return Response(response_data, status=status.HTTP_200_OK) + +# ***************************************************** # + +#Search +@api_view(["GET"]) +def search(request : Request , brand_title): + try: + brand = Brand.objects.get(title = brand_title) + except Exception as e: + return Response({"Message" : "This brand title is not found"}, status=status.HTTP_404_NOT_FOUND) + + brands = Brand.objects.filter(title = brand_title) + brands_list = BrandSerializer(instance=brands, many=True).data + + res_data = { + "Search of brand title" : brand_title, + "Brand" : brands_list + } + + return Response(res_data, status=status.HTTP_200_OK) + diff --git a/eStore/manage.py b/eStore/manage.py new file mode 100755 index 00000000..df40967e --- /dev/null +++ b/eStore/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', 'eStore.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()