treeeditor/admin.py
def _toggle_boolean(self, request):
        """
        Handle an AJAX toggle_boolean request
        """
        try:
            item_id = int(request.POST.get('item_id', None))
            attr = str(request.POST.get('attr', None))
        except:
            return HttpResponseBadRequest("Malformed request")

        if not request.user.is_staff:
            logging.warning("Denied AJAX request by non-staff %s to toggle boolean %s for object #%s", request.user,
                            attr, item_id)
            return HttpResponseForbidden("You do not have permission to access this object")

        self._collect_editable_booleans()

        if not self._ajax_editable_booleans.has_key(attr):
            return HttpResponseBadRequest("not a valid attribute %s" % attr)

        try:
            obj = self.model._default_manager.get(pk=item_id)
        except self.model.DoesNotExist:
            return HttpResponseNotFound("Object does not exist")

        can_change = False

        if hasattr(obj, "user_can") and obj.user_can(request.user, change_page=True):
            # Was added in c7f04dfb5d, but I've no idea what user_can is about.
            can_change = True
        else:
            can_change = self.has_change_permission(request, obj=obj)

        if not can_change:
            logging.warning("Denied AJAX request by %s to toggle boolean %s for object %s", request.user, attr, item_id)
            return HttpResponseForbidden("You do not have permission to access this object")

        logging.info("Processing request by %s to toggle %s on %s", request.user, attr, obj)

        try:
            before_data = self._ajax_editable_booleans[attr](self, obj)

            setattr(obj, attr, not getattr(obj, attr))
            obj.save()

            self._refresh_changelist_caches() # ???: Perhaps better a post_save signal?

            # Construct html snippets to send back to client for status update
            data = self._ajax_editable_booleans[attr](self, obj)

        except Exception:#, e:
            logging.exception("Unhandled exception while toggling %s on %s", attr, obj)
            return HttpResponseServerError("Unable to toggle %s on %s" % (attr, obj))

        # Weed out unchanged cells to keep the updates small. This assumes
        # that the order a possible get_descendents() returns does not change
        # before and after toggling this attribute. Unlikely, but still...
        d = []
        for a, b in zip(before_data, data):
            if a != b:
                d.append(b)

        # TODO: Shorter: [ y for x,y in zip(a,b) if x!=y ]
        return HttpResponse(json.dumps(d), mimetype="application/json")
def _toggle_boolean(self, request):
        """
        Handle an AJAX toggle_boolean request
        """
        try:
            item_id = int(request.POST.get('item_id', None))
            attr = str(request.POST.get('attr', None))
        except:
            return HttpResponseBadRequest("Malformed request")

        if not request.user.is_staff:
            logging.warning("Denied AJAX request by non-staff %s to toggle boolean %s for object #%s", request.user,
                            attr, item_id)
            return HttpResponseForbidden("You do not have permission to access this object")

        self._collect_editable_booleans()

        if not self._ajax_editable_booleans.has_key(attr):
            return HttpResponseBadRequest("not a valid attribute %s" % attr)

        try:
            obj = self.model._default_manager.get(pk=item_id)
        except self.model.DoesNotExist:
            return HttpResponseNotFound("Object does not exist")

        can_change = False

        if hasattr(obj, "user_can") and obj.user_can(request.user, change_page=True):
            # Was added in c7f04dfb5d, but I've no idea what user_can is about.
            can_change = True
        else:
            can_change = self.has_change_permission(request, obj=obj)

        if not can_change:
            logging.warning("Denied AJAX request by %s to toggle boolean %s for object %s", request.user, attr, item_id)
            return HttpResponseForbidden("You do not have permission to access this object")

        logging.info("Processing request by %s to toggle %s on %s", request.user, attr, obj)

        try:
            before_data = self._ajax_editable_booleans[attr](self, obj)

            setattr(obj, attr, not getattr(obj, attr))
            obj.save()

            self._refresh_changelist_caches() # ???: Perhaps better a post_save signal?

            # Construct html snippets to send back to client for status update
            data = self._ajax_editable_booleans[attr](self, obj)

        except Exception:#, e:
            logging.exception("Unhandled exception while toggling %s on %s", attr, obj)
            return HttpResponseServerError("Unable to toggle %s on %s" % (attr, obj))

        # Weed out unchanged cells to keep the updates small. This assumes
        # that the order a possible get_descendents() returns does not change
        # before and after toggling this attribute. Unlikely, but still...
        d = []
        for a, b in zip(before_data, data):
            if a != b:
                d.append(b)

        # TODO: Shorter: [ y for x,y in zip(a,b) if x!=y ]
        return HttpResponse(json.dumps(d), mimetype="application/json")
def _toggle_boolean ( self , request ) : try : item_id = int ( request . POST . get ( 'item_id' , None ) ) attr = str ( request . POST . get ( 'attr' , None ) ) except : return HttpResponseBadRequest ( "Malformed request" ) if not request . user . is_staff : logging . warning ( "Denied AJAX request by non-staff %s to toggle boolean %s for object #%s" , request . user , attr , item_id ) return HttpResponseForbidden ( "You do not have permission to access this object" ) self . _collect_editable_booleans ( ) if not self . _ajax_editable_booleans . has_key ( attr ) : return HttpResponseBadRequest ( "not a valid attribute %s" % attr ) try : obj = self . model . _default_manager . get ( pk = item_id ) except self . model . DoesNotExist : return HttpResponseNotFound ( "Object does not exist" ) can_change = False if hasattr ( obj , "user_can" ) and obj . user_can ( request . user , change_page = True ) : # Was added in c7f04dfb5d, but I've no idea what user_can is about. can_change = True else : can_change = self . has_change_permission ( request , obj = obj ) if not can_change : logging . warning ( "Denied AJAX request by %s to toggle boolean %s for object %s" , request . user , attr , item_id ) return HttpResponseForbidden ( "You do not have permission to access this object" ) logging . info ( "Processing request by %s to toggle %s on %s" , request . user , attr , obj ) try : before_data = self . _ajax_editable_booleans [ attr ] ( self , obj ) setattr ( obj , attr , not getattr ( obj , attr ) ) obj . save ( ) self . _refresh_changelist_caches ( ) # ???: Perhaps better a post_save signal? # Construct html snippets to send back to client for status update data = self . _ajax_editable_booleans [ attr ] ( self , obj ) except Exception : #, e: logging . exception ( "Unhandled exception while toggling %s on %s" , attr , obj ) return HttpResponseServerError ( "Unable to toggle %s on %s" % ( attr , obj ) ) # Weed out unchanged cells to keep the updates small. This assumes # that the order a possible get_descendents() returns does not change # before and after toggling this attribute. Unlikely, but still... d = [ ] for a , b in zip ( before_data , data ) : if a != b : d . append ( b ) # TODO: Shorter: [ y for x,y in zip(a,b) if x!=y ] return HttpResponse ( json . dumps ( d ) , mimetype = "application/json" )
Handle an AJAX toggle_boolean request
Handle an AJAX toggle_boolean request