Re: Set Permissions in User model from views
On Thu, Dec 6, 2012 at 8:10 AM, Nikhil Verma <varma.nikhil22@gmail.com> wrote:
> Hi All
>
> I am developing a simple app in which i have to play with user permissions.I
> register a user(during a sign up process) with this method :-
>
> def register_user(data):
> """
> This method is used to register a new user and create a user profile.
> """
> password = hashlib.sha1(data['confirm_password']).hexdigest()
>
> new_user = User(username=data['email'], email=data['email'],
> is_staff=True
> )
> new_user.set_password(data['confirm_password'])
> # At this point i want to add user_permissions ! How can i do this ?
> new_user.user_premissions.add('can add table_name') @ Since it is a
> manytomany field
>
> How can i set permissions from this part of code ?
> new_user.save()
>
>
>
>
> new_user.first_name = data['title']
> new_user.email = data['email']
> new_user.is_locked = True
> new_user.save()
> new_user.user_permissions('Can add department')
>
> profile = new_user.profile
> key = uuid.uuid4().__str__()
> profile.key = key
> profile.save()
> if new_user and profile:
> return new_user
> else:
> return False
>
> Thanks in advance
>
>
>
Permissions are just objects, fetch them from the database and add
them to the M2M:
>>> from django.contrib.auth.models import Permission, User
>>> user = User.objects.get(id=1)
>>> perm = Permission.objects.get(codename='add_user')
>>> user.user_permissions.add(perm)
>>> user.user_permissions.all()
[<Permission: auth | user | Can add user>]
>>> user.has_perm('auth.add_user')
True
Cheers
Tom
--
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