diff --git a/goodtimes/services.py b/goodtimes/services.py index d6849ec..2cf522f 100644 --- a/goodtimes/services.py +++ b/goodtimes/services.py @@ -386,12 +386,32 @@ class InteractionCalculator: class EventFilterService: + @staticmethod + def filter_events_by_search(query): + 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 + ) + + # 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() + + return events + @staticmethod def filter_events(filter_type, principal=None): today = timezone.now().date() events = Event.objects.none() - current_and_future_events_query = Q(active=True, deleted=False) & ( + current_and_future_events_query = Q(active=True, deleted=False, draft=False) & ( Q(start_date__lte=today, end_date__gte=today) | Q(start_date__gt=today) ) @@ -418,7 +438,7 @@ class EventFilterService: def filter_events_by_category(category_id): today = timezone.now().date() - current_and_future_events_query = Q(active=True, deleted=False) & ( + current_and_future_events_query = Q(active=True, deleted=False, draft=False) & ( Q(start_date__lte=today, end_date__gte=today) | Q(start_date__gt=today) ) @@ -446,7 +466,7 @@ class EventFilterService: | (Q(start_date__lte=tomorrow) & Q(end_date__gte=tomorrow)) ) events = Event.objects.filter( - events_query, active=True, deleted=False + events_query, active=True, deleted=False, draft=False ).distinct() return events @@ -457,8 +477,159 @@ class EventFilterService: print("Today: ", today) events = Event.objects.filter( - Q(active=True) & ~Q(deleted=True), - Q(start_date__lte=today) & Q(end_date__gte=today), + active=True, + deleted=False, + draft=False, + start_date__lte=today, + end_date__gte=today, + ) + + return events + + +# ye package ka naam hai +# onesignal-sdk==2.0.0 + + +# class OneSignalNotificationService: +# """ +# Class for sending notifications using the OneSignal API. + +# Provides a convenient way to create and send notifications to OneSignal users, +# with features like targeting specific devices or segments, customizing notification content, +# and handling errors gracefully. + +# *Parameters:* + +# - *app_id* (str): Your OneSignal App ID. +# - *rest_api_key* (str): Your OneSignal REST API Key. +# - *user_auth_key* (str): Your OneSignal User Auth Key. + +# *Keyword Arguments:* + +# This method accepts additional keyword arguments (`**kwargs`) to customize the notification +# further, including: + +# - `url` (str): URL to open when the notification is clicked. +# - `data` (dict): Custom data to be sent with the notification. +# - `buttons` (list): List of action buttons to display within the notification. +# - `send_after` (str): Timestamp for scheduling the notification. +# - `delayed_option` (dict): Option for delayed delivery (Android-specific). +# - `android_channel_id` (str): Channel ID for Android notifications. +# - `ios_sound` (str): Sound to play for iOS notifications. +# - `ios_badgeType` (str): Badge type for iOS notifications. +# - `ios_badgeCount` (int): Badge count for iOS notifications. +# - `ios_thread_id` (str): Thread ID to group notifications in iOS. +# - `android_background_layout` (str): Layout for background notifications on Android. +# - `android_group` (str): Group notification on Android. +# - `android_group_message` (str): Summary for grouped notifications on Android. +# - `android_group_summary` (str): Summary for grouped notifications on Android. +# - `android_led_color` (str): LED color for Android notifications. +# - `android_accent_color` (str): Accent color for Android notifications. +# - `android_visibility` (str): Visibility settings for Android notifications. + +# *Example usage:* + +# notification = OneSignalNotificationService() +# response = notification.send_notification( +# headings="Welcome", +# message="Thanks for signing up!", +# player_tokens=["PLAYER_TOKEN1", "PLAYER_TOKEN2"], +# url="https://yourwebsite.com/welcome", +# data={"user_id": 123}, +# ) +# """ + +# def __init__(self): +# self.config = OneSignalClient( +# app_id=settings.ONESIGNAL_APP_ID, +# rest_api_key=settings.ONESIGNAL_REST_API_KEY, +# user_auth_key=settings.ONESIGNAL_USER_AUTH_KEY, +# ) + +# # Set up logging +# self.logger = logging.getLogger(__name__) + +# def send_notification(self, headings, message, player_tokens=None, **kwargs): +# notification_obj = { +# "headings": {"en": headings}, +# "contents": {"en": message}, +# **kwargs, +# } + +# if player_tokens: +# notification_obj["include_player_ids"] = player_tokens + +# try: +# response = self.config.send_notification(notification_obj) +# self.logger.info(f"Notification send successfully : {response}") +# return response +# except Exception as e: +# self.logger.error(f"OneSignal error {e}") +# raise Exception("Generic OneSignal error: {}".format(e)) + + +class MyEventFilterService: + @staticmethod + def filter_my_events_draft(user): + today = timezone.now().date() + + current_and_future_events_query = Q( + start_date__lte=today, end_date__gte=today + ) | Q(start_date__gt=today) + + events = Event.objects.filter( + current_and_future_events_query, + draft=True, + deleted=False, + active=True, + created_by=user, + ).distinct() + + return events + + @staticmethod + def filter_my_events_active(user): + today = timezone.now().date() + + current_and_future_events_query = Q( + start_date__lte=today, end_date__gte=today + ) | Q(start_date__gt=today) + + events = Event.objects.filter( + current_and_future_events_query, + draft=False, + deleted=False, + active=True, + created_by=user, + ) + + return events + + @staticmethod + def filter_my_events_active_past(user): + today = timezone.now().date() + + events = Event.objects.filter( + end_date__lt=today, + draft=False, + deleted=False, + active=True, + created_by=user, + ) + + return events + + @staticmethod + def filter_my_events_draft_past(user): + today = timezone.now().date() + + events = Event.objects.filter( + end_date__lt=today, + draft=True, + deleted=False, + active=True, + created_by=user, ) return events diff --git a/manage_events/admin.py b/manage_events/admin.py index 60d5dbf..830d3fe 100644 --- a/manage_events/admin.py +++ b/manage_events/admin.py @@ -31,6 +31,8 @@ class EventAdmin(admin.ModelAdmin): "venue", "status", "draft", + "deleted", + "active", ) list_filter = ("category", "status", "draft", "start_date", "end_date", "venue") search_fields = ( diff --git a/manage_events/api/urls.py b/manage_events/api/urls.py index 5f1ae71..1a33787 100644 --- a/manage_events/api/urls.py +++ b/manage_events/api/urls.py @@ -89,4 +89,6 @@ urlpatterns = [ views.EventDateRangeAPIView.as_view(), name="event-date-range", ), + # My Events + path("my-events/", views.MyEventsAPIView.as_view(), name="my-events"), ] diff --git a/manage_events/api/views.py b/manage_events/api/views.py index 601aec3..4ba9cc7 100644 --- a/manage_events/api/views.py +++ b/manage_events/api/views.py @@ -106,8 +106,17 @@ class EventsAPIView(APIView): def get(self, request, *args, **kwargs): filter = request.query_params.get("filter", None) + query = request.query_params.get("query", None) category_id = request.query_params.get("category_id", None) - params = ["expensive", "cheap", "preference", "today", "tomorrow", "category"] + params = [ + "expensive", + "cheap", + "preference", + "today", + "tomorrow", + "category", + "search", + ] if filter not in params: return ApiResponse.error( status=status.HTTP_400_BAD_REQUEST, @@ -120,6 +129,8 @@ 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 == "category" and category_id is not None: events = services.EventFilterService.filter_events_by_category( int(category_id) @@ -144,6 +155,50 @@ class EventsAPIView(APIView): ) +class MyEventsAPIView(APIView): + permission_classes = [IsAuthenticated] + + def get(self, request, *args, **kwargs): + filter_type = request.query_params.get("filter", None) + user = request.user + params = [ + "draft", + "active", + "active_past", + "draft_past", + ] + if filter_type not in params: + return ApiResponse.error( + status=status.HTTP_400_BAD_REQUEST, + message=constants.FAILURE, + errors="No filter found", + ) + + if filter_type == "draft": + events = services.MyEventFilterService.filter_my_events_draft(user) + elif filter_type == "active": + events = services.MyEventFilterService.filter_my_events_active(user) + elif filter_type == "active_past": + events = services.MyEventFilterService.filter_my_events_active_past(user) + elif filter_type == "draft_past": + events = services.MyEventFilterService.filter_my_events_draft_past(user) + else: + return ApiResponse.error( + status=status.HTTP_400_BAD_REQUEST, + message=constants.FAILURE, + errors="Invalid filter parameter", + ) + + serializer = EventDetailSerializer( + events, context={"request": request}, many=True + ) + return ApiResponse.success( + status=status.HTTP_200_OK, + message=constants.SUCCESS, + data=serializer.data, + ) + + class PrinciaplPreferenceEventsAPIView(APIView): authentication_classes = [JWTAuthentication] permission_classes = [IsAuthenticated] diff --git a/manage_events/views.py b/manage_events/views.py index 19f330e..8c38b5c 100644 --- a/manage_events/views.py +++ b/manage_events/views.py @@ -291,7 +291,7 @@ class EventView(LoginRequiredMixin, generic.ListView): model = Event template_name = "manage_events/event_list.html" context_object_name = "event_obj" - paginate_by = 10 + # paginate_by = 10 def get_queryset(self): return super().get_queryset().filter(deleted=False, active=True, draft=False) diff --git a/manage_subscriptions/views.py b/manage_subscriptions/views.py index 90fb863..1935b46 100644 --- a/manage_subscriptions/views.py +++ b/manage_subscriptions/views.py @@ -107,7 +107,7 @@ class SubscriptionView(LoginRequiredMixin, generic.ListView): context_object_name = "subscription_obj" def get_queryset(self): - return super().get_queryset().filter(deleted=False) + return super().get_queryset().filter(deleted=False, active=True) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) diff --git a/templates/manage_events/event_list.html b/templates/manage_events/event_list.html index dd991eb..7ee2810 100644 --- a/templates/manage_events/event_list.html +++ b/templates/manage_events/event_list.html @@ -12,7 +12,7 @@
-

Manage Subscriptions

+

Manage Events