Sunday, April 24, 2011

Checking for user type in view

Hello guys,

In my application models I have two models, Judge and Participant:

from django.contrib.auth.models import User

class Judge(User):
pass

class Participant(User):
pass

In my view I want to find out if the authenticated user is either a
Judge or a Participant. How can I do that?


I have done the following, and it works most of the time for me:

def index(request):
user = request.user
if user.is_authenticated():
if user.is_superuser:
return redirect('/admin')

judge = None
participant = None
competition = None
try:
participant = user.participant or None
judge = user.judge or None
if participant:
competition = participant.competition
if judge:
competition = judge.competition
except Participant.DoesNotExist, e:
pass
except Judge.DoesNotExist, e:
pass
except Exception, e:
raise
# Do some more stuff...

But this is ugly. It would be cool if you could come up with better ideas.


Kenny

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate