# Add to the bottom of your wsgi.py file
# Don't forget to add barrel to your requirements!
from barrel import cooper
REALM = "PRIVATE"
USERS = [('spam', 'eggs')]
application = cooper.basicauth(users=USERS, realm=REALM)(get_wsgi_application())
Showing posts with label auth. Show all posts
Showing posts with label auth. Show all posts
Jul 12, 2012
Basic access authentication implementation. Django 1.4
http://pydanny.com/simple-http-basic-auth-wall.html
Labels:
auth,
http basic auth
Dec 30, 2011
Extending the Django User model with inheritance
from django.contrib.auth.models import User, UserManager
class CustomUser(User):
"""User with app settings."""
timezone = models.CharField(max_length=50, default='Europe/London')
# Use UserManager to get the create_user method, etc.
objects = UserManager()
...
user = CustomUser.objects.create(...)
# auth_backends.py
from django.conf import settings
from django.contrib.auth.backends import ModelBackend
from django.core.exceptions import ImproperlyConfigured
from django.db.models import get_model
class CustomUserModelBackend(ModelBackend):
def authenticate(self, username=None, password=None):
try:
user = self.user_class.objects.get(username=username)
if user.check_password(password):
return user
except self.user_class.DoesNotExist:
return None
def get_user(self, user_id):
try:
return self.user_class.objects.get(pk=user_id)
except self.user_class.DoesNotExist:
return None
@property
def user_class(self):
if not hasattr(self, '_user_class'):
self._user_class = get_model(*settings.CUSTOM_USER_MODEL.split('.', 2))
if not self._user_class:
raise ImproperlyConfigured('Could not get custom user model')
return self._user_class
# settings.py
AUTHENTICATION_BACKENDS = (
'myproject.auth_backends.CustomUserModelBackend',
)
...
CUSTOM_USER_MODEL = 'accounts.CustomUser'
http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/
Subscribe to:
Comments (Atom)