Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UPDATE] Monthwise Ordering of Events in Timeline api #105

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion main/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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')
Copy link
Contributor

@mayankt18 mayankt18 Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

order in descending order, earlier it was -event_time @Satyam0204


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)
Expand Down
Loading