Exemplo n.º 1
0
 def put(self, request, pk, format=None):
     wormhole = self.get_object(pk)
     serializer = WormholeSerializer(wormhole, data=request.data, partial=True)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 2
0
 def get(self, request, format=None):
     # status = APIView.GET.get('status','')
     # print(status)
     print('wormholelist')
     print(Wormhole.objects.all())
     wormholes = Wormhole.objects.all()
     serializer = WormholeSerializer(wormholes, many=True)
     return Response(serializer.data)
Exemplo n.º 3
0
def sorted_list(request):

    """
    Sorting criteria: nearby, recent
    """

    sorting_criteria = request.GET.get('sort_by', '')

    if request.method == 'GET':
        
        if sorting_criteria == 'recent':
            wormholes = Wormhole.objects.all()
            serializer = WormholeSerializer(wormholes, many=True)
            return Response(serializer.data)

        elif sorting_criteria == 'popular':
            wormholes = Wormhole.objects.all()
            serializer = WormholeSerializer(wormholes, many=True).data

            # sort by like
            sortedWormholes = sorted(serializer, key=lambda worm: worm.likers[0])
            return Response(sortedWormholes)

        elif sorting_criteria == 'nearby':
            wormholes = Wormhole.objects.all()
            serializer = WormholeSerializer(wormholes, many=True).data

            lon2 = request.GET.get('longitude', '')
            lat2 = request.GET.get('latitude', '')

            # loop through wormholes and add distance key for each wormhole
            for worm in serializer:
                lon1 = worm.get('longitude')
                lat1 = worm.get('latitude')
                dist = distanceMain(lon1, lat1, lon2, lat2)
                worm.distance = dist

            # sort by distance
            sortedWormholes = sorted(serializer, key=lambda worm: worm.distance)
            return Response(sortedWormholes)
Exemplo n.º 4
0
 def get(self, request, pk, format=None):
     wormhole = self.get_object(pk)
     serializer = WormholeSerializer(wormhole)
     return Response(serializer.data)