@login_required needs "request" as the first parameter.
On Wed, Oct 31, 2018 at 11:58 AM Joel Mathew <joel@joel.su> wrote:
I have a custom function to check if a user is authorized to do
certain functions:
@login_required
def checkpermission(request, permiss):
username = request.user.username
print(f"username is {username}")
print(f"User.objects.filter() is {User.objects.filter()}")
userobj = User.objects.get(username=username)
print(dir(userobj))
print(userobj.user_permissions.all())
print(Permission.objects.filter(group__user=userobj))
perms = getpermissions(userobj)
for perm in perms:
p = perm.name.lower().replace(" ", "_")
if permiss == p:
print("Has permission: %s" % p)
return True
print("No")
return False
def getpermissions(userobj):
if userobj.is_superuser:
return Permission.objects.all()
return userobj.user_permissions.all() |
Permission.objects.filter(group__user=userobj)
The problem arises if I write the previous function with decorator:
@login_required
def getpermissions(userobj):
if userobj.is_superuser:
return Permission.objects.all()
return userobj.user_permissions.all() |
Permission.objects.filter(group__user=userobj)
In this case, I get the error:
2018-10-31 21:26:51,239 django.request ERROR Internal Server Error:
/clinic/joelent/doctors
Traceback (most recent call last):
File "/home/joel/.local/lib/python3.6/site-packages/django/core/handlers/exception.py",
line 34, in inner
response = get_response(request)
File "/home/joel/.local/lib/python3.6/site-packages/django/core/handlers/base.py",
line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/joel/.local/lib/python3.6/site-packages/django/core/handlers/base.py",
line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/joel/.local/lib/python3.6/site-packages/django/contrib/auth/decorators.py",
line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/joel/myappointments/clinic/views.py", line 2797, in
clinicdoctorlist
if checkpermission(request, "can_change_doctor") and
HasMembership(request, cliniclabel):
File "/home/joel/.local/lib/python3.6/site-packages/django/contrib/auth/decorators.py",
line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/joel/myappointments/clinic/views.py", line 2970, in
checkpermission
perms = getpermissions(userobj)
File "/home/joel/.local/lib/python3.6/site-packages/django/contrib/auth/decorators.py",
line 20, in _wrapped_view
if test_func(request.user):
AttributeError: 'User' object has no attribute 'user'
Why would this be happening?
Sincerely yours,
Joel G Mathew
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAA%3Diw_94fELQWM873_y6w%2B0S8vCA2xtO44sG45dqMp4DSC8qeQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAD4ANxVoNhmE78-n0SQFn1QFFz1O9-wXBs9p5G8jfsiXa190Kw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment