From 4f9d2eab6d22157043566c0bd7e9cb0b54493d2e Mon Sep 17 00:00:00 2001 From: keannu125 Date: Wed, 29 Mar 2023 14:09:24 +0800 Subject: [PATCH] Polished code --- ivy/accounts/urls.py | 6 +----- ivy/accounts/views.py | 7 +++---- ivy/config/settings.py | 5 +++-- ivy/products/urls.py | 4 ++-- ivy/products/views.py | 14 ++++++-------- 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/ivy/accounts/urls.py b/ivy/accounts/urls.py index 2caa5a6..fd74add 100644 --- a/ivy/accounts/urls.py +++ b/ivy/accounts/urls.py @@ -1,13 +1,9 @@ from django.contrib import admin from django.urls import path, include -from rest_framework import routers from . import views -router = routers.DefaultRouter() -router.register(r'user_list', views.UserListViewSet) - urlpatterns = [ path('', include('djoser.urls')), path('', include('djoser.urls.authtoken')), - path('', include(router.urls)), + path('user_list/', views.UserListViewSet.as_view()) ] diff --git a/ivy/accounts/views.py b/ivy/accounts/views.py index 3aec4ae..7bb2a7b 100644 --- a/ivy/accounts/views.py +++ b/ivy/accounts/views.py @@ -1,4 +1,4 @@ -from rest_framework import viewsets +from rest_framework import viewsets, generics from .serializers import UserSerializer from django.contrib.auth.models import User from rest_framework.permissions import IsAuthenticated @@ -6,8 +6,7 @@ from rest_framework.permissions import IsAuthenticated # Create your views here. -class UserListViewSet(viewsets.ModelViewSet): - permission_classes = [IsAuthenticated] - http_method_names = ['get'] +class UserListViewSet(generics.ListAPIView): + # permission_classes = [IsAuthenticated] serializer_class = UserSerializer queryset = User.objects.all() diff --git a/ivy/config/settings.py b/ivy/config/settings.py index bb13db1..e381733 100644 --- a/ivy/config/settings.py +++ b/ivy/config/settings.py @@ -26,9 +26,10 @@ BASE_DIR = Path(__file__).resolve().parent.parent # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = os.environ.get('SECRET_KEY') # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = False +DEBUG = True -ALLOWED_HOSTS = ['lemeow125.github.io', 'keannu125.pythonanywhere.com'] +ALLOWED_HOSTS = ['lemeow125.github.io', + 'keannu125.pythonanywhere.com', '127.0.0.1'] # Application definition diff --git a/ivy/products/urls.py b/ivy/products/urls.py index c6f1356..645b17c 100644 --- a/ivy/products/urls.py +++ b/ivy/products/urls.py @@ -4,11 +4,11 @@ from . import views router = routers.DefaultRouter() router.register(r'products', views.ProductViewSet) -router.register(r'logs', views.LogViewSet) -router.register(r'lowest_stock_product', views.LeastStockProductViewSet) # Wire up our API using automatic URL routing. # Additionally, we include login URLs for the browsable API. urlpatterns = [ path('', include(router.urls)), + path('logs/', views.LogViewSet.as_view()), + path('lowest_stock_product/', views.LeastStockProductViewSet.as_view()) ] diff --git a/ivy/products/views.py b/ivy/products/views.py index d4af15c..2a07cbf 100644 --- a/ivy/products/views.py +++ b/ivy/products/views.py @@ -1,18 +1,17 @@ from rest_framework.permissions import IsAuthenticated -from rest_framework import viewsets +from rest_framework import viewsets, generics from .serializers import ProductSerializer, LogSerializer from .models import Product class ProductViewSet(viewsets.ModelViewSet): - permission_classes = [IsAuthenticated] + # permission_classes = [IsAuthenticated] serializer_class = ProductSerializer queryset = Product.objects.all().order_by('-date_added') -class LeastStockProductViewSet(viewsets.ModelViewSet): - permission_classes = [IsAuthenticated] - http_method_names = ['get'] +class LeastStockProductViewSet(generics.ListAPIView): + # permission_classes = [IsAuthenticated] serializer_class = ProductSerializer queryset = Product.objects.all().order_by('quantity') @@ -20,8 +19,7 @@ class LeastStockProductViewSet(viewsets.ModelViewSet): return super().get_queryset()[:1] -class LogViewSet(viewsets.ModelViewSet): - permission_classes = [IsAuthenticated] - http_method_names = ['get'] +class LogViewSet(generics.ListAPIView): + # permission_classes = [IsAuthenticated] serializer_class = LogSerializer queryset = Product.history.all().order_by('-history_date')