feat: add is_featured/is_top_event fields and API endpoints
- Event model: added is_featured, is_top_event BooleanFields - Migration 0007 applied to DB - EventForm: checkboxes for both new fields - EventAdmin: list_display, list_editable, list_filter for both flags - FeaturedEventsAPI: POST /api/events/featured-events/ -> is_featured=True events - TopEventsAPI: POST /api/events/top-events/ -> is_top_event=True events
This commit is contained in:
@@ -21,4 +21,6 @@ urlpatterns += [
|
||||
path('events/events-by-category/', EventsByCategoryAPI.as_view(), name='api_events_by_category'),
|
||||
path('events/events-by-month-year/', EventsByMonthYearAPI.as_view(), name='events_by_month_year'),
|
||||
path('events/events-by-date/', EventsByDateAPI.as_view(), name='events_by_date'),
|
||||
path('events/featured-events/', FeaturedEventsAPI.as_view(), name='featured_events'),
|
||||
path('events/top-events/', TopEventsAPI.as_view(), name='top_events'),
|
||||
]
|
||||
|
||||
@@ -366,4 +366,56 @@ class EventsByDateAPI(APIView):
|
||||
except Exception as e:
|
||||
return JsonResponse(
|
||||
{"status": "error", "message": str(e)},
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@method_decorator(csrf_exempt, name='dispatch')
|
||||
class FeaturedEventsAPI(APIView):
|
||||
"""Returns events where is_featured=True — used for the homepage hero carousel."""
|
||||
|
||||
def post(self, request):
|
||||
try:
|
||||
user, token, data, error_response = validate_token_and_get_user(request)
|
||||
if error_response:
|
||||
return error_response
|
||||
|
||||
events = Event.objects.filter(is_featured=True).order_by('-created_date')
|
||||
event_list = []
|
||||
for e in events:
|
||||
data_dict = model_to_dict(e)
|
||||
try:
|
||||
thumb = EventImages.objects.get(event=e.id, is_primary=True)
|
||||
data_dict['thumb_img'] = request.build_absolute_uri(thumb.event_image.url)
|
||||
except EventImages.DoesNotExist:
|
||||
data_dict['thumb_img'] = ''
|
||||
event_list.append(data_dict)
|
||||
|
||||
return JsonResponse({status: success, events: event_list})
|
||||
except Exception as e:
|
||||
return JsonResponse({status: error, message: str(e)})
|
||||
|
||||
|
||||
@method_decorator(csrf_exempt, name='dispatch')
|
||||
class TopEventsAPI(APIView):
|
||||
"""Returns events where is_top_event=True — used for the Top Events section."""
|
||||
|
||||
def post(self, request):
|
||||
try:
|
||||
user, token, data, error_response = validate_token_and_get_user(request)
|
||||
if error_response:
|
||||
return error_response
|
||||
|
||||
events = Event.objects.filter(is_top_event=True).order_by('-created_date')
|
||||
event_list = []
|
||||
for e in events:
|
||||
data_dict = model_to_dict(e)
|
||||
try:
|
||||
thumb = EventImages.objects.get(event=e.id, is_primary=True)
|
||||
data_dict['thumb_img'] = request.build_absolute_uri(thumb.event_image.url)
|
||||
except EventImages.DoesNotExist:
|
||||
data_dict['thumb_img'] = ''
|
||||
event_list.append(data_dict)
|
||||
|
||||
return JsonResponse({status: success, events: event_list})
|
||||
except Exception as e:
|
||||
return JsonResponse({status: error, message: str(e)})
|
||||
|
||||
Reference in New Issue
Block a user