Blue K
Blue K

Reputation: 21

Integrating Haystack in Django-CMS omitting Pages with View-Restrictions

I want to integrate haystack with django-cms making a search view. My CMS has pages with view restrictions (only a few authenticated users have access to some pages).

The problem is: when making a search, haystack gives me list with results from all pages, including pages to which the current user has not view permissions.

How can I integrate Haystack in a way that filters results, showing only the ones to which the current user has permissions for? If that's not possible, how to configure haystack letting it index only pages without view-restrictions? Any help is appreciated.

Upvotes: 1

Views: 305

Answers (1)

Blue K
Blue K

Reputation: 21

In my solution to this problem I use aldryn_search to do the integration of Haystack and django-cms. aldryn_search returns a listwith results from all pages, including the ones the current user hast not view-permissions for. To resolve this issue I'm inheriting from AldrynSearchView and override the get_queryset method like this:

 def get_queryset(self):
    queryset = super(IntranetSearchView, self).get_queryset()

    for result in queryset.load_all():  
        page = result.object.page
        # Begin: modified copy (queryset.exclude added) of cms.utils.decorators.cms_perms
        if page:
            if page.login_required and not self.request.user.is_authenticated():
                queryset = queryset.exclude(id=result.id)
            if not page.has_view_permission(self.request, user=self.request.user):
                queryset = queryset.exclude(id=result.id)
        # End: Copy
    return queryset

using queryset.exclude() to exclude results the current user has not permissions for. After that I inherit from AldrynSearchApphook overriding the urls with my new View and than doing a apphoook_pool.register of the modified Apphook.

Upvotes: 1

Related Questions