Skip to content

Commit e7bcc7b

Browse files
committed
Use random endpoint instead of frontpage
1 parent ea1b6ad commit e7bcc7b

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

lego/apps/frontpage/views.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
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

2320
class 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)

lego/apps/users/views/abakus_groups.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
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

37
from lego.apps.permissions.api.views import AllowedPermissionsMixin
48
from 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)

0 commit comments

Comments
 (0)