diff --git a/backend/vehicle/migrations/0002_rename_hydraulic_cylindar_condition_equipment_hydraulic_cylinder_condition.py b/backend/vehicle/migrations/0002_rename_hydraulic_cylindar_condition_equipment_hydraulic_cylinder_condition.py new file mode 100644 index 0000000..ae69cf1 --- /dev/null +++ b/backend/vehicle/migrations/0002_rename_hydraulic_cylindar_condition_equipment_hydraulic_cylinder_condition.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.6 on 2023-11-17 23:19 + +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ("vehicle", "0001_initial"), + ] + + operations = [ + migrations.RenameField( + model_name="equipment", + old_name="hydraulic_cylindar_condition", + new_name="hydraulic_cylinder_condition", + ), + ] diff --git a/backend/vehicle/models.py b/backend/vehicle/models.py index 0e2c63f..959071c 100644 --- a/backend/vehicle/models.py +++ b/backend/vehicle/models.py @@ -49,7 +49,7 @@ class Equipment(MainModel): differentials_condition = models.CharField(max_length=100, blank=True, null=True) brake_condition = models.CharField(max_length=100, blank=True, null=True) electrical_condition = models.CharField(max_length=100, blank=True, null=True) - hydraulic_cylindar_condition = models.CharField( + hydraulic_cylinder_condition = models.CharField( max_length=100, blank=True, null=True ) hydraulic_hoses_and_chrome_condition = models.CharField( diff --git a/backend/vehicle/serializers.py b/backend/vehicle/serializers.py index 5362f77..5d334dd 100644 --- a/backend/vehicle/serializers.py +++ b/backend/vehicle/serializers.py @@ -1,9 +1,93 @@ from rest_framework import serializers -from .models import Vehicle +from .models import ( + Brand, Equipment, Supplier, Trailer, Type, UnitImage, Vehicle) + + +class BrandSerializer(serializers.ModelSerializer): + class Meta: + model = Brand + fields = ["id", "name"] + + +class TypeSerializer(serializers.ModelSerializer): + class Meta: + model = Type + fields = ["id", "name"] + + +class EquipmentSerializer(serializers.ModelSerializer): + class Meta: + model = Equipment + fields = [ + "id", + "unicode_id", + "prefix_id", + "chassis_number", + "engine_number", + "description", + "brand", + "equipment_type", + "location", + "classification_type", + "engine_condition", + "transmission_condition", + "differentials_condition", + "brake_condition", + "electrical_condition", + "hydraulic_cylindar_condition", + "hydraulic_hoses_and_chrome_condition", + "chassis_condition", + "body_condition", + ] class VehicleSerializer(serializers.ModelSerializer): class Meta: model = Vehicle - fields = '__all__' + fields = [ + "id", + "unicode_id", + "model_number", + "chassis_number", + "description", + "brand", + "vehicle_type", + "minimum_price", + "is_sold", + "remarks", + "classification_type", + "engine_condition", + "transmission_condition", + "differentials_condition", + "brake_condition", + "electrical_condition", + "operating_system_condition", + "chassis_condition", + "body_condition", + ] + + +class SupplierSerializer(serializers.ModelSerializer): + class Meta: + model = Supplier + fields = ["id", "name"] + + +class TrailerSerializer(serializers.ModelSerializer): + class Meta: + model = Trailer + fields = [ + "id," "unicode_id", + "chassis_number", + "description", + "supplier", + "trailer_type", + "number_of_axles", + ] + + +class UnitImageSerializer(serializers.ModelSerializer): + class Meta: + model = UnitImage + fields = ["id", "image_url", "content_type", "object_id", "content_object"] diff --git a/backend/vehicle/urls.py b/backend/vehicle/urls.py index 2858889..cd476ca 100644 --- a/backend/vehicle/urls.py +++ b/backend/vehicle/urls.py @@ -1,8 +1,9 @@ from django.contrib import admin from django.urls import include, path -from vehicle import views +from .views import VehicleDetailApiView, VehicleListApiView urlpatterns = [ - path("", views.VehicleAPIView.as_view(), name="vehicle"), + path("", VehicleListApiView.as_view(), name="vehicle"), + path("/", VehicleDetailApiView.as_view(), name="vehicle_detail"), ] diff --git a/backend/vehicle/views.py b/backend/vehicle/views.py index 2505ada..ab62079 100644 --- a/backend/vehicle/views.py +++ b/backend/vehicle/views.py @@ -1,34 +1,78 @@ -from rest_framework import status, generics +from datetime import datetime + +from rest_framework import generics, status from rest_framework.response import Response from rest_framework.views import APIView +from rest_framework.generics import get_object_or_404 + +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): + def get(self, request, *args, **kwargs): + """ + Get all vehicles + """ + vehicles = Vehicle.objects.all() + serializer = VehicleSerializer(vehicles, many=True) + return Response(serializer.data, status=status.HTTP_200_OK) -from .models import Vehicle, Brand, Type -from .serializers import VehicleSerializer + 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) -class VehicleAPIView(APIView): +class VehicleDetailApiView(APIView): serializer_class = VehicleSerializer + """ + Retrieve, update or delete a vehicle instance. + """ - def get(self, request, *args, **kwargs): + def get(self, request, vehicle_id, *args, **kwargs): """ Get specific vehicle """ - identifier = request.data.get("id") - vehicle = Vehicle.objects.get(id=identifier) - serialized_data = self.serializer_class(vehicle) try: - return Response(serialized_data.data, status=status.HTTP_200_OK) + vehicle = get_object_or_404(Vehicle, id=vehicle_id) + serializer = VehicleSerializer(vehicle) + return Response(serializer.data, status=status.HTTP_200_OK) + # identifier = request.data.get("id") + # vehicle = Vehicle.objects.get(id=identifier) + # serialized_data = self.serializer_class(vehicle) + # return Response(serialized_data.data, status=status.HTTP_200_OK) except vehicle.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) - - def post(self, request, *args, **kwargs): - data = request.data - brand_id = data.get("brand") - type_id = data.get("type") - data.pop("brand") - data.pop("type") - brand = Brand.objects.get(id=brand_id) - type = Type.objects.get(id=type_id) - vehicle = Vehicle.objects.create(**data, brand=brand, vehicle_type=type) - serialized_data = self.serializer_class(vehicle) - return Response(serialized_data.data, status=status.HTTP_200_OK)