def get_queryset(self):
        # We narrow the results to only one location
        location_slug = self.kwargs.get('location_slug')
        if location_slug is None:
            qs = self.model.objects.all()
        else:
            qs = self.model.objects.filter(location__slug=location_slug)

        # We filter results only within one category
        try:
            category_pk = int(self.request.GET.get('category'))
        except (ValueError, TypeError):
            category_pk = None
        if category_pk is not None:
            qs = qs.filter(category__pk=category_pk)

        # We set a maximum time period we want to search...
        time_limit = get_time_difference(self.request.GET.get('time', 'all'))
        if time_limit is not None:
            qs = qs.filter(date_created__gte=time_limit)

        # ...and the order of results displayed.
        order = self.request.GET.get('sortby', '-date_created')
        try:
            self.model._meta.get_field_by_name(order.replace('-', ''))
            qs = qs.order_by(order)
        except FieldDoesNotExist:
            pass

        return qs
Beispiel #2
0
    def get_queryset(self):
        # Ograniczamy wyniki tylko do jednej lokalizacji
        location_slug = self.kwargs.get('location_slug')
        if location_slug is None:
            qs = self.model.objects.all()
        else:
            qs = self.model.objects.filter(location__slug=location_slug)

        # Filtrujemy wyniki tylko w ramach jednej kategorii
        try:
            category_pk = int(self.request.GET.get('category'))
        except (ValueError, TypeError):
            category_pk = None
        if category_pk is not None:
            qs = qs.filter(category__pk=category_pk)

        # Ustawiamy maksymalny przedział czasowy do przeszukania...
        time_limit = get_time_difference(self.request.GET.get('time', 'all'))
        if time_limit is not None:
            qs = qs.filter(date_created__gte=time_limit)

        # ...i kolejność wyświetlania wyników.
        order = self.request.GET.get('sortby', '-date_created')
        try:
            self.model._meta.get_field_by_name(order.replace('-', ''))
            qs = qs.order_by(order)
        except FieldDoesNotExist:
            pass

        return qs
Beispiel #3
0
    def get_queryset(self):
        # We narrow the results to only one location
        location_slug = self.kwargs.get('location_slug')
        if location_slug is None:
            qs = self.model.objects.all()
        else:
            l = get_object_or_404(Location, slug=location_slug)
            id_list = [l.pk, ] + [x[0] for x in l.location_set.values_list('pk')]
            qs = self.model.objects.filter(location__pk__in=id_list)

        # We filter results only within one category
        try:
            category_pk = int(self.request.GET.get('category'))
        except (ValueError, TypeError):
            category_pk = None
        if category_pk is not None:
            qs = qs.filter(category__pk=category_pk)

        # We set a maximum time period we want to search...
        time_limit = get_time_difference(self.request.GET.get('time', 'all'))
        if time_limit is not None:
            qs = qs.filter(date_created__gte=time_limit)

        # ...and the order of results displayed.
        order = self.request.GET.get('sortby', '-date_created')
        try:
            self.model._meta.get_field_by_name(order.replace('-', ''))
            qs = qs.order_by(order)
        except FieldDoesNotExist:
            pass

        return qs
Beispiel #4
0
    def get(self, request):

        # Id lokacji, z której pobieramy wpisy
        try:
            location_pk = int(request.QUERY_PARAMS.get('pk'))
        except (ValueError, TypeError):
            location_pk = None

        # Numer strony do wyświetlenia
        try:
            page = int(request.QUERY_PARAMS.get('page'))
        except (ValueError, TypeError):
            page = 1

        # Ilość elementów na stronę
        try:
            per_page = int(request.QUERY_PARAMS.get('per_page'))
        except (ValueError, TypeError):
            per_page = self.paginate_by

        # Rodzaj typu zawartości (albo wszystkie)
        content = request.QUERY_PARAMS.get('content', 'all')

        # Przedział czasowy do przeszukania
        time = get_time_difference(request.QUERY_PARAMS.get('time', 'any'))

        # Wyszukiwanie po tytułach wpisów
        haystack = request.QUERY_PARAMS.get('haystack')

        # Wyszukiwanie poprzez ID kategorii (jeżeli dotyczy)
        try:
            category = int(request.QUERY_PARAMS.get('category'))
        except (ValueError, TypeError):
            category = None

        location = get_object_or_404(Location, pk=location_pk)
        content_objects = location.content_objects()

        if content != 'all':
            content_objects = [x for x in content_objects if x['type']==content]
        if time is not None:
            content_objects = [x for x in content_objects\
                                    if x['date_created'] >= time.isoformat()]
        if haystack:
            content_objects = [x for x in content_objects \
                                    if haystack.lower() in x['title'].lower()]
        if category is not None:
            content_objects = [x for x in content_objects if x['category']['pk']==category]

        # Opcje sortowania wyników (dotyczy konkretnych obiektów)
        sortby = self.request.QUERY_PARAMS.get('sortby')
        if sortby == 'title':
            content_objects = sort_by_locale(content_objects,
                            lambda x: x['title'], translation.get_language())
        elif sortby == 'oldest':
            content_objects.sort(key=lambda x: x['date_created'])
        elif sortby == 'newest':
            content_objects.sort(key=lambda x: x['date_created'], reversed=True)
        elif sortby == 'user':
            content_objects.sort(key=lambda x: x['creator']['name'].split(' ')[1])

        paginator = Paginator(content_objects, per_page)
        try:
            items = paginator.page(page)
        except EmptyPage:
            items = paginator.page(1)
        serializer = ContentPaginatedSerializer(items, context={'request': request})

        return Response(serializer.data)
Beispiel #5
0
 def date_filter(self):
     return get_time_difference(self.request.QUERY_PARAMS.get('time'))
Beispiel #6
0
    def get(self, request):

        # Location Id from which we gather entries
        try:
            location_pk = int(request.QUERY_PARAMS.get('pk'))
        except (ValueError, TypeError):
            location_pk = None

        # Number of page that is going to be displayed
        try:
            page = int(request.QUERY_PARAMS.get('page'))
        except (ValueError, TypeError):
            page = 1

        # Number of elements on the site
        try:
            per_page = int(request.QUERY_PARAMS.get('per_page'))
        except (ValueError, TypeError):
            per_page = self.paginate_by

        # Type of content (or all)
        content = request.QUERY_PARAMS.get('content', 'all')

        # Time duration for search
        time = get_time_difference(request.QUERY_PARAMS.get('time', 'any'))

        # Search by entries' titles
        haystack = request.QUERY_PARAMS.get('haystack')

        # Search through category ID (if applicable)
        try:
            category = int(request.QUERY_PARAMS.get('category'))
        except (ValueError, TypeError):
            category = None

        location = get_object_or_404(Location, pk=location_pk)
        content_objects = location.content_objects()

        if content != 'all':
            content_objects = [
                x for x in content_objects if x['type'] == content
            ]
        if time is not None:
            content_objects = [x for x in content_objects\
                                    if x['date_created'] >= time.isoformat()]
        if haystack:
            content_objects = [x for x in content_objects \
                                    if haystack.lower() in x['title'].lower()]
        if category is not None:
            content_objects = [
                x for x in content_objects if x['category']['pk'] == category
            ]

        # Sort results option (Opcje sortowania wyników (is applicable to concrete objects)
        sortby = self.request.QUERY_PARAMS.get('sortby')
        if sortby == 'title':
            content_objects = sort_by_locale(content_objects,
                                             lambda x: x['title'],
                                             translation.get_language())
        elif sortby == 'oldest':
            content_objects.sort(key=lambda x: x['date_created'])
        elif sortby == 'newest':
            content_objects.sort(key=lambda x: x['date_created'],
                                 reversed=True)
        elif sortby == 'user':
            content_objects.sort(
                key=lambda x: x['creator']['name'].split(' ')[1])

        paginator = Paginator(content_objects, per_page)
        try:
            items = paginator.page(page)
        except EmptyPage:
            items = paginator.page(1)
        serializer = ContentPaginatedSerializer(items,
                                                context={'request': request})

        return Response(serializer.data)
Beispiel #7
0
 def get_queryset(self):
     qs = super(DateFilteredContentMixin, self).get_queryset()
     time = get_time_difference(self.request.QUERY_PARAMS.get('time'))
     if time is not None:
         return qs.filter(date_created__gt=time)
     return qs
Beispiel #8
0
    def get(self, request):

        # Location Id from which we gather entries
        try:
            location_pk = int(request.QUERY_PARAMS.get('pk'))
        except (ValueError, TypeError):
            location_pk = None

        # Number of page that is going to be displayed
        try:
            page = int(request.QUERY_PARAMS.get('page'))
        except (ValueError, TypeError):
            page = 1

        # Number of elements on the site
        try:
            per_page = int(request.QUERY_PARAMS.get('per_page'))
        except (ValueError, TypeError):
            per_page = self.paginate_by

        # Type of content (or all)
        content = request.QUERY_PARAMS.get('content', 'all')

        # Time duration for search
        time = get_time_difference(request.QUERY_PARAMS.get('time', 'any'))

        # Search by entries' titles
        haystack = request.QUERY_PARAMS.get('haystack')

        # Search through category ID (if applicable)
        try:
            category = int(request.QUERY_PARAMS.get('category'))
        except (ValueError, TypeError):
            category = None

        location = get_object_or_404(Location, pk=location_pk)
        content_objects = location.content_objects()

        if content != 'all':
            content_objects = [x for x in content_objects
                               if x['type'] == content]
        if time is not None:
            content_objects = [x for x in content_objects\
                                    if x['date_created'] >= time.isoformat()]
        if haystack:
            content_objects = [x for x in content_objects \
                                    if haystack.lower() in x['title'].lower()]
        if category is not None:
            content_objects = [x for x in content_objects
                               if x['category']['pk'] == category]

        # Sort results option (Opcje sortowania wyników (is applicable to concrete objects)
        sortby = self.request.QUERY_PARAMS.get('sortby')
        if sortby == 'title':
            content_objects = sort_by_locale(content_objects,
                                             lambda x: x['title'],
                                             translation.get_language())
        elif sortby == 'oldest':
            content_objects.sort(key=lambda x: x['date_created'])
        elif sortby == 'newest':
            content_objects.sort(key=lambda x: x['date_created'],
                                 reversed=True)
        elif sortby == 'user':
            content_objects.sort(
                key=lambda x: x['creator']['name'].split(' ')[1])

        paginator = Paginator(content_objects, per_page)
        try:
            items = paginator.page(page)
        except EmptyPage:
            items = paginator.page(1)
        serializer = ContentPaginatedSerializer(items,
                                                context={'request': request})

        return Response(serializer.data)
Beispiel #9
0
 def get_queryset(self):
     qs = super(DateFilteredContentMixin, self).get_queryset()
     time = get_time_difference(self.request.QUERY_PARAMS.get('time'))
     if time is not None:
         return qs.filter(date_created__gt=time)
     return qs
Beispiel #10
0
 def date_filter(self):
     return get_time_difference(self.request.QUERY_PARAMS.get('time'))