Skip to content

Commit

Permalink
changed field names
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinrczhang committed Apr 10, 2024
1 parent eb2ca30 commit 8372d01
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 83 deletions.
1 change: 1 addition & 0 deletions backend/bid/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
class BidConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "bid"

def ready(self):
import bid.signals
36 changes: 14 additions & 22 deletions backend/bid/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,33 @@
from asgiref.sync import async_to_sync
import json


class BidConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.group_name = 'bid_updates'
await self.channel_layer.group_add(
self.group_name,
self.channel_name
)
self.group_name = "bid_updates"
await self.channel_layer.group_add(self.group_name, self.channel_name)
await self.accept()

async def disconnect(self, close_code):
await self.channel_layer.group_discard(
self.group_name,
self.channel_name
)
await self.channel_layer.group_discard(self.group_name, self.channel_name)

async def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json.get("message")
message = text_data_json.get("message")
if message:
await self.send(text_data=json.dumps({"message": message}))

async def bid_update(self, event):
bid_data = event['bid_data']
bid_data = event["bid_data"]

bid_data['id'] = str(bid_data['id'])

await self.send(text_data=json.dumps({
'type': 'bid.update',
'bid_data': bid_data
}))
bid_data["id"] = str(bid_data["id"])

await self.send(
text_data=json.dumps({"type": "bid.update", "bid_data": bid_data})
)

async def bid_delete(self, event):
bid_id = event['bid_id']
await self.send(text_data=json.dumps({
'type': 'bid.delete',
'bid_id': bid_id
}))
bid_id = event["bid_id"]
await self.send(
text_data=json.dumps({"type": "bid.delete", "bid_id": str(bid_id)})
)
4 changes: 2 additions & 2 deletions backend/bid/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
from . import consumer

websocket_urlpatterns = [
path('ws/bid_updates/', consumer.BidConsumer.as_asgi()),
]
path("ws/bid_updates/", consumer.BidConsumer.as_asgi()),
]
33 changes: 19 additions & 14 deletions backend/bid/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,37 @@
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync
import json
from auction.models import AuctionDay

channel_layer = get_channel_layer()


@receiver(post_save, sender=Bid)
def bid_updated(sender, instance, **kwargs):
current_date = instance.auction.start_date.date()
auction_day = AuctionDay.objects.filter(
auction=instance.auction, date=current_date
).first()

bid_data = {
'id': str(instance.id), # Convert UUID to string
'amount': instance.amount,
'auction': str(instance.auction.id),
'bidder' : str(instance.bidder)
"id": str(instance.id),
"amount": instance.amount,
"auction_id": str(instance.auction.id),
"auction_day_id": str(auction_day.id) if auction_day else None,
"vehicle_id": str(instance.object_id),
"bidder": str(instance.bidder),
}
async_to_sync(channel_layer.group_send)(
'bid_updates',
{
'type': 'bid.update',
'bid_data': bid_data
}
"bid_updates", {"type": "bid.update", "bid_data": bid_data}
)


@receiver(post_delete, sender=Bid)
def bid_deleted(sender, instance, **kwargs):
# Notify bid deletion to consumers
async_to_sync(channel_layer.group_send)(
'bid_updates',
"bid_updates",
{
'type': 'bid.delete',
'bid_id': instance.id,
}
"type": "bid.delete",
"bid_id": instance.id,
},
)
1 change: 1 addition & 0 deletions backend/bid/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from .views import BidDetailApiView, BidListApiView
from . import consumer

urlpatterns = [
path("/", BidListApiView.as_view(), name="bid_list"),
path("/<uuid:bid_id>", BidDetailApiView.as_view(), name="bid_detail"),
Expand Down
2 changes: 1 addition & 1 deletion backend/core/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
AuthMiddlewareStack(URLRouter(bid.routing.websocket_urlpatterns))
),
}
)
)
16 changes: 6 additions & 10 deletions backend/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from pathlib import Path
from corsheaders.defaults import default_headers
from dotenv import load_dotenv

load_dotenv()
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand All @@ -36,7 +37,7 @@
# Application definition

INSTALLED_APPS = [
'daphne',
"daphne",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
Expand All @@ -51,15 +52,11 @@
"user",
"vehicle",
"bid",
'channels',
"channels",
]
ASGI_APPLICATION = 'core.asgi.application'
ASGI_APPLICATION = "core.asgi.application"

CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels.layers.InMemoryChannelLayer"
}
}
CHANNEL_LAYERS = {"default": {"BACKEND": "channels.layers.InMemoryChannelLayer"}}
# AWS S3 Configuration
AWS_ACCESS_KEY_ID = os.environ.get("AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = os.environ.get("AWS_SECRET_ACCESS_KEY")
Expand Down Expand Up @@ -94,7 +91,7 @@
]

CORS_ALLOW_HEADERS = list(default_headers) + [
'Authorization',
"Authorization",
]

CORS_ALLOW_CREDENTIALS = True
Expand Down Expand Up @@ -131,7 +128,6 @@
"PORT": os.environ.get("DB_PORT"),
}
}
print(os.environ.get("DB_PASSWORD"))

# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators
Expand Down
2 changes: 1 addition & 1 deletion backend/user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def delete(self, request, bidder_id):


class BidderVerifyApiView(APIView):
permission_classes = [AllowAny]
permission_classes = [IsAdminUser]

cognitoService = AWSCognitoService()

Expand Down
3 changes: 2 additions & 1 deletion backend/vehicle/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
# Create your views here.
class VehicleListApiView(APIView):
serializer_class = VehicleSerializer

def get_permissions(self):
if self.request.method == "POST":
self.permission_classes = [IsAdminUser]
else:
self.permission_classes = [AllowAny]
self.permission_classes = [IsAuthenticated]
return super().get_permissions()

def get(self, request, *args, **kwargs):
Expand Down
32 changes: 0 additions & 32 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8372d01

Please sign in to comment.