From 0b1d424d9e1f581710dce803378ad2a50f311c27 Mon Sep 17 00:00:00 2001 From: Satyam0204 Date: Mon, 15 Jan 2024 17:16:51 +0530 Subject: [PATCH 1/2] [UPDATE] Monthwise Ordering of Events in Timeline api --- main/views.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/main/views.py b/main/views.py index ff29576..51d7cd8 100644 --- a/main/views.py +++ b/main/views.py @@ -1,3 +1,5 @@ +from calendar import month_name +from datetime import date from django.shortcuts import render from rest_framework import viewsets, generics from django.contrib.auth.models import User @@ -192,10 +194,28 @@ def get(self, request, format=None): class TimelineViewSet(viewsets.ModelViewSet): - queryset = Timeline.objects.all().order_by('-event_time') + queryset = Timeline.objects.all() serializer_class = serializers.TimelineSerializers http_method_names = ['get'] + def get_queryset(self): + queryset = super().get_queryset() + # Order by the month of the date_field + return queryset.order_by('event_time') + + def group_by_month_year(self, queryset): + current_year= date.today().year + + result = {f"{month} {year}": [] for year in range(current_year, current_year-3, -1) for month in month_name[1:]} + for obj in queryset: + + month_year = obj.event_time.strftime("%B %Y") + result.setdefault(month_year, []).append(serializers.TimelineSerializers(obj).data) + return result + def list(self, request, *args, **kwargs): + queryset = self.filter_queryset(self.get_queryset()) + grouped_by_month_year = self.group_by_month_year(queryset) + return Response(grouped_by_month_year) class UpcomingEventViewSet(viewsets.ModelViewSet): queryset = Event.objects.all().filter(upcoming=True) From 847d0c93a6ecd85cc6449e50f524d1214a2d93ff Mon Sep 17 00:00:00 2001 From: Satyam0204 Date: Mon, 15 Jan 2024 23:01:52 +0530 Subject: [PATCH 2/2] [FIX] new monthly timeline api --- .gitignore | 2 +- main/urls.py | 2 ++ main/views.py | 10 +++++++--- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 1e2d987..3c61e67 100644 --- a/.gitignore +++ b/.gitignore @@ -17,7 +17,7 @@ db.sqlite3-* main/media/* media/* - +env ## db.json ## diff --git a/main/urls.py b/main/urls.py index b81e71e..1060b04 100644 --- a/main/urls.py +++ b/main/urls.py @@ -18,6 +18,7 @@ router.register(r'carousel', views.CarouselImageViewSet) router.register(r'linit', views.LinitViewSet) router.register(r'timeline', views.TimelineViewSet) +router.register(r'timeline_monthly', views.MonthlyTimelineViewSet) router.register(r'alumni', views.AlumniViewSet) # In the latest DRF, We need to explicitly set base_name in our viewset url if we don't have queryset defined. router.register(r'alumni-by-year', views.AlumniByYearViewSet, basename="alumnibyyear") @@ -36,3 +37,4 @@ path('get_count/', views.GetCount.as_view(), name="get_count"), path('linit-pages/', views.LinitPages.as_view(), name="linit-pages"), ] + diff --git a/main/views.py b/main/views.py index 51d7cd8..18c3bcb 100644 --- a/main/views.py +++ b/main/views.py @@ -194,13 +194,17 @@ def get(self, request, format=None): class TimelineViewSet(viewsets.ModelViewSet): + queryset = Timeline.objects.all().order_by('-event_time') + serializer_class = serializers.TimelineSerializers + http_method_names = ['get'] + +class MonthlyTimelineViewSet(viewsets.ModelViewSet): queryset = Timeline.objects.all() serializer_class = serializers.TimelineSerializers http_method_names = ['get'] def get_queryset(self): queryset = super().get_queryset() - # Order by the month of the date_field - return queryset.order_by('event_time') + return queryset.order_by('-event_time') def group_by_month_year(self, queryset): current_year= date.today().year @@ -237,4 +241,4 @@ class DevPostViewSet(viewsets.ModelViewSet): class ConfigViewSet(viewsets.ModelViewSet): queryset = Config.objects.all() serializer_class = serializers.ConfigSerializers - http_method_names = ['get'] \ No newline at end of file + http_method_names = ['get']