Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
4 changes: 3 additions & 1 deletion backend/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
from django.urls import path

from hoagiecalendar.api.event_views import EventDetailView, EventView
from hoagiecalendar.api.user_event_attending_view import UserEventAttendingView
from hoagiecalendar.api.user_events_view import UserEventsView

urlpatterns = [
path("admin/", admin.site.urls),
path("event/", EventView.as_view(), name="event"),
path("event/<int:event_id>/", EventDetailView.as_view(), name="event-detail"),
path("user/events/", UserEventsView.as_view(), name="user-events"),
path("user/events/created", UserEventsView.as_view(), name="user-events"),
path("user/events/attending", UserEventAttendingView.as_view(), name="user-events-attending"),
]
9 changes: 7 additions & 2 deletions backend/hoagiecalendar/api/event_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class EventSerializer(serializers.ModelSerializer):
},
)

attending_count = serializers.SerializerMethodField(read_only=True)

def get_attending_count(self, obj: Event) -> int:
return obj.attending_count()

class Meta:
model = Event
fields = [
Expand All @@ -50,9 +55,9 @@ class Meta:
"owner",
"category",
"from_mail",
"ordering",
"attending_count",
]
read_only_fields = ["id", "owner", "created_at", "updated_at"]
read_only_fields = ["id", "owner", "created_at", "updated_at", "attending_count"]


class EventView(APIView):
Expand Down
44 changes: 44 additions & 0 deletions backend/hoagiecalendar/api/user_event_attending_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from rest_framework import status
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView

from ..models.event import Event


class UserEventAttendingView(APIView):
def post(self, request: Request) -> Response:
event_id = request.query_params.get("event_id")

if not event_id:
return Response({"error": "invalid request: no event_id provided"}, status=status.HTTP_400_BAD_REQUEST)

try:
Event.objects.get(pk=event_id).attendees.add(request.user)
Comment thread
asze17 marked this conversation as resolved.
Outdated
except Event.DoesNotExist:
return Response({"error": "event not found"}, status=status.HTTP_400_BAD_REQUEST)
Comment thread
asze17 marked this conversation as resolved.
Outdated
except Exception as e:
return Response(
{"error": f"an internal error has occured: {e}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR
Comment thread
asze17 marked this conversation as resolved.
Outdated
)

return Response({"message": "added to event attendees (if not already added)"}, status=status.HTTP_201_CREATED)
Comment thread
asze17 marked this conversation as resolved.
Outdated

def delete(self, request: Request) -> Response:
event_id = request.query_params.get("event_id")

if not event_id:
return Response({"error": "invalid request: no event_id provided"}, status=status.HTTP_400_BAD_REQUEST)

try:
Event.objects.get(pk=event_id).attendees.remove(request.user)
Comment thread
asze17 marked this conversation as resolved.
Outdated
except Event.DoesNotExist:
return Response({"error": "event not found"}, status=status.HTTP_400_BAD_REQUEST)
Comment thread
asze17 marked this conversation as resolved.
Outdated
except Exception as e:
return Response(
{"error": f"an internal error has occured: {e}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR
Comment thread
asze17 marked this conversation as resolved.
Outdated
)

return Response(
{"message": "removed from event attendees (if user was an attendee)"}, status=status.HTTP_201_CREATED
Comment thread
asze17 marked this conversation as resolved.
Outdated
)
4 changes: 4 additions & 0 deletions backend/hoagiecalendar/models/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class Event(models.Model):
from_mail = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
attendees = models.ManyToManyField(User, related_name="events_attending", blank=True)

def attending_count(self) -> int:
return self.attendees.count()

def __str__(self) -> str:
return self.name
Expand Down
Loading