Example #1
0
    def delete(self, *args, **kwargs):
        """
        When a custom field choice is deleted, remove references to in custom field data
        """
        if self.field.default:
            # Cannot delete the choice if it is the default value.
            if self.field.type == CustomFieldTypeChoices.TYPE_SELECT and self.field.default == self.value:
                raise models.ProtectedError(
                    self, "Cannot delete this choice because it is the default value for the field."
                )
            elif self.value in self.field.default:
                raise models.ProtectedError(
                    self, "Cannot delete this choice because it is one of the default values for the field."
                )

        if self.field.type == CustomFieldTypeChoices.TYPE_SELECT:
            # Check if this value is in active use in a select field
            for ct in self.field.content_types.all():
                model = ct.model_class()
                if model.objects.filter(**{f"_custom_field_data__{self.field.name}": self.value}).exists():
                    raise models.ProtectedError(self, "Cannot delete this choice because it is in active use.")

        else:
            # Check if this value is in active use in a multi-select field
            for ct in self.field.content_types.all():
                model = ct.model_class()
                if model.objects.filter(**{f"_custom_field_data__{self.field.name}__contains": self.value}).exists():
                    raise models.ProtectedError(self, "Cannot delete this choice because it is in active use.")

        super().delete(*args, **kwargs)
Example #2
0
 def delete(self, *args, **kwargs):
     """Delete this element, or raise ProtectedError if its in use."""
     if not self.deletable:
         raise models.ProtectedError(
             "Element '{0}' is in use and cannot be deleted.".format(
                 self.name), list(self.environments.all()))
     return super(Element, self).delete(*args, **kwargs)
Example #3
0
def _cascade_soft_delete(inst_or_qs, using, keep_parents=False):
    """
    Return collector instance that has marked ArchiveMixin instances for
    archive (i.e. update) instead of actual delete.
    Arguments:
        inst_or_qs (models.Model or models.QuerySet): the instance(s) that
            are to be deleted.
        using (db connection/router): the db to delete from.
        keep_parents (bool): defaults to False.  Determine if cascade is true.
    Returns:
        models.deletion.Collector: this is a standard Collector instance but
            the ArchiveMixin instances are in the fields for update list.
    """
    if not isinstance(inst_or_qs, models.QuerySet):
        instances = [inst_or_qs]
    else:
        instances = inst_or_qs

    # The collector will iteratively crawl the relationships and
    # create a list of models and instances that are connected to
    # this instance.
    collector = NestedObjects(using=using)
    collector.collect(instances, keep_parents=keep_parents)
    if collector.protected:
        raise models.ProtectedError("Delete protected", collector.protected)
    collector.sort()

    return collector
Example #4
0
def prevent_non_empty_project_group_deletion(sender, instance, **kwargs):
    related_projects = Project.objects.filter(project_groups=instance)

    if related_projects.exists():
        raise models.ProtectedError(
            "Cannot delete some instances of model 'ProjectGroup' because "
            "they have connected 'Projects'", related_projects)
Example #5
0
    def disable(self):
        if self.disabled:
            raise models.ProtectedError(
                gettext('This question is already disabled.'),
                [self],
            )

        if PollFormLineCondition.objects.filter(source=self).exists():
            raise models.ProtectedError(
                gettext(
                    'There is at least one other question which depends on this question.'
                ),
                [self],
            )

        self.disabled = True
        self.conditions.all().delete()
        self.save()
Example #6
0
 def delete(self, *args, **kwargs):
     """Delete this environment, or raise ProtectedError if its in use."""
     if not self.deletable:
         from moztrap.model import ProductVersion
         raise models.ProtectedError(
             "Environment '{0}' is in use and cannot be deleted.".format(
                 str(self)),
             list(ProductVersion.objects.filter(environments=self).all()))
     return super(Environment, self).delete(*args, **kwargs)
Example #7
0
 def delete(self, *args, **kwargs):
     """Delete this category, or raise ProtectedError if its in use."""
     if not self.deletable:
         raise models.ProtectedError(
             "Category '{0}' is in use and cannot be deleted.".format(
                 self.name),
             list(
                 Environment.objects.filter(elements__category=self).all()))
     return super(Category, self).delete(*args, **kwargs)
Example #8
0
  def delete( self ):
    if not self.can_delete:
      raise models.ProtectedError( 'Structure not Deleatable', self )

    subclass = self.subclass

    if self == subclass:
      super().delete()
    else:
      subclass.delete()
Example #9
0
def prevent_deletion_of_instances_with_connected_backups(
        sender, instance, **kwargs):
    from nodeconductor.backup.models import Backup
    ct = ContentType.objects.get_for_model(instance._meta.model)
    connected_backups = Backup.objects.filter(content_type=ct,
                                              object_id=instance.id)

    if connected_backups.exists():
        raise models.ProtectedError(
            "Cannot delete instance because it has connected backups.",
            connected_backups)
Example #10
0
    def delete(self, *args, **kwargs):
        if not self.disabled and PollFormLineCondition.objects.filter(
                source=self).exists():
            raise models.ProtectedError(
                gettext(
                    'There is at least one other question which depends on this question.'
                ),
                [self],
            )

        super().delete(*args, **kwargs)
Example #11
0
def CREME_REPLACE(collector, field, sub_objs, using):
    """Can be used as value for <on_delete> attribute of ForeignKeys.
    It's equivalent to regular Django's PROTECT, but creme_config
    will propose to replace the deleted value by another instance.
    """
    raise models.ProtectedError(
        _('Cannot delete some instances of model «{model}» because they are '
          'referenced through a protected foreign key: «{related} - {field}»').
        format(
            model=field.remote_field.model.__name__,
            related=type(sub_objs[0])._meta.verbose_name,
            field=field.verbose_name,
        ), sub_objs)
Example #12
0
def prevent_updating_of_frozen_page_and_data(sender, instance, **kwargs):
    try:
        obj = sender.objects.get(pk=instance.pk)
    except sender.DoesNotExist:
        pass  # Initial save -- do nothing
    else:
        if (
            (obj.frozen_page != instance.frozen_page)
            or (obj.url != instance.url)
            or (obj.freeze_time != instance.freeze_time)
            or (obj.freeze_software != instance.freeze_software)
        ):
            raise models.ProtectedError(
                "Only notes can be changed after initial creation."
            )
Example #13
0
    def delete(self, *args, **kwargs):
        from ..utils import SectionTree

        section_id = self.id

        for node in SectionTree(self.pform):
            if node.is_section and node.id == section_id:
                if not node.has_line:
                    break

                raise models.ProtectedError(
                    gettext('There is at least one question in this section.'),
                    [self],
                )

        super().delete(*args, **kwargs)
Example #14
0
 def delete(self, *args, **kwargs):
     if not self.can_delete():
         raise models.ProtectedError(
             _("Can't delete. This object is protected."), [self])
     super(ContactGroup, self).delete(*args, **kwargs)
Example #15
0
  def delete( self ):
    if not self.can_delete:
      raise models.ProtectedError( 'Structure not Deleteable', self )

    super().delete()
Example #16
0
 def delete(self):
     raise models.ProtectedError('Can not delete JobLog entries', self)
Example #17
0
 def delete(self, *args, **kwargs):
     if self.current_student_count and not kwargs.get('force'):
         raise models.ProtectedError(
             "Cannot delete section with enrolled students", self)
     kwargs.pop('force', None)
     super().delete(*args, **kwargs)
Example #18
0
def prevent_deletes(sender, instance, **kwargs):
    raise models.ProtectedError("Not allowed to delete.", [instance])