From e2e970dbc4e56c8b44ac113af42cc481a8c32f9e Mon Sep 17 00:00:00 2001 From: parthahere001 <101795082+parthahere001@users.noreply.github.com> Date: Wed, 2 Nov 2022 21:24:56 +0530 Subject: [PATCH] [ADD] Made a new route to serve alumni grouped by year (#83) Co-authored-by: parthahere001 --- main/urls.py | 1 + main/views.py | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/main/urls.py b/main/urls.py index 6201f86..abd19bb 100644 --- a/main/urls.py +++ b/main/urls.py @@ -19,6 +19,7 @@ router.register(r'linit', views.LinitViewSet) router.register(r'timeline', views.TimelineViewSet) router.register(r'alumni', views.AlumniViewSet) +router.register(r'alumni-by-year', views.AlumniByYearViewSet) router.register(r'techbytes', views.TechBytesViewSet) router.register(r'devposts', views.DevPostViewSet) router.register(r'configs', views.ConfigViewSet) diff --git a/main/views.py b/main/views.py index effe023..d88991b 100644 --- a/main/views.py +++ b/main/views.py @@ -4,14 +4,14 @@ from main.models import Config, Event, Profile, Alumni, About, Project, Contact, Activity, CarouselImage, Linit, Timeline, LinitImage, TechBytes, DevPost from main import serializers from main.forms import ProfileForm, ProfileChangeForm, MemberRegistrationForm -from django.contrib.auth.forms import UserCreationForm -from django.http import HttpResponseRedirect, HttpResponse +from django.http import HttpResponseRedirect from django.contrib.auth.decorators import login_required from django.contrib import messages -from django.urls import reverse, NoReverseMatch +from django.urls import reverse from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.permissions import AllowAny +from collections import defaultdict def register(request): @@ -108,7 +108,16 @@ class AlumniViewSet(viewsets.ModelViewSet): # ViewSets define the view behavior. - +class AlumniByYearViewSet(viewsets.ModelViewSet): + queryset = Alumni.objects.all().order_by('-passout_year', 'first_name') + serializer_class = serializers.AlumniSerializer + http_method_names = ['get'] + def list(self,request): + data = defaultdict(list) + alumunus = self.queryset + for alumni in alumunus : + data[alumni.passout_year].append(self.serializer_class(alumni).data) + return Response(data) class UserViewSet(viewsets.ModelViewSet): queryset = User.objects.all()