manager for deleted objects

Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
Jens Langhammer
2024-04-23 23:05:10 +02:00
parent a4853a1e09
commit e4c11a5284
2 changed files with 14 additions and 3 deletions

View File

@ -54,7 +54,7 @@ class InheritanceForeignKey(models.ForeignKey):
forward_related_accessor_class = InheritanceForwardManyToOneDescriptor
class SoftDeleteQuerySet(models.query.QuerySet):
class SoftDeleteQuerySet(models.QuerySet):
def delete(self):
for obj in self.all():
@ -70,6 +70,12 @@ class SoftDeleteManager(models.Manager):
return SoftDeleteQuerySet(self.model, using=self._db).filter(deleted_at__isnull=True)
class DeletedSoftDeleteManager(models.Manager):
def get_queryset(self):
return super().get_queryset().exclude(deleted_at__isnull=True)
class SoftDeleteModel(models.Model):
"""Model which doesn't fully delete itself, but rather saved the delete status
so cleanup events can run."""
@ -77,6 +83,7 @@ class SoftDeleteModel(models.Model):
deleted_at = models.DateTimeField(blank=True, null=True)
objects = SoftDeleteManager()
deleted = DeletedSoftDeleteManager()
class Meta:
abstract = True

View File

@ -132,7 +132,11 @@ def outpost_controller_all():
def outpost_controller(self: SystemTask, outpost_pk: str, action: str = "up"):
"""Create/update/monitor/delete the deployment of an Outpost"""
logs = []
outpost: Outpost = Outpost.objects.filter(pk=outpost_pk).first()
outpost: Outpost = None
if action == "up":
outpost = Outpost.objects.filter(pk=outpost_pk).first()
elif action == "down":
outpost = Outpost.deleted.filter(pk=outpost_pk).first()
if not outpost:
LOGGER.warning("No outpost")
return
@ -150,7 +154,7 @@ def outpost_controller(self: SystemTask, outpost_pk: str, action: str = "up"):
else:
self.set_status(TaskStatus.SUCCESSFUL, *logs)
finally:
if action == "down":
if outpost.deleted_at:
outpost.force_delete()