• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

safe-global / safe-config-service / 7528317545

11 Jan 2024 03:28PM UTC coverage: 99.757% (-0.02%) from 99.772%
7528317545

push

github

web-flow
Invalidate safe apps for removed chains (#1020)

- Decouples `post_delete` signal from the affected flow, as it remains as it is.
- Change the handling of `post_save` signal to handle `pre_save` signal, to have access to the instance's `Chain` list before the update is made.
- Both the `Chain` items related to the `SafeApp` before and after the update are stored in a `Set` to avoid repetitions. Hooks are dispatched for all the `Chain` items in the set.

4923 of 4935 relevant lines covered (99.76%)

1.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.31
/src/chains/signals.py
1
import logging
1✔
2
from typing import Any
1✔
3

4
from django.conf import settings
1✔
5
from django.db.models.signals import m2m_changed, post_delete, post_save, pre_delete
1✔
6
from django.dispatch import receiver
1✔
7

8
from clients.safe_client_gateway import HookEvent, flush, hook_event
1✔
9

10
from .models import Chain, Feature, GasPrice, Wallet
1✔
11

12
logger = logging.getLogger(__name__)
1✔
13

14

15
@receiver(post_save, sender=Chain)
1✔
16
@receiver(post_delete, sender=Chain)
1✔
17
def on_chain_update(sender: Chain, instance: Chain, **kwargs: Any) -> None:
1✔
18
    logger.info("Chain update. Triggering CGW webhook")
1✔
19
    if settings.FF_HOOK_EVENTS:
1✔
20
        hook_event(HookEvent(type=HookEvent.Type.CHAIN_UPDATE, chain_id=instance.id))
1✔
21
    else:
×
22
        flush()
1✔
23

24

25
@receiver(post_save, sender=GasPrice)
1✔
26
@receiver(post_delete, sender=GasPrice)
1✔
27
def on_gas_price_update(sender: GasPrice, instance: GasPrice, **kwargs: Any) -> None:
1✔
28
    logger.info("GasPrice update. Triggering CGW webhook")
1✔
29
    if settings.FF_HOOK_EVENTS:
1✔
30
        hook_event(
1✔
31
            HookEvent(type=HookEvent.Type.CHAIN_UPDATE, chain_id=instance.chain.id)
1✔
32
        )
33
    else:
×
34
        flush()
1✔
35

36

37
# pre_delete is used because on pre_delete the model still has chains
38
# which is not the case on post_delete
39
@receiver(post_save, sender=Feature)
1✔
40
@receiver(pre_delete, sender=Feature)
1✔
41
def on_feature_changed(sender: Feature, instance: Feature, **kwargs: Any) -> None:
1✔
42
    logger.info("Feature update. Triggering CGW webhook")
1✔
43
    if settings.FF_HOOK_EVENTS:
1✔
44
        # A Feature change affects all the chains that have this feature
45
        for chain in instance.chains.all():
1✔
46
            hook_event(HookEvent(type=HookEvent.Type.CHAIN_UPDATE, chain_id=chain.id))
1✔
47
    else:
×
48
        flush()
1✔
49

50

51
@receiver(m2m_changed, sender=Feature.chains.through)
1✔
52
def on_feature_chains_changed(
1✔
53
    sender: Feature, instance: Feature, action: str, pk_set: set[int], **kwargs: Any
1✔
54
) -> None:
1✔
55
    logger.info("FeatureChains update. Triggering CGW webhook")
1✔
56
    if action == "post_add" or action == "post_remove":
1✔
57
        if settings.FF_HOOK_EVENTS:
1✔
58
            for chain_id in pk_set:
1✔
59
                hook_event(
1✔
60
                    HookEvent(type=HookEvent.Type.CHAIN_UPDATE, chain_id=chain_id)
1✔
61
                )
62
        else:
63
            flush()
1✔
64

65

66
# pre_delete is used because on pre_delete the model still has chains
67
# which is not the case on post_delete
68
@receiver(post_save, sender=Wallet)
1✔
69
@receiver(pre_delete, sender=Wallet)
1✔
70
def on_wallet_changed(sender: Wallet, instance: Wallet, **kwargs: Any) -> None:
1✔
71
    logger.info("Wallet update. Triggering CGW webhook")
1✔
72
    if settings.FF_HOOK_EVENTS:
1✔
73
        # A Wallet change affects all the chains that have this wallet
74
        for chain in instance.chains.all():
1✔
75
            hook_event(HookEvent(type=HookEvent.Type.CHAIN_UPDATE, chain_id=chain.id))
1✔
76
    else:
77
        flush()
1✔
78

79

80
@receiver(m2m_changed, sender=Wallet.chains.through)
1✔
81
def on_wallet_chains_changed(
1✔
82
    sender: Wallet, instance: Wallet, action: str, pk_set: set[int], **kwargs: Any
1✔
83
) -> None:
1✔
84
    logger.info("WalletChains update. Triggering CGW webhook")
1✔
85
    if action == "post_add" or action == "post_remove":
1✔
86
        if settings.FF_HOOK_EVENTS:
1✔
87
            for chain_id in pk_set:
1✔
88
                hook_event(
1✔
89
                    HookEvent(type=HookEvent.Type.CHAIN_UPDATE, chain_id=chain_id)
1✔
90
                )
91
        else:
92
            flush()
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc