Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Integrating Sphinx and Django in order to require users to log in to see the documentation

I am curious if it is possible to hide sphinx documentation inside a django app so that only people who log in can see it. It seems to me that since sphinx creates its own structure and that Django uses the urlconf to define which pages a user can see, that it wouldn't be possible to combine the two. Although there must be some combining since the Django website likely uses django and sphinx. I am wondering if anyone has any insight or if they can point me in the right direction.

Thank You in Advance!

like image 301
spike74 Avatar asked Feb 14 '23 11:02


1 Answers

Sphinx builds your docs into HTML files, so in most cases this docs should be served by your web server rather then Django. However Django is able to serve static files as well.

You can use the django.views.static.serve function to do this and wrap this function with login_required. E.g:

from django.views.static import serve
from django.contrib.auth.decorators import login_required

urlpatterns += patterns('', 
    url(r'^docs/(?P<path>.*)', login_required(serve), {'document_root': '/path/to/sphinx/build/html'}, 'docs'),

However this configuration will be considered a bad practice in production environment as in this case Django will serve both html and css/js files from your sphinx theme.

The first improvement you can do here is to serve /path/to/sphinx/build/html/_static/ with apache/nginx or whatever you use.

The more proper way is to serve docs with apache/nginx and make it handle the auth itself. Unfortunately I made a quick Google search but did not find a way to use Django's User model to handle http_auth in apache or other. Alternatively you can use something like mod_sendfile or X-Accel modules - http://www.wellfireinteractive.com/blog/nginx-django-x-accel-redirects/ In a nutshell - Django app checks permission if user can view the file and add special header to response containing file path. Webserver will serve this file instead of original message from django

like image 70
Igor Avatar answered Feb 17 '23 20:02