Hey all,
I'm trying to implement a function similar to get_object_or_404. I'm trying
to check permissions for a user, and if those permissions are met, then I
want them to be sent to a page explaining what's going on. I found the
exception PermissionDenied, but I can't find a way to do a custom permission
denied page (would this even be the right thing to do?)
Is there a way to do a raise Http200('<html code here, or template, or what
have you') for this sort of reroute? Here's some code to give you all
context:
dictToSend = {}
def get_membership(user, resource, required_roles=('O','U','A'),
request=None):
"""
Get the membership object of this lock for this user.
Check if it exists, if it doesn't, then raise error.
If the role isn't enough, then raise error.
"""
try:
return LockMembership.objects.get(user=user, lock=resource,
role__in=required_roles)
except ObjectDoesNotExist:
dictToSend['user'] = user
dictToSend['resource'] = resource
#I wish this was right... can't do return since it would return to
the function calling it, and not hijacking the reponse sending the user to
where I want them to go
#raise/return render_to_response('locks/bad_permissions.html',
dictToSend, context_instance=RequestContext(request))
#Or this?
#raise PermissionDenied()
@login_required
def favor_resource(request, resource_id, new_favor):
resource = get_object_or_404(Lock, pk=resource_id)
#above function called here... if they don't have permissions, then
hijack the response and send them to a "You can't access this" page.
membership = get_membership(request.user, resource, ('A',), request)
if membership and (new_favor == "True" or new_favor == "False"):
membership.is_favorite=(new_favor == "True")
membership.save()
return HttpResponseRedirect(reverse(view_resource, args=[resource.id]))
Ideas?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---