From 34d60bf735929fe3d4a1ee33c1814ff676867ef1 Mon Sep 17 00:00:00 2001 From: rizwanisready Date: Sun, 10 Mar 2024 17:17:08 +0530 Subject: [PATCH] chat --- chat/api/urls.py | 4 +-- chat/api/views.py | 8 ++--- chat/consumers.py | 2 +- chat/models.py | 2 +- chat/routing.py | 5 +-- goodtimes/asgi.py | 14 ++++++-- goodtimes/services.py | 32 +++++++++++-------- goodtimes/settings/base.py | 2 +- goodtimes/urls.py | 4 +-- manage_events/admin.py | 1 + manage_events/api/views.py | 32 +++++++++++++------ .../layout/base_authentication_template.html | 2 +- 12 files changed, 67 insertions(+), 41 deletions(-) diff --git a/chat/api/urls.py b/chat/api/urls.py index 89f3d0d..c791cfa 100644 --- a/chat/api/urls.py +++ b/chat/api/urls.py @@ -6,10 +6,10 @@ app_name = "chat_api" urlpatterns = [ path("/", views.EnterRoomApi.as_view(), name="enter_room"), path( - "chat_group//", views.ChatGroupAPIView.as_view(), name="chat_group" + "chat_group//", views.ChatGroupAPIView.as_view(), name="chat_group" ), path( - "chat_messages//", + "chat_messages//", views.ChatMessageAPIView.as_view(), name="chat_messages", ), diff --git a/chat/api/views.py b/chat/api/views.py index f779f3c..bc82b38 100644 --- a/chat/api/views.py +++ b/chat/api/views.py @@ -28,8 +28,8 @@ class ChatGroupAPIView(APIView): authentication_classes = [JWTAuthentication] permission_classes = [IsAuthenticated] - def get(self, request, game_id): - group_name = f"game_{game_id}" + def get(self, request, event_id): + group_name = f"event_{event_id}" chat_group, created = models.ChatGroup.objects.get_or_create(name=group_name) serializer = serializers.ChatGroupSerializer(chat_group) @@ -52,8 +52,8 @@ class ChatMessageAPIView(generics.ListAPIView): pagination_class = CustomPagination def get_queryset(self): - game_name = f"game_{self.kwargs['game_name']}" - return models.ChatMessage.objects.filter(group__name=game_name).order_by( + event_name = f"event_{self.kwargs['event_name']}" + return models.ChatMessage.objects.filter(group__name=event_name).order_by( "-timestamp" ) diff --git a/chat/consumers.py b/chat/consumers.py index 9bdbf11..9bf2a3f 100644 --- a/chat/consumers.py +++ b/chat/consumers.py @@ -22,7 +22,7 @@ class ChatConsumer(AsyncWebsocketConsumer): self.scope.get("url_route", {}).get("kwargs", {}).get("room_name") ) print("self.room_name: ", self.room_name) - token_key = self.scope["url_route"]["kwargs"]["user"] + token_key = self.scope["url_route"]["kwargs"]["token"] print("token_key: ", token_key) self.user = await self.get_user_async(token_key) print("self.user: ", self.user) diff --git a/chat/models.py b/chat/models.py index 297f002..7507873 100644 --- a/chat/models.py +++ b/chat/models.py @@ -28,4 +28,4 @@ class ChatMessage(models.Model): ) def __str__(self): - return self.group + return f"{self.user} - {self.message}" diff --git a/chat/routing.py b/chat/routing.py index a565b78..e87a539 100644 --- a/chat/routing.py +++ b/chat/routing.py @@ -1,8 +1,9 @@ -from django.urls import re_path, path +from django.urls import path, re_path from . import consumers websocket_urlpatterns = [ - path("ws/chat//", consumers.ChatConsumer.as_asgi()), + # path("ws/chat//", consumers.ChatConsumer.as_asgi()), + re_path(r'^ws/chat/(?P\w+)/(?P[^/]+)/$', consumers.ChatConsumer.as_asgi()), # path("ws/chat/", consumers.ChatConsumer.as_asgi()), ] diff --git a/goodtimes/asgi.py b/goodtimes/asgi.py index 26374ad..eccfdf2 100644 --- a/goodtimes/asgi.py +++ b/goodtimes/asgi.py @@ -8,9 +8,17 @@ https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/ """ import os - +from django.urls import path from django.core.asgi import get_asgi_application +from channels.routing import ProtocolTypeRouter, URLRouter +from chat.routing import websocket_urlpatterns + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "goodtimes.settings") - -application = get_asgi_application() +django_asgi_app = get_asgi_application() +application = ProtocolTypeRouter( + { + "http": django_asgi_app, + "websocket": URLRouter(websocket_urlpatterns), + } +) diff --git a/goodtimes/services.py b/goodtimes/services.py index 2626640..9baa415 100644 --- a/goodtimes/services.py +++ b/goodtimes/services.py @@ -387,24 +387,28 @@ class InteractionCalculator: class EventFilterService: @staticmethod - def filter_events_by_search(query): + def filter_events_by_search(search_query=None): today = timezone.now().date() - - # Basic search filtering on title and key_guest, excluding deleted and draft events - events = Event.objects.filter( - Q(title__icontains=query) | Q(key_guest__icontains=query), - deleted=False, # Assuming there's a `deleted` field to check if an event is deleted - active=True, # Assuming there's an `active` field to check if an event is active - draft=False, # Assuming there's a `draft` field to filter out drafts + filtered_events = Event.objects.filter( + key_guest__isnull=False, + deleted=False, + active=True, + draft=False ) - # Further filtering to ensure only current and future events are considered - current_and_future_events_query = Q(active=True, deleted=False) & ( - Q(start_date__lte=today, end_date__gte=today) | Q(start_date__gt=today) - ) - events = events.filter(current_and_future_events_query).distinct() + # Optional search filtering on title and key_guest (modify as needed) + if search_query: + filtered_events = filtered_events.filter( + Q(title__icontains=search_query) | Q(key_guest__icontains=search_query) + ) - return events + # Filter for current, future, or ongoing events + current_and_future_events_query = Q( + start_date__lte=today, end_date__gte=today + ) | Q(start_date__gt=today) + filtered_events = filtered_events.filter(current_and_future_events_query).distinct() + + return filtered_events @staticmethod def filter_events(filter_type, principal=None): diff --git a/goodtimes/settings/base.py b/goodtimes/settings/base.py index f250c43..38e3888 100644 --- a/goodtimes/settings/base.py +++ b/goodtimes/settings/base.py @@ -306,7 +306,7 @@ CHANNEL_LAYERS = { "default": { "BACKEND": "channels_redis.core.RedisChannelLayer", "CONFIG": { - # "hosts": [("192.168.29.219", 6379)], + # "hosts": [("192.168.0.101", 6379)], "hosts": [("127.0.0.1", 6379)], }, }, diff --git a/goodtimes/urls.py b/goodtimes/urls.py index f1fa376..50e16a9 100644 --- a/goodtimes/urls.py +++ b/goodtimes/urls.py @@ -47,8 +47,8 @@ urlpatterns = [ path("communications/", include("manage_communications.urls")), path('api/communications/', include("manage_communications.api.urls")), - # path('chat/', include('chat.urls')), - # path('api/chat/', include("chat.api.urls")), + path('chat/', include('chat.urls')), + path('api/chat/', include("chat.api.urls")), path("subscriptions/", include("manage_subscriptions.urls")), path("api/subscriptions/", include("manage_subscriptions.api.urls")), diff --git a/manage_events/admin.py b/manage_events/admin.py index 830d3fe..c0328b5 100644 --- a/manage_events/admin.py +++ b/manage_events/admin.py @@ -23,6 +23,7 @@ class EventMasterAdmin(admin.ModelAdmin): class EventAdmin(admin.ModelAdmin): list_display = ( + "id", "title", "category", "event_master", diff --git a/manage_events/api/views.py b/manage_events/api/views.py index e9fcff7..79f074d 100644 --- a/manage_events/api/views.py +++ b/manage_events/api/views.py @@ -9,6 +9,7 @@ from accounts.models import IAmPrincipalLocation from accounts.permission import IsOwnerOrReadOnly from goodtimes import constants from django.db.models import Q +from django.utils.dateparse import parse_date from goodtimes import services from goodtimes.utils import ApiResponse, CapacityError from rest_framework.permissions import IsAuthenticated @@ -115,7 +116,7 @@ class EventsAPIView(APIView): "today", "tomorrow", "category", - "search", + "key_guest", ] if filter not in params: return ApiResponse.error( @@ -129,8 +130,10 @@ class EventsAPIView(APIView): events = services.EventFilterService.filter_events_for_today() elif filter == "tomorrow": events = services.EventFilterService.filter_events_for_tomorrow() - elif filter == "search": - events = services.EventFilterService.filter_events_by_search(query) + elif filter == "key_guest": + events = services.EventFilterService.filter_events_by_search( + search_query=query + ) elif filter == "category" and category_id is not None: events = services.EventFilterService.filter_events_by_category( int(category_id) @@ -291,7 +294,9 @@ class VenueDetailView(generics.RetrieveUpdateDestroyAPIView): def get_queryset(self): # This ensures a user can only access their own venues - return self.queryset.filter(created_by=self.request.user, deleted=False, active=True) + return self.queryset.filter( + created_by=self.request.user, deleted=False, active=True + ) class GeocodeAPIView(APIView): @@ -620,17 +625,24 @@ class EventDateRangeAPIView(APIView): permission_classes = [IsAuthenticated] def get(self, request, *args, **kwargs): - serializer = EventDateRangeSerializer(data=request.data) - if not serializer.is_valid(): + start_date_str = request.query_params.get("start_date") + end_date_str = request.query_params.get("end_date") + + # Attempt to parse the dates from the query parameters + start_date = parse_date(start_date_str) + end_date = parse_date(end_date_str) + + # Validate the parsed dates + if not start_date or not end_date: return ApiResponse.error( - errors=serializer.errors, + errors={ + "start_date": "Invalid or missing", + "end_date": "Invalid or missing", + }, message=constants.FAILURE, status=status.HTTP_400_BAD_REQUEST, ) - start_date = serializer.validated_data["start_date"] - end_date = serializer.validated_data["end_date"] - events = Event.objects.filter( Q(end_date__gte=start_date) & Q(start_date__lte=end_date) diff --git a/templates/layout/base_authentication_template.html b/templates/layout/base_authentication_template.html index d2e36b2..69a4199 100644 --- a/templates/layout/base_authentication_template.html +++ b/templates/layout/base_authentication_template.html @@ -4,7 +4,7 @@ - Nifty11 + Good Times Ltd {% load static %}