예제 #1
0
    def get_queryset(self):
        queryset = super(LinkList, self).get_queryset().order_by('-added')
        not_on_page = 'page' not in self.request.GET

        if self.has_query():
            queryset = SearchQuerySet().filter(content=AutoQuery(
                self.request.GET['q']), ).filter_or(
                    categories=AutoQuery(self.request.GET['q'])).filter_or(
                        network_location=AutoQuery(self.request.GET['q']))

        if self.has_favourites():
            fav_pks = [l.pk for l in self.request.user.favourites.all()]
            if self.has_query():
                queryset = queryset.models(Link).filter(key__in=fav_pks)
            else:
                queryset = queryset.filter(id__in=fav_pks).distinct()

        if self.has_categories():
            categories_to_filter = dict(self.request.GET)['categories']
            if type(categories_to_filter) == str:
                categories_to_filter = [categories_to_filter]
            if self.has_query():
                queryset = queryset.models(Link).filter(
                    categories__in=categories_to_filter)
            else:
                # At this point, the queryset should already be ordered because
                # of the original get_queryset call at the beginning of this
                # function.
                queryset = queryset.filter(
                    categories__name__in=categories_to_filter).distinct()

        if self.external_only() or self.internal_only():
            if self.has_query():
                queryset = queryset.models(Link).exclude(
                    is_external=self.internal_only())
            else:
                # At this point, the queryset should already be ordered because
                # of the original get_queryset call at the beginning of this
                # function.
                queryset = queryset.exclude(
                    is_external=self.internal_only()).distinct()

        if self.has_query() and not self.has_categories() and not_on_page:
            # At this point the queryset is a list of SearchResult objects, all
            # of them. So, the length is accurate. By the time it reaches
            # context, it won't be.
            st, created = SearchTerm.objects.get_or_create(
                query=self.request.GET.get('q'))
            sq = SearchQuery()
            sq.term = st
            sq.results_length = len(queryset)
            sq.user = self.request.user
            sq.save()

        return queryset
예제 #2
0
    def test_query_is_saved(self):
        st = SearchTerm.objects.create(
            query='test'
        )
        sq = SearchQuery()
        sq.term = st
        sq.results_length = 5
        sq.user = self.user
        sq.save()

        retrieved_term = SearchTerm.objects.all()[0]
        retrieved_query = SearchQuery.objects.all()[0]

        self.assertEquals(retrieved_term.query, 'test')
        self.assertEquals(retrieved_query.term, retrieved_term)