python - How to handle multiple user type in Django -



python - How to handle multiple user type in Django -

i'm trying create little website has 3 types of users ["client" , "volunteer" , "coordinator"]. each type of user has restrictions on views can access. 3 users have different login pages.

approach 1 : accomplish this, i've added key session category, assign 1 of above given usertypes during login and, whenever view called, check whether user can access view.

login.html:

{% extends "base.html" %} {% block content %} {% if form.errors %} <p class="error"> sorry , invalid</p> {% endif %} <form action="/login_volunteer/authenticate/" method="post">{% csrf_token %} <label for="username"> username : </label> <input type="text" name="username" value="" id="username"> <label for="password"> password : </label> <input type="password" name="password" value="" id="password"> <input type="hidden" name="category" value="volunteer" id="category"> <input type="submit" value="login" /> </form> {% endblock %}

view.py:

def hello(request): name = "abhishek" if request.session.session_key none: html = '<html><body>session expired</body></html>' homecoming httpresponse(html) try: if not request.post.get('category') == 'volunteer html = '<html><body>you not allowed here</body></html>' homecoming httpresponse(html) except : print "error" html = '<html><body>hi awesome</body></html>' homecoming httpresponse(html)

approach 2 : thought create custom user class rather using default user provided django , assign customuser request.user during login. when view called, check is_client or is_volunteer.

customuser.py:

from django.db import models django.contrib.auth.models import abstractbaseuser class volunteeruser(abstractbaseuser): """ custom user class. """ email = models.emailfield('email address', unique=true, db_index=true) joined = models.datetimefield(auto_now_add=true) is_active = models.booleanfield(default=true) is_volunteer = models.booleanfield(default=false) class clientuser(abstractbaseuser): """ custom user class. """ email = models.emailfield('email address', unique=true, db_index=true) joined = models.datetimefield(auto_now_add=true) is_active = models.booleanfield(default=true) is_client = models.booleanfield(default=false)

so question is, of these approaches best way accomplish task @ hand? there other method solves this?

i'm concerned security , sense first method more insecure second.

the improve approach achieving requirement utilize inbuilt group , permissions model in django. since permissions can little tricky, alternative approach create userprofile model below:

from django.contrib.auth.models import user class userprofile(models.model): user = models.foreignkey(user) type = models.charfield(max_length=15)

then utilize decorators controlling access views this:

from django.contrib.auth.decorators import user_passes_test @user_pass_test(lambda u: u.get_profile().type == 'client') def view_for_client(request): ...

the userprofile model useful save of preferences of user. need set next setting:

auth_profile_module = 'accounts.userprofile'

python django python-2.7 django-sessions

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -