Skip to content
Open

:( #26

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added eCommerce/brand/__init__.py
Empty file.
Binary file added eCommerce/brand/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added eCommerce/brand/__pycache__/admin.cpython-310.pyc
Binary file not shown.
Binary file added eCommerce/brand/__pycache__/apps.cpython-310.pyc
Binary file not shown.
Binary file added eCommerce/brand/__pycache__/models.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file added eCommerce/brand/__pycache__/urls.cpython-310.pyc
Binary file not shown.
Binary file added eCommerce/brand/__pycache__/views.cpython-310.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions eCommerce/brand/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions eCommerce/brand/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class BrandConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'brand'
38 changes: 38 additions & 0 deletions eCommerce/brand/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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')),
],
),
]
18 changes: 18 additions & 0 deletions eCommerce/brand/migrations/0002_alter_product_price.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
19 changes: 19 additions & 0 deletions eCommerce/brand/models.py
Original file line number Diff line number Diff line change
@@ -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()
10 changes: 10 additions & 0 deletions eCommerce/brand/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from rest_framework import serializers

from .models import Product


class ProductSerializer(serializers.ModelSerializer):

class Meta:
model = Product
fields = '__all__'
3 changes: 3 additions & 0 deletions eCommerce/brand/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
19 changes: 19 additions & 0 deletions eCommerce/brand/urls.py
Original file line number Diff line number Diff line change
@@ -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/<brand_id>/", views.update_brand, name="update_brand"),
path("delete/<brand_id>/", 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/<product_id>/", views.update_product, name="update_product"),
path("products/delete/<product_id>/", views.delete_product, name="delete_product"),
path("products/get/<product_id>/", views.get_product, name="get_product"),

]
138 changes: 138 additions & 0 deletions eCommerce/brand/views.py
Original file line number Diff line number Diff line change
@@ -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)




Binary file added eCommerce/db.sqlite3
Binary file not shown.
Empty file added eCommerce/eCommerce/__init__.py
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added eCommerce/eCommerce/__pycache__/wsgi.cpython-310.pyc
Binary file not shown.
16 changes: 16 additions & 0 deletions eCommerce/eCommerce/asgi.py
Original file line number Diff line number Diff line change
@@ -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()
Loading