File: signals.py

package info (click to toggle)
python-django-pgschemas 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 848 kB
  • sloc: python: 3,887; makefile: 33; sh: 10; sql: 2
file content (34 lines) | stat: -rw-r--r-- 1,186 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from typing import Any

from django.db.models.signals import pre_delete
from django.dispatch import Signal, receiver

from django_pgschemas.utils import get_tenant_model, schema_exists

schema_activate = Signal()
schema_activate.__doc__ = "Sent after a schema has been activated"

dynamic_tenant_needs_sync = Signal()
dynamic_tenant_needs_sync.__doc__ = (
    "Sent when a schema from a dynamic tenant needs to have migrations applied"
)

dynamic_tenant_post_sync = Signal()
dynamic_tenant_post_sync.__doc__ = (
    "Sent after a tenant has been saved, its schema created and all migrations applied"
)

dynamic_tenant_pre_drop = Signal()
dynamic_tenant_pre_drop.__doc__ = "Sent when a schema from a dynamic tenant is about to be dropped"


@receiver(pre_delete)
def tenant_delete_callback(sender: Any, instance: Any, **kwargs: object) -> None:
    TenantModel = get_tenant_model()
    if TenantModel is None:
        return
    if not isinstance(instance, TenantModel):
        return
    if instance.auto_drop_schema and schema_exists(instance.schema_name):
        dynamic_tenant_pre_drop.send(sender=TenantModel, tenant=instance.serializable_fields())
        instance.drop_schema()