Пример #1
0
def deleteinstance(djp, force_redirect = False):
    '''Delete an instance from database'''
    instance = djp.instance
    view    = djp.view
    request = djp.request
    
    curr    = request.environ.get('HTTP_REFERER')
    next    = get_next(request)
    if next:
        next = request.build_absolute_uri(next)
    next = next or curr
        
    bid     = view.appmodel.remove_object(instance)
    msg     = 'Successfully deleted %s' % instance
    if request.is_ajax():
        if next == curr and bid and not force_redirect:
            return jremove('#%s' % bid)
        else:
            messages.info(request,msg)
            return jredirect(next)
    else:
        messages.info(request,msg)
        next = next or curr
        return http.HttpResponseRedirect(next)
Пример #2
0
    def post_response(self, djp):
        '''Handle the post view. This function checks the request.POST dictionary
for a post_view_key specified in HTML_CLASSES (by default 'xhr')
        
If it finds this key, the post view is an AJAX-JSON request and the key VALUE represents
 a function responsible for handling the response.
        
These ajax enabled post view functions must start with the prefix
ajax__ followed by the VALUE of the post_view_key.

Example:
    so lets say we find in the POST dictionary
    
    {
    ...
    'xhr': 'change_parameter',
    ...
    }

Than there should be a function called   'ajax__change_parameter'
which handle the response'''
        request   = djp.request
        post      = request.POST
        is_ajax   = request.is_ajax()
        ajax_key  = False
        mimetype  = None
        http      = djp.http
        
        if is_ajax:
            mimetype = 'application/javascript'
            params   = dict(post.items())
            prefix   = params.get('_prefixed',None)
            ajax_key = params.get(djp.css.post_view_key, None)
            if ajax_key:
                if prefix and prefix in ajax_key:
                    ajax_key = ajax_key[len(prefix)+1:]
                ajax_key = ajax_key.replace('-','_').lower()
            
        # If ajax_key is defined, it means this is a AJAX-JSON request
        if is_ajax:
            #
            # Handle the cancel request redirect.
            # Check for next in the parameters,
            # If not there redirect to self.defaultredirect
            if ajax_key == 'cancel':
                next = params.get('next',None)
                next = self.defaultredirect(djp.request, next = url, **djp.kwargs)
                res  = jredirect(next)
            else:
                ajax_view_function = None
                if ajax_key:
                    ajax_view = 'ajax__%s' % ajax_key
                    ajax_view_function  = getattr(self,str(ajax_view),None)
                
                # No post view function found. Let's try the default ajax post view
                if not ajax_view_function:
                    ajax_view_function = self.default_post;
            
                try:
                    res  = ajax_view_function(djp)
                except Exception as e:
                    # we got an error. If in debug mode send a JSON response with
                    # the error message back to javascript.
                    if djp.settings.DEBUG:
                        exc_info = sys.exc_info()
                        stack_trace = '\n'.join(traceback.format_exception(*exc_info))
                        logerror(self.logger, request, exc_info)
                        res = jservererror(stack_trace, url = djp.url)
                    else:
                        raise e
            
            return http.HttpResponse(res.dumps(),mimetype)
        #
        # Otherwise it is the default POST response
        else:
            return self.default_post(djp)
Пример #3
0
def saveform(djp, editing = False, force_redirect = False):
    '''Comprehensive save method for forms'''
    view = djp.view
    request = djp.request
    http = djp.http
    is_ajax = request.is_ajax()
    POST = request.POST
    GET = request.GET
    curr = request.environ.get('HTTP_REFERER')
    next = get_next(request)
    f = view.get_form(djp)
    
    if POST.has_key("_cancel"):
        redirect_url = next
        if not redirect_url:
            if djp.instance:
                redirect_url = view.appmodel.viewurl(request,djp.instance)
            if not redirect_url:
                redirect_url = view.appmodel.searchurl(request) or '/'

        if is_ajax:
            return jredirect(url = redirect_url)
        else:
            return http.HttpResponseRedirect(redirect_url)
    
    if f.is_valid():
        try:
            editing  = editing if not POST.has_key('_save_as_new') else False
            instance = view.save(request, f)
            smsg     = getattr(view,'success_message',success_message)
            msg      = smsg(instance, 'changed' if editing else 'added')
            f.add_message(request, msg)
        except Exception, e:
            exc_info = sys.exc_info()
            logger.error('Form Error: %s' % request.path,
                         exc_info=exc_info,
                         extra={'request':request})
            f.add_message(request,e,error=True)
            if is_ajax:
                return f.json_errors()
            elif next:
                return http.HttpResponseRedirect(next)
            else:
                return view.handle_response(djp)
        
        if POST.has_key('_save_and_continue'):
            if is_ajax:
                return f.json_message()
            else:
                redirect_url = curr
        else:
            redirect_url = view.defaultredirect(request,
                                                next = next,
                                                instance = instance)
            
            # not forcing redirect. Check if we can send a json message
            if not force_redirect:
                if redirect_url == curr and is_ajax:
                    return f.json_message()
            
        # We are Redirecting
        if is_ajax:
            f.force_message(request)
            return jredirect(url = redirect_url)
        else:
            return http.HttpResponseRedirect(redirect_url)
Пример #4
0
 def redirect(self, url):
     if self.request.is_ajax():
         return jredirect(url = url)
     else:
         return self.http.HttpResponseRedirect(url)