my events api
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 = (
|
||||
|
||||
@@ -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"),
|
||||
]
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
<div class="col-lg-12">
|
||||
<div class="row">
|
||||
<div class="col-sm-6">
|
||||
<h3>Manage Subscriptions</h3>
|
||||
<h3>Manage Events</h3>
|
||||
</div>
|
||||
<div class="col-sm-6 text-md-end">
|
||||
<!--
|
||||
|
||||
Reference in New Issue
Block a user