Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoint for creating auction #7

Merged
merged 2 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion backend/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
11 changes: 11 additions & 0 deletions backend/auction/admin.py
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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,
),
),
]
18 changes: 14 additions & 4 deletions backend/auction/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
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 not None
else "Auction at {start_date} to {end_date}"
)
9 changes: 9 additions & 0 deletions backend/auction/serializers.py
Original file line number Diff line number Diff line change
@@ -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"]
8 changes: 8 additions & 0 deletions backend/auction/urls.py
Original file line number Diff line number Diff line change
@@ -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"),
]
63 changes: 62 additions & 1 deletion backend/auction/views.py
Original file line number Diff line number Diff line change
@@ -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)
28 changes: 28 additions & 0 deletions backend/bid/migrations/0002_remove_bid_identifier_alter_bid_id.py
Original file line number Diff line number Diff line change
@@ -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,
),
),
]
8 changes: 5 additions & 3 deletions backend/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions backend/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
]
2 changes: 1 addition & 1 deletion backend/tox.ini
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
),
),
]