From e309e3aea81a0db79de0cc9000723730d961a0d7 Mon Sep 17 00:00:00 2001 From: Lance Tan Date: Sat, 21 Oct 2023 21:58:19 -0700 Subject: [PATCH 1/2] Add endpoint for creating auction --- backend/Pipfile | 1 - backend/auction/admin.py | 11 ++++ ...uction_identifier_auction_name_and_more.py | 50 +++++++++++++++ backend/auction/models.py | 16 +++-- backend/auction/serializers.py | 9 +++ backend/auction/urls.py | 8 +++ backend/auction/views.py | 63 ++++++++++++++++++- ...0002_remove_bid_identifier_alter_bid_id.py | 28 +++++++++ backend/core/models.py | 8 ++- backend/core/urls.py | 5 +- backend/tox.ini | 2 +- ...fier_remove_vehicle_identifier_and_more.py | 58 +++++++++++++++++ 12 files changed, 247 insertions(+), 12 deletions(-) create mode 100644 backend/auction/migrations/0002_remove_auction_identifier_auction_name_and_more.py create mode 100644 backend/auction/serializers.py create mode 100644 backend/auction/urls.py create mode 100644 backend/bid/migrations/0002_remove_bid_identifier_alter_bid_id.py create mode 100644 backend/vehicle/migrations/0002_remove_brand_identifier_remove_vehicle_identifier_and_more.py diff --git a/backend/Pipfile b/backend/Pipfile index 09551d9..40ed5c2 100644 --- a/backend/Pipfile +++ b/backend/Pipfile @@ -21,4 +21,3 @@ python_version = "3.10" test = "python manage.py test" start = "python manage.py runserver" migrate = "python manage.py migrate" -makemigrations = "python manage.py makemigrations" diff --git a/backend/auction/admin.py b/backend/auction/admin.py index 8c38f3f..e34bda8 100644 --- a/backend/auction/admin.py +++ b/backend/auction/admin.py @@ -1,3 +1,14 @@ from django.contrib import admin +from auction.models import Auction +from bid.models import Bid +from user.models import User +from vehicle.models import Brand, Vehicle, VehicleType + # Register your models here. +admin.site.register(Auction) +admin.site.register(Vehicle) +admin.site.register(Brand) +admin.site.register(VehicleType) +admin.site.register(Bid) +admin.site.register(User) diff --git a/backend/auction/migrations/0002_remove_auction_identifier_auction_name_and_more.py b/backend/auction/migrations/0002_remove_auction_identifier_auction_name_and_more.py new file mode 100644 index 0000000..347c802 --- /dev/null +++ b/backend/auction/migrations/0002_remove_auction_identifier_auction_name_and_more.py @@ -0,0 +1,50 @@ +# Generated by Django 4.2.6 on 2023-10-22 02:26 + +import datetime +from django.db import migrations, models +import uuid + + +class Migration(migrations.Migration): + dependencies = [ + ("auction", "0001_initial"), + ] + + operations = [ + migrations.RemoveField( + model_name="auction", + name="identifier", + ), + migrations.AddField( + model_name="auction", + name="name", + field=models.CharField(default="Auction", max_length=150), + preserve_default=False, + ), + migrations.AddField( + model_name="auction", + name="start_date", + field=models.DateTimeField( + default=datetime.datetime( + 2023, 10, 22, 2, 26, 16, 722675, tzinfo=datetime.timezone.utc + ) + ), + preserve_default=False, + ), + migrations.AlterField( + model_name="auction", + name="end_date", + field=models.DateTimeField(), + ), + migrations.AlterField( + model_name="auction", + name="id", + field=models.UUIDField( + default=uuid.uuid4, + editable=False, + primary_key=True, + serialize=False, + unique=True, + ), + ), + ] diff --git a/backend/auction/models.py b/backend/auction/models.py index 3ee02d6..ee02878 100644 --- a/backend/auction/models.py +++ b/backend/auction/models.py @@ -1,10 +1,18 @@ from django.db import models from core.models import MainModel -from user.models import User -from vehicle.models import Vehicle class Auction(MainModel): - # start date is given by MainMode's created_at date - end_date = models.DateTimeField(editable=False) + """ + Auction that bidders can place bid on vehicles + """ + + name = models.CharField(max_length=150) + start_date = models.DateTimeField(null=False) + end_date = models.DateTimeField(null=False) + + def __str__(self): + return ( + self.name if self.name is None else "Auction at {start_date} to {end_date}" + ) diff --git a/backend/auction/serializers.py b/backend/auction/serializers.py new file mode 100644 index 0000000..c1e2c92 --- /dev/null +++ b/backend/auction/serializers.py @@ -0,0 +1,9 @@ +from rest_framework import serializers + +from .models import Auction + + +class AuctionSerializer(serializers.ModelSerializer): + class Meta: + model = Auction + fields = ["id", "name", "start_date", "end_date"] diff --git a/backend/auction/urls.py b/backend/auction/urls.py new file mode 100644 index 0000000..1b22cf2 --- /dev/null +++ b/backend/auction/urls.py @@ -0,0 +1,8 @@ +from django.contrib import admin +from django.urls import include, path + +from .views import AuctionListApiView + +urlpatterns = [ + path("", AuctionListApiView.as_view(), name="auction"), +] diff --git a/backend/auction/views.py b/backend/auction/views.py index 91ea44a..ec6ea04 100644 --- a/backend/auction/views.py +++ b/backend/auction/views.py @@ -1,3 +1,64 @@ -from django.shortcuts import render +from datetime import datetime + +from rest_framework import status +from rest_framework.response import Response +from rest_framework.views import APIView + +from .models import Auction +from .serializers import AuctionSerializer + # Create your views here. +class AuctionListApiView(APIView): + def get(self, request, *args, **kwargs): + """ + Get all auctions + """ + auctions = Auction.objects.all() + serializer = AuctionSerializer(auctions, many=True) + return Response(serializer.data, status=status.HTTP_200_OK) + + def post(self, request, *args, **kwargs): + """ + Create the Auction with given auction data + """ + date_format = "%Y-%m-%d" + + # Check if start_date and end_date are provided + if ( + request.data.get("start_date") is None + or request.data.get("end_date") is None + ): + return Response( + {"error": "Start date and end date are required"}, + status=status.HTTP_400_BAD_REQUEST, + ) + + start_date = datetime.strptime(request.data.get("start_date"), date_format) + end_date = datetime.strptime(request.data.get("end_date"), date_format) + + # Check if start date is in the past + if start_date < datetime.now().date(): + return Response( + {"error": "Start date should be in the future"}, + status=status.HTTP_400_BAD_REQUEST, + ) + + # Check if end date is before start date + if end_date < start_date: + return Response( + {"error": "End date should be after start date"}, + status=status.HTTP_400_BAD_REQUEST, + ) + + data = { + "name": request.data.get("name"), + "start_date": start_date, + "end_date": end_date, + } + serializer = AuctionSerializer(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) diff --git a/backend/bid/migrations/0002_remove_bid_identifier_alter_bid_id.py b/backend/bid/migrations/0002_remove_bid_identifier_alter_bid_id.py new file mode 100644 index 0000000..6730f1d --- /dev/null +++ b/backend/bid/migrations/0002_remove_bid_identifier_alter_bid_id.py @@ -0,0 +1,28 @@ +# Generated by Django 4.2.6 on 2023-10-22 02:26 + +from django.db import migrations, models +import uuid + + +class Migration(migrations.Migration): + dependencies = [ + ("bid", "0001_initial"), + ] + + operations = [ + migrations.RemoveField( + model_name="bid", + name="identifier", + ), + migrations.AlterField( + model_name="bid", + name="id", + field=models.UUIDField( + default=uuid.uuid4, + editable=False, + primary_key=True, + serialize=False, + unique=True, + ), + ), + ] diff --git a/backend/core/models.py b/backend/core/models.py index bafb1c8..2f5231b 100644 --- a/backend/core/models.py +++ b/backend/core/models.py @@ -10,9 +10,11 @@ class MainModel(models.Model): that every model should have. All models should extend this. """ - identifier = models.UUIDField(unique=True, default=uuid.uuid4, editable=False) - created_at = models.DateTimeField(editable=False) - updated_at = models.DateTimeField() + id = models.UUIDField( + unique=True, default=uuid.uuid4, editable=False, primary_key=True + ) + created_at = models.DateTimeField(editable=False, auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) class Meta: abstract = True diff --git a/backend/core/urls.py b/backend/core/urls.py index 86ddbdd..1337ff4 100644 --- a/backend/core/urls.py +++ b/backend/core/urls.py @@ -15,8 +15,9 @@ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import include, path urlpatterns = [ - path("admin/", admin.site.urls), + path("admin", admin.site.urls), + path("api/v1/auction", include("auction.urls")), ] diff --git a/backend/tox.ini b/backend/tox.ini index 8e4a014..378c154 100644 --- a/backend/tox.ini +++ b/backend/tox.ini @@ -1,7 +1,7 @@ [flake8] exclude = static,assets,logs,media,tests,templates,*/migrations/*.py,urls.py,settings.py,Pipfile,Pipfile.lock max-line-length = 88 -ignore = F401 +ignore = F401 W503 [tool:black] line-length = 88 diff --git a/backend/vehicle/migrations/0002_remove_brand_identifier_remove_vehicle_identifier_and_more.py b/backend/vehicle/migrations/0002_remove_brand_identifier_remove_vehicle_identifier_and_more.py new file mode 100644 index 0000000..e0f1e8f --- /dev/null +++ b/backend/vehicle/migrations/0002_remove_brand_identifier_remove_vehicle_identifier_and_more.py @@ -0,0 +1,58 @@ +# Generated by Django 4.2.6 on 2023-10-22 02:26 + +from django.db import migrations, models +import uuid + + +class Migration(migrations.Migration): + dependencies = [ + ("vehicle", "0001_initial"), + ] + + operations = [ + migrations.RemoveField( + model_name="brand", + name="identifier", + ), + migrations.RemoveField( + model_name="vehicle", + name="identifier", + ), + migrations.RemoveField( + model_name="vehicletype", + name="identifier", + ), + migrations.AlterField( + model_name="brand", + name="id", + field=models.UUIDField( + default=uuid.uuid4, + editable=False, + primary_key=True, + serialize=False, + unique=True, + ), + ), + migrations.AlterField( + model_name="vehicle", + name="id", + field=models.UUIDField( + default=uuid.uuid4, + editable=False, + primary_key=True, + serialize=False, + unique=True, + ), + ), + migrations.AlterField( + model_name="vehicletype", + name="id", + field=models.UUIDField( + default=uuid.uuid4, + editable=False, + primary_key=True, + serialize=False, + unique=True, + ), + ), + ] From ca27972b3ece0cdd006085127b9915a15d5ba70e Mon Sep 17 00:00:00 2001 From: Lance Tan Date: Sat, 21 Oct 2023 22:05:43 -0700 Subject: [PATCH 2/2] Fix string representation --- backend/auction/models.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backend/auction/models.py b/backend/auction/models.py index ee02878..a78426f 100644 --- a/backend/auction/models.py +++ b/backend/auction/models.py @@ -14,5 +14,7 @@ class Auction(MainModel): def __str__(self): return ( - self.name if self.name is None else "Auction at {start_date} to {end_date}" + self.name + if self.name is not None + else "Auction at {start_date} to {end_date}" )