File tree Expand file tree Collapse file tree 2 files changed +24
-15
lines changed Expand file tree Collapse file tree 2 files changed +24
-15
lines changed Original file line number Diff line number Diff line change 1515 DetailedPollSerializer ,
1616 HiddenResultsDetailedPollSerializer ,
1717)
18- from lego .apps .users .models import AbakusGroup
19- from lego .apps .users .serializers .abakus_groups import PublicAbakusGroupSerializer
20- from lego .apps .users .constants import GROUP_INTEREST
2118
2219
2320class FrontpageViewSet (viewsets .ViewSet ):
@@ -93,8 +90,6 @@ def get_serializer_context():
9390
9491 queryset_poll = Poll .objects .filter (pinned = True ).order_by ("created_at" ).last ()
9592
96- queryset_interestgroups = AbakusGroup .objects .filter (type = GROUP_INTEREST , active = True ).order_by ("?" )
97-
9893 articles = PublicArticleSerializer (
9994 queryset_articles [:10 ], context = get_serializer_context (), many = True
10095 ).data
@@ -111,15 +106,7 @@ def get_serializer_context():
111106 poll = DetailedPollSerializer (
112107 queryset_poll , context = get_serializer_context ()
113108 ).data
114- interestgroups = PublicAbakusGroupSerializer (
115- queryset_interestgroups [:3 ], context = get_serializer_context (), many = True
116- ).data
117109
118- ret = {
119- "articles" : articles ,
120- "events" : events ,
121- "poll" : poll ,
122- "interestgroups" : interestgroups ,
123- }
110+ ret = {"articles" : articles , "events" : events , "poll" : poll }
124111
125112 return Response (ret )
Original file line number Diff line number Diff line change 1- from rest_framework import viewsets
1+ from random import sample
2+
3+ from rest_framework import status , viewsets
4+ from rest_framework .decorators import action
5+ from rest_framework .response import Response
26
37from lego .apps .permissions .api .views import AllowedPermissionsMixin
48from lego .apps .permissions .constants import EDIT
@@ -43,3 +47,21 @@ def get_queryset(self):
4347 return AbakusGroup .objects_with_text .prefetch_related ("users" ).all ()
4448
4549 return self .queryset
50+
51+ @action (detail = False , methods = ["GET" ])
52+ def random_interests (self , request ):
53+ queryset = self .get_queryset ().filter (type = "interesse" )
54+
55+ values = queryset .values_list ("pk" , flat = True )
56+ if not values :
57+ return Response (status = status .HTTP_204_NO_CONTENT )
58+
59+ values = list (values )
60+
61+ if len (values ) <= 3 :
62+ instance = [queryset .get (pk = pk ) for pk in values ]
63+ else :
64+ instance = [queryset .get (pk = pk ) for pk in sample (values , 3 )]
65+
66+ serializer = self .get_serializer (instance , many = True )
67+ return Response (serializer .data )
You can’t perform that action at this time.
0 commit comments