diff --git a/backend/user/urls.py b/backend/user/urls.py index f0bcd68..1e8c1af 100644 --- a/backend/user/urls.py +++ b/backend/user/urls.py @@ -2,8 +2,13 @@ from django.urls import include, path from .views import ( - AdminDetailApiView, AdminListApiView, BidderBlacklistApiView, - BidderDetailApiView, BidderListApiView, BidderVerifyApiView) + AdminDetailApiView, + AdminListApiView, + BidderBlacklistApiView, + BidderDetailApiView, + BidderListApiView, + BidderVerifyApiView, +) urlpatterns = [ path("bidders/", BidderListApiView.as_view(), name="bidder-list"), diff --git a/backend/user/views.py b/backend/user/views.py index a71782c..f87ce90 100644 --- a/backend/user/views.py +++ b/backend/user/views.py @@ -5,8 +5,11 @@ from .models import Admin, Bidder from .serializers import ( - AdminSerializer, BidderBlacklistedSerializer, BidderSerializer, - BidderVerifiedSerializer) + AdminSerializer, + BidderBlacklistedSerializer, + BidderSerializer, + BidderVerifiedSerializer, +) class BidderListApiView(APIView): diff --git a/backend/vehicle/admin.py b/backend/vehicle/admin.py index 94fa14c..4efe9a8 100644 --- a/backend/vehicle/admin.py +++ b/backend/vehicle/admin.py @@ -1,7 +1,15 @@ from django.contrib import admin from .models import ( - Brand, Equipment, SavedUnits, Supplier, Trailer, Type, UnitImage, Vehicle) + Brand, + Equipment, + SavedUnits, + Supplier, + Trailer, + Type, + UnitImage, + Vehicle, +) class BrandAdmin(admin.ModelAdmin): diff --git a/backend/vehicle/helpers.py b/backend/vehicle/helpers.py new file mode 100644 index 0000000..ec1037b --- /dev/null +++ b/backend/vehicle/helpers.py @@ -0,0 +1,31 @@ +from .models import Brand, Equipment, Supplier, Trailer, Type, UnitImage, Vehicle + + +def infinite_filter(request): + url_parameter = request.GET.get("search") + if url_parameter: + limit = request.GET.get("l") + offset = request.GET.get("o") + if limit and offset: + return Vehicle.objects.filter(unicode_id__icontains=url_parameter)[ + : int(offset) + int(limit) + ] + elif limit: + return Vehicle.objects.filter(unicode_id__icontains=url_parameter)[ + : int(limit) + ] + else: + return Vehicle.objects.filter(unicode_id__icontains=url_parameter)[:15] + return Vehicle.objects.all()[:40] + + +def has_more_data(request): + offset = request.GET.get("o") + limit = request.GET.get("l") + if offset: + return Vehicle.objects.all().count() > (int(offset) + int(limit)) + + elif limit: + return Vehicle.objects.all().count() > int(limit) + else: + return False diff --git a/backend/vehicle/migrations/0003_savedunits.py b/backend/vehicle/migrations/0003_savedunits.py index e34a98c..2818953 100644 --- a/backend/vehicle/migrations/0003_savedunits.py +++ b/backend/vehicle/migrations/0003_savedunits.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.6 on 2023-11-25 01:48 +# Generated by Django 4.2.7 on 2023-11-26 06:03 from django.db import migrations, models import django.db.models.deletion @@ -8,8 +8,8 @@ class Migration(migrations.Migration): dependencies = [ ("user", "0003_remove_admin_password"), - ("contenttypes", "0002_remove_content_type_name"), ("auction", "0002_auction_cover_image_auction_is_published_and_more"), + ("contenttypes", "0002_remove_content_type_name"), ( "vehicle", "0002_rename_hydraulic_cylindar_condition_equipment_hydraulic_cylinder_condition", diff --git a/backend/vehicle/serializers.py b/backend/vehicle/serializers.py index 5d334dd..095a03e 100644 --- a/backend/vehicle/serializers.py +++ b/backend/vehicle/serializers.py @@ -1,7 +1,6 @@ from rest_framework import serializers -from .models import ( - Brand, Equipment, Supplier, Trailer, Type, UnitImage, Vehicle) +from .models import Brand, Equipment, Supplier, Trailer, Type, UnitImage, Vehicle class BrandSerializer(serializers.ModelSerializer): diff --git a/backend/vehicle/urls.py b/backend/vehicle/urls.py index cd476ca..492efd7 100644 --- a/backend/vehicle/urls.py +++ b/backend/vehicle/urls.py @@ -1,9 +1,10 @@ from django.contrib import admin from django.urls import include, path -from .views import VehicleDetailApiView, VehicleListApiView +from .views import VehicleDetailApiView, VehicleListApiView, VehicleFilterList urlpatterns = [ path("", VehicleListApiView.as_view(), name="vehicle"), + path("filter/", VehicleFilterList.as_view(), name="vehicle-list"), path("/", VehicleDetailApiView.as_view(), name="vehicle_detail"), ] diff --git a/backend/vehicle/views.py b/backend/vehicle/views.py index 7e0c569..815ba3a 100644 --- a/backend/vehicle/views.py +++ b/backend/vehicle/views.py @@ -1,16 +1,26 @@ from datetime import datetime -from rest_framework import generics, status +from rest_framework import generics, status, viewsets from rest_framework.generics import get_object_or_404 from rest_framework.response import Response from rest_framework.views import APIView - -from .models import Vehicle -from .serializers import VehicleSerializer +from .helpers import has_more_data, infinite_filter +from .models import Brand, Equipment, Supplier, Trailer, Type, UnitImage, Vehicle +from .serializers import ( + BrandSerializer, + EquipmentSerializer, + SupplierSerializer, + TrailerSerializer, + TypeSerializer, + UnitImageSerializer, + VehicleSerializer, +) # Create your views here. class VehicleListApiView(APIView): + serializer_class = VehicleSerializer + def get(self, request, *args, **kwargs): """ Get all vehicles @@ -23,34 +33,17 @@ def post(self, request, *args, **kwargs): """ Create the Vehicle with given vehicle data """ - data = { - "unicode_id": request.data.get("unicode_id"), - "model_number": request.data.get("model_number"), - "chassis_number": request.data.get("chassis_number"), - "description": request.data.get("description"), - "brand": request.data.get("brand"), - "vehicle_type": request.data.get("vehicle_type"), - "minimum_price": request.data.get("minimum_price"), - "is_sold": request.data.get("is_sold"), - "remarks": request.data.get("remarks"), - "classification_type": request.data.get("classification_type"), - "engine_condition": request.data.get("engine_condition"), - "transmission_condition": request.data.get("tansmission_condition"), - "differentials_condition": request.data.get("differentials_condition"), - "brake_condition": request.data.get("brake_condition"), - "electrical_condition": request.data.get("electrical_condition"), - "operating_system_condition": request.data.get( - "operating_system_condition" - ), - "chassis_condition": request.data.get("chassis_condition"), - "body_condition": request.data.get("body_condition"), - } - serializer = VehicleSerializer(data=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) + data = request.data.copy() + brand_id = data.pop("brand", None) + type_id = data.pop("type", None) + # Handling the possibility that brand or type IDs might not exist + brand = get_object_or_404(Brand, id=brand_id) if brand_id else None + vehicle_type = get_object_or_404(Type, id=type_id) if type_id else None + + vehicle = Vehicle.objects.create(brand=brand, vehicle_type=vehicle_type, **data) + # Use the serializer class's data directly + serialized_data = self.serializer_class(vehicle) + return Response(serialized_data.data, status=status.HTTP_201_CREATED) class VehicleDetailApiView(APIView): @@ -69,3 +62,27 @@ def get(self, request, vehicle_id, *args, **kwargs): return Response(serializer.data, status=status.HTTP_200_OK) except vehicle.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) + + +class VehicleFilterList(APIView): + """ + Get list of vehicles based off of filter + Takes limit + offset from url + """ + + def get_queryset(self): + queryset = infinite_filter(self.request) + return queryset + + def get(self, request): + url_parameter = request.GET.get("search") + if url_parameter: + vehicles = self.get_queryset() + + serialized_data = VehicleSerializer(vehicles, many=True) + + return Response( + {"vehicles": serialized_data.data, "more_data": has_more_data(request)} + ) + + return Response(VehicleSerializer(Vehicle.objects.all()[:10], many=True).data)