2020-09-02 22:04:12 +00:00
|
|
|
"""passbook outpost signals"""
|
|
|
|
from django.db.models import Model
|
2020-10-14 16:41:16 +00:00
|
|
|
from django.db.models.signals import post_save, pre_delete
|
2020-09-02 22:04:12 +00:00
|
|
|
from django.dispatch import receiver
|
|
|
|
from structlog import get_logger
|
|
|
|
|
2020-09-13 12:29:40 +00:00
|
|
|
from passbook.lib.utils.reflection import class_to_path
|
2020-10-14 16:41:16 +00:00
|
|
|
from passbook.outposts.models import Outpost
|
2020-10-16 20:26:47 +00:00
|
|
|
from passbook.outposts.tasks import outpost_post_save, outpost_pre_delete
|
2020-09-02 22:04:12 +00:00
|
|
|
|
|
|
|
LOGGER = get_logger()
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save)
|
|
|
|
# pylint: disable=unused-argument
|
2020-09-13 12:29:40 +00:00
|
|
|
def post_save_update(sender, instance: Model, **_):
|
2020-10-14 16:41:16 +00:00
|
|
|
"""If an Outpost is saved, Ensure that token is created/updated
|
2020-09-02 22:04:12 +00:00
|
|
|
|
2020-10-14 16:41:16 +00:00
|
|
|
If an OutpostModel, or a model that is somehow connected to an OutpostModel is saved,
|
|
|
|
we send a message down the relevant OutpostModels WS connection to trigger an update"""
|
2020-10-16 13:58:28 +00:00
|
|
|
if instance.__module__ == "django.db.migrations.recorder":
|
|
|
|
return
|
|
|
|
if instance.__module__ == "__fake__":
|
|
|
|
return
|
2020-10-14 16:41:16 +00:00
|
|
|
outpost_post_save.delay(class_to_path(instance.__class__), instance.pk)
|
2020-09-02 22:04:12 +00:00
|
|
|
|
|
|
|
|
2020-10-14 16:41:16 +00:00
|
|
|
@receiver(pre_delete, sender=Outpost)
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def pre_delete_cleanup(sender, instance: Outpost, **_):
|
|
|
|
"""Ensure that Outpost's user is deleted (which will delete the token through cascade)"""
|
|
|
|
instance.user.delete()
|
2020-10-16 20:26:47 +00:00
|
|
|
# To ensure that deployment is cleaned up *consistently* we call the controller, and wait
|
|
|
|
# for it to finish. We don't want to call it in this thread, as we don't have the K8s
|
|
|
|
# credentials here
|
|
|
|
outpost_pre_delete.delay(instance.pk.hex).get()
|