Spaces:
Runtime error
Runtime error
gamingflexer
commited on
Commit
·
28f1741
1
Parent(s):
4b0bd4a
Add DatabaseSerializer and new API views
Browse files- src/app/api/serializers.py +6 -1
- src/app/api/views.py +25 -4
src/app/api/serializers.py
CHANGED
|
@@ -1,7 +1,12 @@
|
|
| 1 |
from rest_framework import serializers
|
| 2 |
-
from .models import Product
|
| 3 |
|
| 4 |
class ProductSerializer(serializers.ModelSerializer):
|
| 5 |
class Meta:
|
| 6 |
model = Product
|
| 7 |
fields = '__all__'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from rest_framework import serializers
|
| 2 |
+
from .models import Product,Database
|
| 3 |
|
| 4 |
class ProductSerializer(serializers.ModelSerializer):
|
| 5 |
class Meta:
|
| 6 |
model = Product
|
| 7 |
fields = '__all__'
|
| 8 |
+
|
| 9 |
+
class DatabaseSerializer(serializers.ModelSerializer):
|
| 10 |
+
class Meta:
|
| 11 |
+
model = Database
|
| 12 |
+
fields = '__all__'
|
src/app/api/views.py
CHANGED
|
@@ -6,14 +6,14 @@ from django.shortcuts import render, get_object_or_404
|
|
| 6 |
from rest_framework.views import APIView
|
| 7 |
from rest_framework.response import Response
|
| 8 |
from rest_framework import status
|
| 9 |
-
from .models import Product
|
| 10 |
-
from .serializers import ProductSerializer
|
| 11 |
from django.http import JsonResponse
|
| 12 |
import os
|
| 13 |
from config import BASE_PATH
|
| 14 |
from api.module.product_description import get_details
|
| 15 |
from django.views.decorators.http import require_http_methods
|
| 16 |
-
|
| 17 |
|
| 18 |
def catalouge_page(request):
|
| 19 |
# Fetch all products from the database
|
|
@@ -140,4 +140,25 @@ def delete_product_api(request, product_id):
|
|
| 140 |
product.delete()
|
| 141 |
return HttpResponseRedirect(reverse('index'))
|
| 142 |
except Product.DoesNotExist:
|
| 143 |
-
return JsonResponse({'error': 'Product not found.'}, status=404)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from rest_framework.views import APIView
|
| 7 |
from rest_framework.response import Response
|
| 8 |
from rest_framework import status
|
| 9 |
+
from .models import Product,Database
|
| 10 |
+
from .serializers import ProductSerializer,DatabaseSerializer
|
| 11 |
from django.http import JsonResponse
|
| 12 |
import os
|
| 13 |
from config import BASE_PATH
|
| 14 |
from api.module.product_description import get_details
|
| 15 |
from django.views.decorators.http import require_http_methods
|
| 16 |
+
from django.db.models import Q
|
| 17 |
|
| 18 |
def catalouge_page(request):
|
| 19 |
# Fetch all products from the database
|
|
|
|
| 140 |
product.delete()
|
| 141 |
return HttpResponseRedirect(reverse('index'))
|
| 142 |
except Product.DoesNotExist:
|
| 143 |
+
return JsonResponse({'error': 'Product not found.'}, status=404)
|
| 144 |
+
|
| 145 |
+
class SearchProducts(APIView):
|
| 146 |
+
def get(self, request):
|
| 147 |
+
name = request.query_params.get('name', '')
|
| 148 |
+
products = Database.objects.filter(Q(product_name__icontains=name) & Q(description__icontains=name))
|
| 149 |
+
db_serializer = DatabaseSerializer(products, many=True)
|
| 150 |
+
return Response(db_serializer.data)
|
| 151 |
+
|
| 152 |
+
class TotalNumberOfProducts(APIView):
|
| 153 |
+
def get(self, request):
|
| 154 |
+
count = Database.objects.count()
|
| 155 |
+
return Response({"total_number_of_products": count})
|
| 156 |
+
|
| 157 |
+
class ProductDetailsById(APIView):
|
| 158 |
+
def get(self, request, pk):
|
| 159 |
+
try:
|
| 160 |
+
product = Product.objects.get(pk=pk)
|
| 161 |
+
product_serializer = ProductSerializer(product)
|
| 162 |
+
return Response(product_serializer.data)
|
| 163 |
+
except Product.DoesNotExist:
|
| 164 |
+
return Response({"error": "Product not found"}, status=404)
|