137 lines
5.5 KiB
Python
137 lines
5.5 KiB
Python
from django.db.models.signals import pre_save, post_save, post_delete
|
|
from django.dispatch import receiver
|
|
from custom_objects.models import CustomRecord
|
|
from crm_core.models import Account, Contact, Lead, Opportunity, Activity
|
|
from outreach.models import Campaign
|
|
from custom_objects.formula_engine import evaluate_all_formulas_for_instance
|
|
from custom_objects.rollup_engine import recalculate_rollups_for_record, should_recalculate_child_rollups
|
|
|
|
|
|
@receiver(pre_save, sender=Opportunity)
|
|
@receiver(pre_save, sender=Contact)
|
|
@receiver(pre_save, sender=Activity)
|
|
@receiver(pre_save, sender=CustomRecord)
|
|
def capture_pre_save_rollup_state(sender, instance, **kwargs):
|
|
"""
|
|
Captures snapshot of fields before saving to enable early exit optimization on post_save.
|
|
"""
|
|
if instance.pk:
|
|
try:
|
|
orig = sender.objects.get(pk=instance.pk)
|
|
state = {}
|
|
# Capture standard model attributes
|
|
for f in instance._meta.fields:
|
|
state[f.name] = getattr(orig, f.name, None)
|
|
# Capture data attributes if CustomRecord
|
|
if hasattr(orig, 'data') and isinstance(orig.data, dict):
|
|
for k, v in orig.data.items():
|
|
state[k] = v
|
|
# Capture custom_fields if present
|
|
if hasattr(orig, 'custom_fields') and isinstance(orig.custom_fields, dict):
|
|
for k, v in orig.custom_fields.items():
|
|
state[k] = v
|
|
instance._rollup_old_state = state
|
|
except Exception:
|
|
instance._rollup_old_state = None
|
|
|
|
|
|
@receiver(pre_save, sender=Account)
|
|
@receiver(pre_save, sender=Contact)
|
|
@receiver(pre_save, sender=Lead)
|
|
@receiver(pre_save, sender=Opportunity)
|
|
@receiver(pre_save, sender=Activity)
|
|
@receiver(pre_save, sender=Campaign)
|
|
@receiver(pre_save, sender=CustomRecord)
|
|
def evaluate_record_formulas(sender, instance, **kwargs):
|
|
evaluate_all_formulas_for_instance(instance)
|
|
|
|
|
|
# Roll-up summary triggers on child objects
|
|
@receiver(post_save, sender=Opportunity)
|
|
def trigger_opportunity_post_save(sender, instance, created, **kwargs):
|
|
if not should_recalculate_child_rollups('Opportunity', instance, created):
|
|
return
|
|
if instance.account_id:
|
|
recalculate_rollups_for_record('Account', instance.account_id)
|
|
if instance.primary_contact_id:
|
|
recalculate_rollups_for_record('Contact', instance.primary_contact_id)
|
|
|
|
|
|
@receiver(post_delete, sender=Opportunity)
|
|
def trigger_opportunity_post_delete(sender, instance, **kwargs):
|
|
if instance.account_id:
|
|
recalculate_rollups_for_record('Account', instance.account_id)
|
|
if instance.primary_contact_id:
|
|
recalculate_rollups_for_record('Contact', instance.primary_contact_id)
|
|
|
|
|
|
@receiver(post_save, sender=Contact)
|
|
def trigger_contact_post_save(sender, instance, created, **kwargs):
|
|
if not should_recalculate_child_rollups('Contact', instance, created):
|
|
return
|
|
if instance.account_id:
|
|
recalculate_rollups_for_record('Account', instance.account_id)
|
|
|
|
|
|
@receiver(post_delete, sender=Contact)
|
|
def trigger_contact_post_delete(sender, instance, **kwargs):
|
|
if instance.account_id:
|
|
recalculate_rollups_for_record('Account', instance.account_id)
|
|
|
|
|
|
@receiver(post_save, sender=Activity)
|
|
def trigger_activity_post_save(sender, instance, created, **kwargs):
|
|
if not should_recalculate_child_rollups('Activity', instance, created):
|
|
return
|
|
_recalc_activity_parents(instance)
|
|
|
|
|
|
@receiver(post_delete, sender=Activity)
|
|
def trigger_activity_post_delete(sender, instance, **kwargs):
|
|
_recalc_activity_parents(instance)
|
|
|
|
|
|
def _recalc_activity_parents(instance):
|
|
if hasattr(instance, 'content_type') and instance.content_type:
|
|
model_name = instance.content_type.model.capitalize()
|
|
recalculate_rollups_for_record(model_name, instance.object_id)
|
|
if getattr(instance, 'account_id', None):
|
|
recalculate_rollups_for_record('Account', instance.account_id)
|
|
if getattr(instance, 'contact_id', None):
|
|
recalculate_rollups_for_record('Contact', instance.contact_id)
|
|
if getattr(instance, 'opportunity_id', None):
|
|
recalculate_rollups_for_record('Opportunity', instance.opportunity_id)
|
|
if getattr(instance, 'lead_id', None):
|
|
recalculate_rollups_for_record('Lead', instance.lead_id)
|
|
|
|
|
|
# Roll-up summary triggers for CustomRecord children
|
|
@receiver(post_save, sender=CustomRecord)
|
|
def trigger_custom_record_post_save(sender, instance, created, **kwargs):
|
|
cobj_name = instance.custom_object.name
|
|
if not should_recalculate_child_rollups(cobj_name, instance, created):
|
|
return
|
|
_recalc_custom_record_parents(instance)
|
|
|
|
|
|
@receiver(post_delete, sender=CustomRecord)
|
|
def trigger_custom_record_post_delete(sender, instance, **kwargs):
|
|
_recalc_custom_record_parents(instance)
|
|
|
|
|
|
def _recalc_custom_record_parents(instance):
|
|
data = instance.data or {}
|
|
# Recalculate parent CustomRecord if linked via parent_id
|
|
if 'parent_id' in data:
|
|
# Check custom object definition if available
|
|
recalculate_rollups_for_record(None, data['parent_id'])
|
|
|
|
# Recalculate standard parent entities if linked via standard fk (e.g. account_id, contact_id)
|
|
if 'account_id' in data:
|
|
recalculate_rollups_for_record('Account', data['account_id'])
|
|
if 'contact_id' in data:
|
|
recalculate_rollups_for_record('Contact', data['contact_id'])
|
|
if 'opportunity_id' in data:
|
|
recalculate_rollups_for_record('Opportunity', data['opportunity_id'])
|
|
if 'lead_id' in data:
|
|
recalculate_rollups_for_record('Lead', data['lead_id'])
|