etianen / django-watson

Full-text multi-table search application for Django. Easy to install and use, with good performance.
BSD 3-Clause "New" or "Revised" License
1.2k stars 130 forks source link

Require Authentication #184

Closed mweinelt closed 7 years ago

mweinelt commented 7 years ago

I've tried and failed to require authentication on the search view.

The search view can be used without authentication in both cases, what am I missing?

@method_decorator(login_required, name='get')
class SearchView(SearchMixin, ListView):
    login_url = '/login/'
    redirect_field_name = 'redirect_to'
    template_name = 'module/search_result.html'
class SearchView(LoginRequiredMixin, SearchMixin, ListView):
    login_url = '/login/'
    redirect_field_name = 'redirect_to'
    template_name = 'module/search_result.html'
etianen commented 7 years ago

Try:

@method_decorator(login_required, name='dispatch')
mweinelt commented 7 years ago

That's what works on djangos generic views, but they also have a dispatch method to decorate.

Which dispatch method would that refer to in Watsons SearchMixin? It doesn't inherit from a basic view, and has no dispatch method that I can see. Anyway, I tried that and it didn't work.

etianen commented 7 years ago

This should work:

from watson.views import SearchView

class LoginSearchView(LoginRequiredMixin, SearchView):
    login_url = '/login/'
    redirect_field_name = 'redirect_to'
    template_name = 'module/search_result.html'

If it's not, then it's time to start debugging the django LoginRequiredMixin. Put print statements into it's dispatch() method etc. django-watson does nothing that could interfere with Django authentication.