diff --git a/backend/vehicle/helpers.py b/backend/vehicle/helpers.py new file mode 100644 index 0000000..cb73dde --- /dev/null +++ b/backend/vehicle/helpers.py @@ -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 \ No newline at end of file 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/views.py b/backend/vehicle/views.py index e2f7b44..7b2de32 100644 --- a/backend/vehicle/views.py +++ b/backend/vehicle/views.py @@ -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 ( @@ -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) \ No newline at end of file