views:

53

answers:

2

I'm relatively new to django..

In the app that I'm building, there are multiple types of users (ie User1, User2, User3) that are all inheriting from django.contrib.auth.models.User and upon login, each user should be redirected to a success page depending on what type of user they are.

In views.py:

def login_attempt(request):

user = request.user
data = {}


username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:    
    if user.is_active:
        login(request, user)

        try: 
            User1.objects.get(username = user.username)
            type = "undergrad"
        except ObjectDoesNotExist:
            pass

        try:
            User2.objects.get(username = user.username)
            type = "grad"
        except ObjectDoesNotExist:
            pass

        try:
            User3.objects.get(username = user.username)
            type = "sponsor"
        except ObjectDoesNotExist:
            pass

        return render_to_response (
                "templates/success_"+type+".html",
                data,
                context_instance=RequestContext(request)
        )

    else:
        return render_to_response (
                "templates/fail1.html",
                data,
                context_instance=RequestContext(request)
        )
else:
    return render_to_response (
            "templates/fail2.html",
            data,
            context_instance=RequestContext(request))

and type(user) is <class 'django.contrib.auth.models.User'>

I'm currently running tests via "manage.py test" -- authentication and redirects are working for User1 and User2 successfully, however it doesn't authenticate for User3 and returns the "fail2.html" template. All other tests with User3 have returned valid results.

Any suggestions? This is my first question post, so feel free to ask questions if I've left relevant information out!

Thanks in advance.

A: 

This is not exactly an answer to your problem but have you considered using groups/permissions to differentiate between different types of users rather than creating subclasses of auth.User? Django's auth feature comes with a reasonably useful groups/permissions mechanism which can be leveraged to make your life more easier.

Using groups you can various create groups ("undergrad", "grad" etc.) and grant each group appropriate permissions to achieve the necessary effect.

Manoj Govindan
That's very true and I am using the groups so I might try and find a way to incorporate that in. Thanks
Eunice
Thanks for your suggestion! I figured out my problem, but wanted to thank you for you efforts in suggesting the permissions!
Eunice
Glad to help :)
Manoj Govindan
+1  A: 

Not really an answer for your question, but why not use a user profile to determine your type and other data specific to the UserX classes? They are easy to set up, allow you to store additional User information, and allows you to continue using the built in User objects.

The profile classes are pretty easy (I lifted this from The Django Book, Chapter 12):

class MySiteProfile(models.Model):
    # This is the only required field
    user = models.ForeignKey(User, unique=True)

    # The rest is completely up to you...
    type = models.CharField(maxlength=100, blank=True)

Beyond that, you change on item in settings.py, and set up a trigger to automagically create the profile on User creation, and you're good to go.

Your resulting view code would be drastically simplified, too:

def login_attempt(request):
    user = request.user
    data = {}
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(username=username, password=password)
    if user is not None:    
        if user.is_active:
            login(request, user)
            type = user.get_profile().type

            return render_to_response (
                "templates/success_"+type+".html",
                data,
                context_instance=RequestContext(request)
            )

        else:
            return render_to_response (
                    "templates/fail1.html",
                    data,
                    context_instance=RequestContext(request)
                )
    else:
        return render_to_response (
                "templates/fail2.html",
                data,
                context_instance=RequestContext(request)
            )
Jack M.
So I currently have a user profile, the issue being that each user type has specific fields depending on that type and I didn't want to have too many null-fields.. but I'll try some variation of this out. Thanks!
Eunice
To be honest, if it is less than say 45 fields, I wouldn't normalize them. Having to join together 4 different `UserX` tables, 4 different `GroupY` tables, and the `User` table is absolutely not worth it.
Jack M.
I figured out my problem, but want to thank you for your efforts anyways! unfortunately, i can't vote your answer up bc i lack reputation, haha! but thank you, Jack!
Eunice