mirror of
https://github.com/andwati/store-api.git
synced 2026-10-18 01:12:52 +00:00
completed products views
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
from django.urls import path
|
||||||
|
|
||||||
|
from .views import LoginView, RegisterView
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path("register", RegisterView.as_view(), name="register"),
|
||||||
|
path("login", LoginView.as_view(), name="login"),
|
||||||
|
]
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from django.contrib.auth.models import User
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
@@ -8,6 +9,7 @@ class Product(models.Model):
|
|||||||
price = models.DecimalField(max_digits=10, decimal_places=2)
|
price = models.DecimalField(max_digits=10, decimal_places=2)
|
||||||
quantity = models.IntegerField(default=0)
|
quantity = models.IntegerField(default=0)
|
||||||
barcode = models.CharField(max_length=100, blank=True, null=True)
|
barcode = models.CharField(max_length=100, blank=True, null=True)
|
||||||
|
owner = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.name}"
|
return f"{self.name}"
|
||||||
|
|||||||
@@ -6,4 +6,4 @@ from .models import Product
|
|||||||
class ProductSerializer(serializers.ModelSerializer):
|
class ProductSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Product
|
model = Product
|
||||||
fields = "__all__"
|
fields = ["id", "name", "description", "sku", "price", "quantity", "barcode"]
|
||||||
|
|||||||
+15
-6
@@ -1,8 +1,17 @@
|
|||||||
from django.urls import include, path
|
# from django.urls import include, path
|
||||||
from rest_framework.routers import DefaultRouter
|
# from rest_framework.routers import DefaultRouter
|
||||||
|
|
||||||
from .views import ProductViewSet
|
# from .views import ProductViewSet
|
||||||
|
|
||||||
router = DefaultRouter()
|
# router = DefaultRouter()
|
||||||
router.register(r"Products", ProductViewSet)
|
# router.register(r"Products", ProductViewSet)
|
||||||
urlpatterns = router.urls
|
# urlpatterns = router.urls
|
||||||
|
|
||||||
|
from django.urls import path
|
||||||
|
|
||||||
|
from .views import ProductDetailView, ProductList
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path("", ProductList.as_view(), name="products"),
|
||||||
|
path("<int:id>", ProductDetailView.as_view(), name="product-detail"),
|
||||||
|
]
|
||||||
|
|||||||
+64
-38
@@ -1,49 +1,75 @@
|
|||||||
from rest_framework import status, viewsets
|
# from rest_framework import status, viewsets
|
||||||
from rest_framework.response import Response
|
# from rest_framework.response import Response
|
||||||
|
|
||||||
|
# from .models import Product
|
||||||
|
# from .serializers import ProductSerializer
|
||||||
|
|
||||||
|
|
||||||
|
# class ProductViewSet(viewsets.ModelViewSet):
|
||||||
|
# queryset = Product.objects.all()
|
||||||
|
# serializer_class = ProductSerializer
|
||||||
|
|
||||||
|
# def list(self, request, *args, **kwargs):
|
||||||
|
# data = list(Product.objects.all().values())
|
||||||
|
# return Response(data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
# def retrieve(self, request, *args, **kwargs):
|
||||||
|
# data = list(Product.objects.filter(id=kwargs["pk"]).values())
|
||||||
|
# return Response(data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
# def create(self, request, *args, **kwargs):
|
||||||
|
# product_serializer_data = ProductSerializer(data=request.data)
|
||||||
|
# if product_serializer_data.is_valid():
|
||||||
|
# product_serializer_data.save()
|
||||||
|
# status_code = status.HTTP_201_CREATED
|
||||||
|
# return Response({"message": "Product added successfully"}, status=status_code)
|
||||||
|
# else:
|
||||||
|
# status_code = status.HTTP_400_BAD_REQUEST
|
||||||
|
# return Response({"message": "please fill the details"}, status=status_code)
|
||||||
|
|
||||||
|
# def destroy(self, request, *args, **kwargs):
|
||||||
|
# product_data = Product.objects.filter(id=kwargs["pk"])
|
||||||
|
# if product_data:
|
||||||
|
# product_data.delete()
|
||||||
|
# status_code = status.HTTP_201_CREATED
|
||||||
|
# return Response({"message": "Product deleted successfully"}, status=status_code)
|
||||||
|
# else:
|
||||||
|
# status_code = status.HTTP_400_BAD_REQUEST
|
||||||
|
# return Response({"message": "Product not found"}, status=status_code)
|
||||||
|
|
||||||
|
# def update(self, request, *args, **kwargs):
|
||||||
|
# product_details = Product.objects.get(id=kwargs["pk"])
|
||||||
|
# product_serializer_data = ProductSerializer(product_details, data=request, partial=True)
|
||||||
|
# if product_serializer_data.is_valid():
|
||||||
|
# product_serializer_data.save()
|
||||||
|
# status_code = status.HTTP_201_CREATED
|
||||||
|
# return Response({"message": "Product updated successfully"}, status=status_code)
|
||||||
|
# else:
|
||||||
|
# status_code = status.HTTP_400_BAD_REQUEST
|
||||||
|
# return Response({"message": "Product not found"}, status=status_code)
|
||||||
|
|
||||||
|
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView
|
||||||
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
|
||||||
from .models import Product
|
from .models import Product
|
||||||
from .serializers import ProductSerializer
|
from .serializers import ProductSerializer
|
||||||
|
|
||||||
|
|
||||||
class ProductViewSet(viewsets.ModelViewSet):
|
class ProductList(ListCreateAPIView):
|
||||||
queryset = Product.objects.all()
|
|
||||||
serializer_class = ProductSerializer
|
serializer_class = ProductSerializer
|
||||||
|
permission_classes = (IsAuthenticated,)
|
||||||
|
|
||||||
def list(self, request, *args, **kwargs):
|
def perform_create(self, serializer):
|
||||||
data = list(Product.objects.all().values())
|
serializer.save(owner=self.request.user)
|
||||||
return Response(data, status=status.HTTP_200_OK)
|
|
||||||
|
|
||||||
def retrieve(self, request, *args, **kwargs):
|
def get_queryset(self):
|
||||||
data = list(Product.objects.filter(id=kwargs["pk"]).values())
|
return Product.objects.filter(owner=self.request.user)
|
||||||
return Response(data, status=status.HTTP_200_OK)
|
|
||||||
|
|
||||||
def create(self, request, *args, **kwargs):
|
|
||||||
product_serializer_data = ProductSerializer(data=request.data)
|
|
||||||
if product_serializer_data.is_valid():
|
|
||||||
product_serializer_data.save()
|
|
||||||
status_code = status.HTTP_201_CREATED
|
|
||||||
return Response({"message": "Product added successfully"}, status=status_code)
|
|
||||||
else:
|
|
||||||
status_code = status.HTTP_400_BAD_REQUEST
|
|
||||||
return Response({"message": "please fill the details"}, status=status_code)
|
|
||||||
|
|
||||||
def destroy(self, request, *args, **kwargs):
|
class ProductDetailView(RetrieveUpdateDestroyAPIView):
|
||||||
product_data = Product.objects.filter(id=kwargs["pk"])
|
serializer_class = ProductSerializer
|
||||||
if product_data:
|
permission_classes = (IsAuthenticated,)
|
||||||
product_data.delete()
|
lookup_field = "id"
|
||||||
status_code = status.HTTP_201_CREATED
|
|
||||||
return Response({"message": "Product deleted successfully"}, status=status_code)
|
|
||||||
else:
|
|
||||||
status_code = status.HTTP_400_BAD_REQUEST
|
|
||||||
return Response({"message": "Product not found"}, status=status_code)
|
|
||||||
|
|
||||||
def update(self, request, *args, **kwargs):
|
def get_queryset(self):
|
||||||
product_details = Product.objects.get(id=kwargs["pk"])
|
return Product.objects.filter(owner=self.request.user)
|
||||||
product_serializer_data = ProductSerializer(product_details, data=request, partial=True)
|
|
||||||
if product_serializer_data.is_valid():
|
|
||||||
product_serializer_data.save()
|
|
||||||
status_code = status.HTTP_201_CREATED
|
|
||||||
return Response({"message": "Product updated successfully"}, status=status_code)
|
|
||||||
else:
|
|
||||||
status_code = status.HTTP_400_BAD_REQUEST
|
|
||||||
return Response({"message": "Product not found"}, status=status_code)
|
|
||||||
|
|||||||
+4
-5
@@ -30,13 +30,12 @@ schema_view = get_schema_view(
|
|||||||
license=openapi.License(name="MIT License"),
|
license=openapi.License(name="MIT License"),
|
||||||
),
|
),
|
||||||
public=True,
|
public=True,
|
||||||
permission_classes=[
|
permission_classes=(permissions.AllowAny,),
|
||||||
permissions.AllowAny,
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("admin/", admin.site.urls),
|
path("admin/", admin.site.urls),
|
||||||
path("api/", include("products.urls")),
|
path("api/auth/", include("authentication.urls")),
|
||||||
path("docs/", schema_view.with_ui("swagger", cache_timeout=0), name="schema-swagger-ui"),
|
path("api/products/", include("products.urls")),
|
||||||
|
path("", schema_view.with_ui("swagger", cache_timeout=0), name="schema-swagger-ui"),
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user