def save(self, *args, **kwargs):
        """
        A custom save method that handles figuring out when something is activated or deactivated.
        """
        current_activable_value = getattr(self, self.ACTIVATABLE_FIELD_NAME)
        is_active_changed = self.id is None or self.__original_activatable_value != current_activable_value
        self.__original_activatable_value = current_activable_value

        ret_val = super(BaseActivatableModel, self).save(*args, **kwargs)

        # Emit the signals for when the is_active flag is changed
        if is_active_changed:
            model_activations_changed.send(self.__class__, instance_ids=[self.id], is_active=current_activable_value)
        if self.activatable_field_updated:
            model_activations_updated.send(self.__class__, instance_ids=[self.id], is_active=current_activable_value)

        return ret_val
Exemplo n.º 2
0
    def save(self, *args, **kwargs):
        """
        A custom save method that handles figuring out when something is activated or deactivated.
        """
        current_activable_value = getattr(self, self.ACTIVATABLE_FIELD_NAME)
        is_active_changed = self.id is None or self.__original_activatable_value != current_activable_value
        self.__original_activatable_value = current_activable_value

        ret_val = super(BaseActivatableModel, self).save(*args, **kwargs)

        # Emit the signals for when the is_active flag is changed
        if is_active_changed:
            model_activations_changed.send(self.__class__,
                                           instance_ids=[self.id],
                                           is_active=current_activable_value)
        if self.activatable_field_updated:
            model_activations_updated.send(self.__class__,
                                           instance_ids=[self.id],
                                           is_active=current_activable_value)

        return ret_val
    def update(self, *args, **kwargs):
        if self.model.ACTIVATABLE_FIELD_NAME in kwargs:
            # Fetch the instances that are about to be updated if they have an activatable flag. This
            # is because their activatable flag may be changed in the subsequent update, causing us
            # to potentially lose what this original query referenced
            new_active_state_kwargs = {
                self.model.ACTIVATABLE_FIELD_NAME: kwargs.get(self.model.ACTIVATABLE_FIELD_NAME)
            }
            changed_instance_ids = list(self.exclude(**new_active_state_kwargs).values_list('id', flat=True))
            updated_instance_ids = list(self.values_list('id', flat=True))

        ret_val = super(ActivatableQuerySet, self).update(*args, **kwargs)

        if self.model.ACTIVATABLE_FIELD_NAME in kwargs and updated_instance_ids:
            # send the instances that were updated to the activation signals
            model_activations_changed.send(
                self.model, instance_ids=changed_instance_ids,
                is_active=kwargs[self.model.ACTIVATABLE_FIELD_NAME])
            model_activations_updated.send(
                self.model, instance_ids=updated_instance_ids,
                is_active=kwargs[self.model.ACTIVATABLE_FIELD_NAME])
        return ret_val
Exemplo n.º 4
0
    def update(self, *args, **kwargs):
        if self.model.ACTIVATABLE_FIELD_NAME in kwargs:
            # Fetch the instances that are about to be updated if they have an activatable flag. This
            # is because their activatable flag may be changed in the subsequent update, causing us
            # to potentially lose what this original query referenced
            new_active_state_kwargs = {
                self.model.ACTIVATABLE_FIELD_NAME: kwargs.get(self.model.ACTIVATABLE_FIELD_NAME)
            }
            changed_instance_ids = list(self.exclude(**new_active_state_kwargs).values_list('id', flat=True))
            updated_instance_ids = list(self.values_list('id', flat=True))

        ret_val = super(ActivatableQuerySet, self).update(*args, **kwargs)

        if self.model.ACTIVATABLE_FIELD_NAME in kwargs and updated_instance_ids:
            # send the instances that were updated to the activation signals
            model_activations_changed.send(
                self.model, instance_ids=changed_instance_ids,
                is_active=kwargs[self.model.ACTIVATABLE_FIELD_NAME])
            model_activations_updated.send(
                self.model, instance_ids=updated_instance_ids,
                is_active=kwargs[self.model.ACTIVATABLE_FIELD_NAME])
        return ret_val