Skip to content

Commit

Permalink
Filter vehicles endpoint (#20)
Browse files Browse the repository at this point in the history
Co-authored-by: _Kevin_Zhang_ <[email protected]>
  • Loading branch information
kevinrczhang and kevinrczhang authored Nov 30, 2023
1 parent bfe4204 commit 7327bfe
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 42 deletions.
9 changes: 7 additions & 2 deletions backend/user/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
7 changes: 5 additions & 2 deletions backend/user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

from .models import Admin, Bidder
from .serializers import (
AdminSerializer, BidderBlacklistedSerializer, BidderSerializer,
BidderVerifiedSerializer)
AdminSerializer,
BidderBlacklistedSerializer,
BidderSerializer,
BidderVerifiedSerializer,
)


class BidderListApiView(APIView):
Expand Down
10 changes: 9 additions & 1 deletion backend/vehicle/admin.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
31 changes: 31 additions & 0 deletions backend/vehicle/helpers.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions backend/vehicle/migrations/0003_savedunits.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions backend/vehicle/serializers.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
3 changes: 2 additions & 1 deletion backend/vehicle/urls.py
Original file line number Diff line number Diff line change
@@ -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("<uuid:vehicle_id>/", VehicleDetailApiView.as_view(), name="vehicle_detail"),
]
81 changes: 49 additions & 32 deletions backend/vehicle/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand All @@ -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)

0 comments on commit 7327bfe

Please sign in to comment.