Skip to content

Commit

Permalink
Merge pull request #35 from rkshaon/shaon
Browse files Browse the repository at this point in the history
frontend & backend
  • Loading branch information
rkshaon authored Jul 24, 2024
2 parents 6e08827 + a73f5de commit d999f23
Show file tree
Hide file tree
Showing 57 changed files with 845 additions and 213 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ backend/product_api/urls/__pycache__
backend/product_api/migrations
backend/user_api/__pycache__
backend/user_api/urls/__pycache__
backend/user_api/views/__pycache__
backend/user_api/migrations
backend/db.sqlite3
backend/media
backend/media
backend/configure_api/__pycache__
backend/configure_api/migrations
backend/configure_api/urls/__pycache__
backend/configure_api/views/__pycache__
__pycache__
7 changes: 1 addition & 6 deletions backend/category_api/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
from django.db import models
from django.utils.text import slugify

from user_api.models import User




class Category(models.Model):
title = models.CharField(max_length=255, blank=False, null=False)
category_slug = models.SlugField(unique=True, blank=True, null=True)
Expand All @@ -17,11 +14,9 @@ class Category(models.Model):
is_deleted = models.BooleanField(default=False)
added_by = models.ForeignKey(User, on_delete=models.CASCADE)
added_date_time = models.DateTimeField(auto_now_add=True)


def save(self, *args, **kwargs):
super().save(*args, **kwargs)


def __str__(self):
return f"ID: {self.id}, Title: {self.title}"
return f"ID: {self.id}, Title: {self.title}"
12 changes: 5 additions & 7 deletions backend/category_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@
from category_api.models import Category



class CategorySerializer(serializers.ModelSerializer):
"""Serializer for the Category model."""
icon = serializers.FileField(required=False)

class Meta:
model = Category
fields = '__all__'


# def get_serializer(self, instance):
# """Returns a serializer instance for the given instance."""
# return self.__class__(instance=instance)


# def to_representation(self, instance):
# representation = super().to_representation(instance)

# representation['children'] = [
# self.get_serializer(child).data for child in instance.children.all()
# self.get_serializer(
# child
# ).data for child in instance.children.all()
# ]
# return representation

9 changes: 4 additions & 5 deletions backend/category_api/urls/v1.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from django.urls import path

from category_api import views

from category_api.views import v1 as v1_view


urlpatterns = [
path('', views.CategoryView.as_view()),
path('<int:pk>', views.CategoryView.as_view()),
]
path('', v1_view.CategoryView.as_view()),
path('<int:pk>', v1_view.CategoryView.as_view()),
]
58 changes: 32 additions & 26 deletions backend/category_api/views.py → backend/category_api/views/v1.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,69 @@
from rest_framework.viewsets import ViewSet
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.decorators import permission_classes, authentication_classes
from rest_framework.decorators import permission_classes
from rest_framework.decorators import authentication_classes
from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.pagination import PageNumberPagination
from rest_framework import status
from rest_framework_simplejwt.authentication import JWTAuthentication

from django.shortcuts import get_object_or_404

import copy

from category_api.models import Category
from product_api.models import Product

from category_api.serializers import CategorySerializer
from product_api.serializers import ProductSerializer



class CategoryView(APIView):
serializer_class = CategorySerializer

@authentication_classes([SessionAuthentication, JWTAuthentication])
@permission_classes([IsAuthenticated])
def post(self, request, *args, **kwargs):
request_data = copy.deepcopy(request.data)
request_data['added_by'] = request.user.id

serializer = self.serializer_class(data=request_data)

if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)

return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


def get(self, request, *args, **kwargs):
pk = kwargs.get('pk', None)

if pk:
try:
data = self.serializer_class(Category.objects.get(pk=pk)).data
data = self.serializer_class(
Category.objects.get(pk=pk)
).data
return Response(data)
except Category.DoesNotExist:
return Response({
'error': 'Category does not exist.'
}, status=status.HTTP_404_NOT_FOUND)

query = request.GET.get('query', None)

if query and query.lower() == 'only-parent':
queryset = Category.objects.filter(
is_active=True,
parent=None,
)
).order_by('-added_date_time')
elif query and query.lower() == 'only-feature':
queryset = Category.objects.filter(
is_active=True, is_featured=True, is_deleted=False)
is_active=True,
is_featured=True,
is_deleted=False
).order_by('-added_date_time')
else:
queryset = Category.objects.filter(
is_active=True, is_deleted=False)
is_active=True,
is_deleted=False
).order_by('-added_date_time')

paginator = PageNumberPagination()
page = paginator.paginate_queryset(queryset, request)
Expand All @@ -71,11 +72,10 @@ def get(self, request, *args, **kwargs):
serializer = self.serializer_class(page, many=True)

return paginator.get_paginated_response(serializer.data)

serializer = self.serializer_class(queryset, many=True)

return Response(serializer.data)


@authentication_classes([SessionAuthentication, JWTAuthentication])
@permission_classes([IsAuthenticated])
Expand All @@ -86,20 +86,23 @@ def delete(self, request, *args, **kwargs):
return Response({
'error': 'Category ID is required to delete.'
}, status=status.HTTP_400_BAD_REQUEST)

try:
category = Category.objects.get(pk=pk, is_active=True, is_deleted=False)
category = Category.objects.get(
pk=pk,
is_active=True,
is_deleted=False
)
category.is_deleted = True
category.save()
except Category.DoesNotExist:
return Response({
'error': 'Category does not exist.'
}, status=status.HTTP_404_NOT_FOUND)

return Response({
'message': 'Successfully deleted.',
}, status=status.HTTP_202_ACCEPTED)


@authentication_classes([SessionAuthentication, JWTAuthentication])
@permission_classes([IsAuthenticated])
Expand All @@ -110,19 +113,22 @@ def put(self, request, *args, **kwargs):
return Response({
'error': 'Category ID is required.',
}, status=status.HTTP_400_BAD_REQUEST)

try:
category = Category.objects.get(
pk=pk, is_active=True, is_deleted=False)
except Category.DoesNotExist:
return Response({
'error': 'Category does not exist.'
}, status=status.HTTP_404_NOT_FOUND)

serializer = CategorySerializer(category, data=request.data, partial=True)

serializer = CategorySerializer(
category,
data=request.data,
partial=True)

if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_202_ACCEPTED)

return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Empty file.
3 changes: 3 additions & 0 deletions backend/configure_api/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 backend/configure_api/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class ConfigureApiConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'configure_api'
3 changes: 3 additions & 0 deletions backend/configure_api/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions backend/configure_api/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.
8 changes: 8 additions & 0 deletions backend/configure_api/urls/v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path

from configure_api.views import v1 as v1_view


urlpatterns = [
path('setup', v1_view.AdminSetupView.as_view()),
]
21 changes: 21 additions & 0 deletions backend/configure_api/views/v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from rest_framework.views import APIView
from rest_framework.response import Response

from category_api.models import Category
from product_api.models import Product


class AdminSetupView(APIView):
def get(self, request, *args, **kwargs):
return Response({
'categories': Category.objects.filter(
is_deleted=False
).count(),
'active_categories': Category.objects.filter(
is_deleted=False,
is_active=True
).count(),
'products': Product.objects.filter(
is_deleted=False
).count(),
})
1 change: 1 addition & 0 deletions backend/ecommerce/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
'category_api',
'product_api',
'user_api',
'configure_api',
]

MIDDLEWARE = [
Expand Down
1 change: 1 addition & 0 deletions backend/ecommerce/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
path('api/v1/users/', include('user_api.urls.v1')),
path('api/v1/categories/', include('category_api.urls.v1')),
path('api/v1/products/', include('product_api.urls.v1')),
path('api/v1/configures/', include('configure_api.urls.v1')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Binary file removed backend/media/products/IMG_0464-scaled.jpg
Binary file not shown.
Binary file removed backend/media/products/IMG_0467-1-scaled.jpg
Binary file not shown.
Binary file removed backend/media/products/IMG_0473-scaled.jpg
Binary file not shown.
Binary file removed backend/media/products/IMG_0475-scaled.jpg
Binary file not shown.
Binary file removed backend/media/products/img_0480-2-scaled.jpg
Binary file not shown.
15 changes: 3 additions & 12 deletions backend/product_api/models.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
from django.db import models
from django.utils.text import slugify
# from django.conf import settings

# from django.contrib.auth.models import User
from user_api.models import User
from category_api.models import Category



class ProductImage(models.Model):
# product = models.ForeignKey(Product, related_name='images', on_delete=models.CASCADE)
image = models.ImageField(upload_to='products/')
is_active = models.BooleanField(default=True)
is_deleted = models.BooleanField(default=False)
added_by = models.ForeignKey(User, on_delete=models.CASCADE)
# added_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
added_date_time = models.DateTimeField(auto_now_add=True)


def __str__(self):
return f"{self.id}"

Expand All @@ -28,23 +22,20 @@ class Product(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
description = models.TextField(null=True, blank=True)
images = models.ManyToManyField(
to=ProductImage,
to=ProductImage,
related_name='product_images',
blank=True)
is_featured = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_deleted = models.BooleanField(default=False)
added_by = models.ForeignKey(User, on_delete=models.CASCADE)
# added_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
added_date_time = models.DateTimeField(auto_now_add=True)


def save(self, *args, **kwargs):
if not self.product_slug:
self.product_slug = slugify(self.title)

super().save(*args, **kwargs)


def __str__(self):
return f"{self.id}: {self.title}"
return f"{self.id}: {self.title}"
8 changes: 4 additions & 4 deletions backend/product_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ class Meta:
model = Product
fields = '__all__'


def to_representation(self, instance):
representation = super().to_representation(instance)

representation['images'] = [
ProductImageSerializer(image).data for image in instance.images.all()
ProductImageSerializer(
image).data for image in instance.images.all()
]
return representation


class ProductImageSerializer(serializers.ModelSerializer):
class Meta:
model = ProductImage
fields = '__all__'
fields = '__all__'
Loading

0 comments on commit d999f23

Please sign in to comment.