added random_company_sample endpoint

This commit is contained in:
Sam
2021-03-05 11:26:40 +00:00
parent 7825810448
commit bcea4abb23
3 changed files with 70 additions and 1 deletions

View File

@@ -7,7 +7,7 @@ from django.template.loader import render_to_string
from django.contrib.gis.geoip2 import GeoIP2
# Create your views here.
from rest_framework import viewsets
from rest_framework import viewsets, status
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticatedOrReadOnly, IsAuthenticated
from rest_framework.decorators import api_view, permission_classes, action
@@ -172,3 +172,21 @@ def my_company(request):
limit = int(limit)
data = data[:limit]
return Response(data=data)
@api_view(['GET',])
@permission_classes([IsAuthenticatedOrReadOnly,])
def random_company_sample(request):
"""Return an slice of a randomized list of companies
Takes optional parameter size (int)
"""
try:
size = request.GET.get('size', 6)
size = int(size)
except:
return Response({"error": "Wrong value for size, must be a number"}, status=status.HTTP_406_NOT_ACCEPTABLE)
queryset = Company.objects.all().order_by('?')[:size]
serializer = CompanySerializer(queryset, many=True)
return Response(serializer.data)