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

Fixed bugs and improvements in events. #173

Merged
merged 3 commits into from
Oct 2, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ class CommunityPartnerSerializer(serializers.Serializer):

class CommunityPartnerAndSponsorSerializer(serializers.Serializer):
sponsors = SponsorSerializer(many=True, read_only=True, source='community_sponsors')
partners = CommunityPartnerSerializer(many=True, read_only=True)
partners = CommunityPartnerSerializer(many=True, read_only=True, source='community_partners')
61 changes: 44 additions & 17 deletions backend/djangoindia/api/views/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from djangoindia.constants import POST, PRIMARY_KEY_SHORT
from django.db.models import Prefetch

from django.utils import timezone

# Create your views here.
class EventAPIView(
Expand Down Expand Up @@ -56,24 +56,51 @@ def get(self, request, *args, **kwargs):

def post(self, request, *args, **kwargs):
try:
if EventRegistration.objects.filter(
email=request.data.get("email"), event=request.data.get("event")
).exists():
return Response(
{"message": "We get it, you’re excited. But you've already secured your ticket!"},
status=status.HTTP_409_CONFLICT,
)
self.create(request, *args, **kwargs)

# send email after registration
recipient_email = request.data.get("email")
event_id = request.data.get("event")
registration_confirmation_email_task.delay(recipient_email, event_id)
email = request.data.get("email")

event = self.get_event(event_id)
self.validate_event_registration(event)
self.check_existing_registration(email, event_id)

self.create(request, *args, **kwargs)
self.send_confirmation_email(email, event_id)

return Response(
{"message": "You're in! We've sent a shiny email to your inbox. Time to celebrate!"},
status=status.HTTP_201_CREATED,
status=status.HTTP_201_CREATED
)
except ValidationError as e:
return Response({"message": str(e)}, status=status.HTTP_400_BAD_REQUEST)
except ConflictError as e:
return Response({"message": str(e)}, status=status.HTTP_409_CONFLICT)
except Exception as e:
return Response(
{"message": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR
)
return Response({"message": "An unexpected error occurred."}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

def get_event(self, event_id):
try:
return Event.objects.get(id=event_id)
except Event.DoesNotExist:
raise ValidationError("Event not found.")

def validate_event_registration(self, event):
if event.registration_end_date <= timezone.now():
raise ValidationError("Registration has already ended for this event.")
if event.seats_left is None:
raise ValidationError("Registration hasn't started yet.")
if event.seats_left < 1:
raise ValidationError("Unfortunately, there are no more seats left for the event.")


def check_existing_registration(self, email, event_id):
if EventRegistration.objects.filter(email=email, event=event_id).exists():
raise ConflictError("We get it, you're excited. But you've already secured your ticket!")

def send_confirmation_email(self, email, event_id):
registration_confirmation_email_task.delay(email, event_id)

class ValidationError(Exception):
pass

class ConflictError(Exception):
pass
7 changes: 1 addition & 6 deletions backend/djangoindia/api/views/partner_and_sponsor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,4 @@ def get_queryset(self):
return {
'community_partners': partners_queryset,
'community_sponsors': sponsors_queryset
}

def get(self, request):
queryset = self.get_queryset()
serializer = CommunityPartnerAndSponsorSerializer(queryset)
return Response(serializer.data)
}
2 changes: 1 addition & 1 deletion backend/djangoindia/db/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def send_email_to_selected_users(modeladmin, request, queryset):

class SponsorInline(admin.TabularInline):
model = Sponsorship
extra = 1
extra = 1

@admin.register(Event)
class EventAdmin(admin.ModelAdmin):
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/containers/Event/Event.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ const EventContainer = async ({
<h2 className='text-6xl font-bold'>{name}</h2>
{start_date?(<span>Starts {dayjs(start_date).format('DD MMMM, YYYY')} at {dayjs(start_date).format('hh:mm A')}</span>):(<span>Starts: TBA</span>)}
{city && <span>City: {city}</span>}
{seats_left && <span>Seats left: {seats_left}</span>}
<RegisterEvent />
{seats_left != null&& <span>Seats left: {seats_left}</span>}
<RegisterEvent seats_left={seats_left} registration_end_date={registration_end_date} />
<div className='my-12 text-l flex flex-col gap-3'>
<span className='flex items-center gap-2'>
Hey Everyone <MdWavingHand className='text-amber-500' />
Expand All @@ -92,7 +92,7 @@ const EventContainer = async ({
<span className='flex items-center gap-2'>
<CiClock1 />
{dayjs(start_date).format('hh:mm A')}
{end_date && ` - ${dayjs(end_date).format('DD MMMM, YYYY')}`}
{end_date && ` - ${dayjs(end_date).format('hh:mm A')}`}
</span>
{/* TODO: Use text variant of button */}
<Button className='w-fit' asChild>
Expand Down Expand Up @@ -137,7 +137,7 @@ const EventContainer = async ({
<CiLocationOn />
{splitAndCapitalize(event_mode)}
</span>
<RegisterEvent eventId={id} />
<RegisterEvent eventId={id} seats_left={seats_left} registration_end_date={registration_end_date} />
</div>
</div>
)
Expand Down
18 changes: 13 additions & 5 deletions frontend/src/containers/RegisterEvent/RegisterEvent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import React, { useState } from 'react'
import { yupResolver } from '@hookform/resolvers/yup'
import dayjs from 'dayjs';

import {
Drawer,
Expand Down Expand Up @@ -33,7 +34,7 @@ import { REGISTER_FORM_FIELDS } from './RegisterEvent.config'
import { fetchData } from '@/utils'
import { enqueueSnackbar } from 'notistack'

export const RegisterEvent = ({ eventId }: { eventId: string }) => {
export const RegisterEvent = ({ eventId, seats_left, registration_end_date }: { eventId: string, seats_left: number, registration_end_date: string }) => {
const [isOpen, setIsOpen] = useState(false)

const {
Expand Down Expand Up @@ -76,14 +77,21 @@ export const RegisterEvent = ({ eventId }: { eventId: string }) => {
reset()
setIsOpen(false)
}


const currentDate = dayjs();

return (
<Drawer open={isOpen} onOpenChange={setIsOpen}>
<DrawerTrigger asChild>
<Button className='w-fit bg-blue-900 z-50' onClick={() => setIsOpen(true)}>
Register
</Button>
{seats_left !== 0 && seats_left != null && currentDate.isBefore(dayjs(registration_end_date)) ? (
<Button className="w-fit bg-blue-900 z-50" onClick={() => setIsOpen(true)}>
Register
</Button>
) : ( seats_left != null &&
<Button className="w-fit bg-blue-900 z-50" disabled>
{seats_left < 1 ? 'Housefull !' : 'Registration closed'}
</Button>
)}
</DrawerTrigger>
<DrawerContent className="bg-orange-50 bg-[url('/sprinkle.svg')] bg-cover h-full pb-8 z-50">
<div className="overflow-auto no-scrollbar">
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/sections/EventSection/EventCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface EventProps {
venue: string
time: string
event_mode: string
seats_left: BigInteger
seats_left: number
}

const EventCard: React.FC<EventProps> = ({
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/types/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type Event = {
end_date: string
registration_end_date: string
event_mode: string
seats_left: BigInteger
seats_left: number
}

export type EventsResponse = Event[]