Skip to content

Instantly share code, notes, and snippets.

@Mortal
Created February 17, 2013 15:58
Show Gist options
  • Save Mortal/4971972 to your computer and use it in GitHub Desktop.
Save Mortal/4971972 to your computer and use it in GitHub Desktop.
Django password protection with Apache basic protection
import os, sys
here = lambda * x: os.path.join(os.path.abspath(os.path.dirname(__file__)), *x)
sys.path.append(here('venv/lib/python2.6/site-packages'))
sys.path.append(here('web'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'mftutor.settings'
from django.contrib.auth.models import User
from django import db
from mftutor.settings import YEAR
def check_password(environ, user, password):
db.reset_queries()
kwargs = {'username': user, 'is_active': True, 'tutorprofile__tutor__year__exact': YEAR}
try:
try:
user = User.objects.get(**kwargs)
except User.DoesNotExist:
return None
if user.check_password(password):
return True
else:
return False
finally:
db.connection.close()
<Location /static/gallery/>
AuthType Basic
AuthName "matfystutor.dk"
AuthBasicProvider wsgi
WSGIAuthUserScript /home/mftutor/web/auth.wsgi
Require valid-user
</Location>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment