Posts about Python

Avatar for vbabiy@howsthe.com

django.contrib.auth User object login

Published Feb. 1, 2011 by Vitaly Babiy

While working on the sign up process I noticed it takes to many steps for a user to login to their new account. Where I wanted to get to was once the user completes the sign up process they're automatically redirected in to their account, and logged in. In order to make this work with django.contrib.auth it requires us to write a custom auth backend. Due to the clean api this is very trivial to do. Here is what my auth backend looks like:

from django.contrib.auth.backends import ModelBackend

class UserObjectAuthBackend(ModelBackend):
    def authenticate(self, username=None, password=None, user=None):
        if not user:
            try:
                user = self.user_class.login.get(username=username)
            except self.user_class.DoesNotExist:
                pass

            if user and user.check_password(password):
                return user
            return None
        return user

If you‘ve ever created a auth backend you will notice something a little different about my version, I will explain this a little later. First a little explanation of what the backend is doing. In order to implement a backend we will need to over write one method, authenticate. This method is suppose to take a username and password, and using this information it needs to look up the user and then verify that the password is correct, if its correct return the user object. Our goal is to login a user using only the User object, to reach our goal we will change the default api just a little to allow us to pass in a user object to authenticate. In this version the only time we need to check the users password is when we don’t already have a user object, and if we do we just return it. To wrap up the work on the auth backend we will need to let the auth app we want to use our custom backend, to do this we need to add the following to our settings.py file:

AUTHENTICATION_BACKENDS = (
    'lib.UserObjectAuthBackend',
)

Now its time to see the view that will login in a user and redirect them to where ever they need to be:

from django.contrib.auth import authenticate, login

def signup(request):
    # ...
    # Sign up process
    # `user` is the new user that just signed up.
    # ...
    user = authenticate(user=user)
    login(request, user)
    return HttpResponseRedirect('/my_account')

That’s it folks, when the user see the next page they will be logged in. For more information also check out the great Django documentation. If you got any question or suggestions leave them below.

Tags
  • auth
  • Django
Avatar for vbabiy@howsthe.com

Datetime mocking with pyxmox

Published Feb. 11, 2010 by Vitaly Babiy

This is a reference on how to mock out the datetime.now() call in python using mox. Since datetime is a built-in it makes things a little more tricky.

def test_datetime(self):
    now = datetime.datetime.now() # Store it for later use
    m.StubOutWithMock(datetime, 'datetime')

    datetime.datetime.now().AndReturn(now)

    m.ReplayAll()

    datetime.datetime.now()

    m.VerifyAll()
    m.UnsetStubs()

First we will store the current datetime, this has to be done before mocking takes place. Next we use StubOutWitMock to mock out the datetime class, we then record the call to now and have it return our stored now. If you have any question leave them in the comments.

Links:

Avatar for vbabiy@howsthe.com

Python mocking with mox

Published Jan. 10, 2010 by Vitaly Babiy

When building an application it's hard not to depend on some outside resource, that you have no control over. When writing unit tests for this type of application it gets tricky, but thanks to mocking you are able to do so. Mocking comes in two forms, manual (a.k.a monkey patching) or using a mocking framework, we will be using the latter. A mocking framework creates objects that add expectations, define their methods, and return values for each method call in a simple way.

There are a few mocking frameworks available for python:

A blog about development, marketing, and design.

The next version of Ubuntu is coming soon