Entry 780

Some View function

   

Submitted by anonymous on May 16, 2008 at 12:17 p.m.
Language: Python. Code size: 2.3 KB.

def __call__(self, request, **kwargs):
        """Checks wether or not the request should be allowed.
        
        If the authentication matched, the view function of this wrapper is called (and its
        result returned).
        There will be an additional keyword-argument to the call, person or organisation,
        poiniting to the object used during validation.         
        Otherwise a HttpResponseRedirect is returned with an error message.
        The Url used is that of the contrib.auth middleware.
        
        If neither ``auth_person_id`` nor ``auth_organisaition_id`` are present, the view function
        is kept untouched and simply called.
        
        Note this class will *always* redirect the request if the user is not logged in.
        """        
        # Fastpath : disallow anonymous user
        if request.user.is_anonymous():
            return self.handle_unauthorized(request, "Sie müssen sich anmelden, um diese Seite zu sehen.")
        is_authed = True
        
        for model_class, retriever in KEYWORD_LOOKUPS :   
            instance_name = model_class.__name__.lower()         
            keyword = 'auth_%s_id' % instance_name
            if kwargs.has_key(keyword):
                print "Getting %s with id %d" % (model_class, int(kwargs[keyword]))
                obj = get_object_or_404(model_class, pk = kwargs[keyword])
                auth_obj = retriever(obj)
                is_authed = auth.match_admin_level(request.user, auth_obj, **self.auth_options)
                if not is_authed:
                    msg = "Sie haben nicht die Berechtigung, diese %s zu bearbeiten" % model_class.__name__
                    return self.handle_unauthorized(request, msg)
                else:
                    kwargs[instance_name] = obj
                    del kwargs[keyword]
                    auth_arg = getattr(retriever, 'put_auth_object', None)
                    if auth_arg:
                        kwargs[auth_arg] = auth_obj
                    break                                      
        try:
            return self.view_func(request, **kwargs)
        except DynamicAuthorizationError, dae:
            print dae.message
            return self.handle_unauthorized(request, msg=dae.message)

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).