Skip to content

Commit

Permalink
added basic endpoint, no url yet
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinrczhang committed Nov 26, 2023
1 parent 4c0d94d commit 66d8add
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
26 changes: 26 additions & 0 deletions backend/vehicle/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from .models import (
Brand, Equipment, Supplier, Trailer, Type, UnitImage, Vehicle)

def infinite_filter(request):
url_parameter = request.GET.get('p')
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):
o = request.GET.get('o')
l = request.GET.get('l')
if o:
return Vehicle.objects.all().count() > int(o)

elif l:
return Vehicle.objects.all().count() > int(l)
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
26 changes: 24 additions & 2 deletions backend/vehicle/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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 .helpers import has_more_data, infinite_filter
from .models import (
Brand, Equipment, Supplier, Trailer, Type, UnitImage, Vehicle)
from .serializers import (
Expand Down Expand Up @@ -76,3 +76,25 @@ def get(self, request, vehicle_id, *args, **kwargs):
# return Response(serialized_data.data, status=status.HTTP_200_OK)
except vehicle.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)

class VehicleListViewset(viewsets.ModelViewSet):
"""
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 list(self, request):
url_parameter = request.GET.get('p')
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 66d8add

Please sign in to comment.