Polished code

This commit is contained in:
keannu125 2023-03-29 14:09:24 +08:00
parent fa684fb195
commit 4f9d2eab6d
5 changed files with 15 additions and 21 deletions

View file

@ -1,13 +1,9 @@
from django.contrib import admin from django.contrib import admin
from django.urls import path, include from django.urls import path, include
from rest_framework import routers
from . import views from . import views
router = routers.DefaultRouter()
router.register(r'user_list', views.UserListViewSet)
urlpatterns = [ urlpatterns = [
path('', include('djoser.urls')), path('', include('djoser.urls')),
path('', include('djoser.urls.authtoken')), path('', include('djoser.urls.authtoken')),
path('', include(router.urls)), path('user_list/', views.UserListViewSet.as_view())
] ]

View file

@ -1,4 +1,4 @@
from rest_framework import viewsets from rest_framework import viewsets, generics
from .serializers import UserSerializer from .serializers import UserSerializer
from django.contrib.auth.models import User from django.contrib.auth.models import User
from rest_framework.permissions import IsAuthenticated from rest_framework.permissions import IsAuthenticated
@ -6,8 +6,7 @@ from rest_framework.permissions import IsAuthenticated
# Create your views here. # Create your views here.
class UserListViewSet(viewsets.ModelViewSet): class UserListViewSet(generics.ListAPIView):
permission_classes = [IsAuthenticated] # permission_classes = [IsAuthenticated]
http_method_names = ['get']
serializer_class = UserSerializer serializer_class = UserSerializer
queryset = User.objects.all() queryset = User.objects.all()

View file

@ -26,9 +26,10 @@ BASE_DIR = Path(__file__).resolve().parent.parent
# SECURITY WARNING: keep the secret key used in production secret! # SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('SECRET_KEY') SECRET_KEY = os.environ.get('SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production! # 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 # Application definition

View file

@ -4,11 +4,11 @@ from . import views
router = routers.DefaultRouter() router = routers.DefaultRouter()
router.register(r'products', views.ProductViewSet) 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. # Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API. # Additionally, we include login URLs for the browsable API.
urlpatterns = [ urlpatterns = [
path('', include(router.urls)), path('', include(router.urls)),
path('logs/', views.LogViewSet.as_view()),
path('lowest_stock_product/', views.LeastStockProductViewSet.as_view())
] ]

View file

@ -1,18 +1,17 @@
from rest_framework.permissions import IsAuthenticated from rest_framework.permissions import IsAuthenticated
from rest_framework import viewsets from rest_framework import viewsets, generics
from .serializers import ProductSerializer, LogSerializer from .serializers import ProductSerializer, LogSerializer
from .models import Product from .models import Product
class ProductViewSet(viewsets.ModelViewSet): class ProductViewSet(viewsets.ModelViewSet):
permission_classes = [IsAuthenticated] # permission_classes = [IsAuthenticated]
serializer_class = ProductSerializer serializer_class = ProductSerializer
queryset = Product.objects.all().order_by('-date_added') queryset = Product.objects.all().order_by('-date_added')
class LeastStockProductViewSet(viewsets.ModelViewSet): class LeastStockProductViewSet(generics.ListAPIView):
permission_classes = [IsAuthenticated] # permission_classes = [IsAuthenticated]
http_method_names = ['get']
serializer_class = ProductSerializer serializer_class = ProductSerializer
queryset = Product.objects.all().order_by('quantity') queryset = Product.objects.all().order_by('quantity')
@ -20,8 +19,7 @@ class LeastStockProductViewSet(viewsets.ModelViewSet):
return super().get_queryset()[:1] return super().get_queryset()[:1]
class LogViewSet(viewsets.ModelViewSet): class LogViewSet(generics.ListAPIView):
permission_classes = [IsAuthenticated] # permission_classes = [IsAuthenticated]
http_method_names = ['get']
serializer_class = LogSerializer serializer_class = LogSerializer
queryset = Product.history.all().order_by('-history_date') queryset = Product.history.all().order_by('-history_date')