Beispiel #1
0
    def search(request):
        """EXPENSIVE: Search with query param `query` throughout the database."""
        MIN_LENGTH = 3

        req_query = request.GET.get("query")
        if not req_query or len(req_query) < MIN_LENGTH:
            return Response({"message": "No query or too short!"}, status=400)

        # Search bodies by name and description
        bodies = query_search(request, MIN_LENGTH, Body.objects,
                              ['name', 'description'])

        # Search events by name and description
        events = get_prioritized(
            query_search(request, MIN_LENGTH, Event.objects,
                         ['name', 'description'])[:20], request)

        # Search users by only name: don't add anything else here
        users = query_search(request, MIN_LENGTH, UserProfile.objects,
                             ['name', 'ldap_id', 'roll_no'])[:20]

        return Response({
            "bodies": BodySerializerMin(bodies, many=True).data,
            "events": EventSerializer(events, many=True).data,
            "users": UserProfileSerializer(users, many=True).data
        })
Beispiel #2
0
    def search(request):
        """EXPENSIVE: Search with query param `query` throughout the database."""
        MIN_LENGTH = 3

        req_query = request.GET.get("query")
        if not req_query or len(req_query) < MIN_LENGTH:
            return Response({"message": "No query or too short!"}, status=400)

        types = ('bodies', 'events', 'users')
        req_types = request.GET.get('types')
        if req_types:
            types = tuple(req_types.split(','))

        # Include only the types we want
        bodies, events, users = ([] for i in range(3))

        # Search bodies by name and description
        if 'bodies' in types:
            bodies = query_search(request,
                                  MIN_LENGTH,
                                  Body.objects, ['name', 'description'],
                                  'bodies',
                                  order_relevance=True)

        # Search events by name and description
        if 'events' in types:
            events = get_prioritized(
                query_search(request, MIN_LENGTH, Event.objects,
                             ['name', 'description'], 'events')[:20], request)

        # Search users by only name: don't add anything else here
        if 'users' in types:
            users = query_search(request,
                                 MIN_LENGTH,
                                 UserProfile.objects.filter(active=True),
                                 ['name', 'ldap_id', 'roll_no'],
                                 'profiles',
                                 order_relevance=True)[:20]

        return Response({
            "bodies": BodySerializerMin(bodies, many=True).data,
            "events": EventSerializer(events, many=True).data,
            "users": UserProfileSerializer(users, many=True).data
        })
Beispiel #3
0
    def list(self, request):  #pylint: disable=unused-argument
        """List Events.
        List fresh events prioritized for the current user."""

        # Check for date filtered query params
        start = request.GET.get('start')
        end = request.GET.get('end')

        if start is not None and end is not None:
            # Try date-filtered if we have the params
            queryset = get_prioritized(
                self.queryset.filter(start_time__range=(start, end)), request)
        else:
            # Respond with recent events
            queryset = get_fresh_prioritized_events(self.queryset, request)

        serializer = EventSerializer(queryset, many=True)
        data = serializer.data

        return Response({'count': len(data), 'data': data})