blob: 9224c50e9d3674160830e7e054a52ce970bf0262 [file] [log] [blame]
Sapan Bhatia0235e982014-09-03 13:14:40 -04001from django.db.models.signals import post_save
2from django.dispatch import receiver
3import pdb
Sapan Bhatia3a5980e2014-09-15 03:48:11 -04004from core.models import *
5import model_policies
Sapan Bhatia0235e982014-09-03 13:14:40 -04006
Scott Baker79eaac22014-10-21 15:05:29 -07007modelPolicyEnabled = True
8
9def EnableModelPolicy(x):
10 global modelPolicyEnabled
11 modelPolicyEnabled = x
12
Sapan Bhatia0235e982014-09-03 13:14:40 -040013@receiver(post_save)
Scott Bakerd56ccd72014-09-30 14:53:45 -070014def post_save_handler(sender, instance, **kwargs):
Sapan Bhatia0235e982014-09-03 13:14:40 -040015 sender_name = sender.__name__
16 policy_name = 'model_policy_%s'%sender_name
Scott Baker79eaac22014-10-21 15:05:29 -070017
18 if not modelPolicyEnabled:
19 return
Sapan Bhatia640f2b72014-09-15 04:04:32 -040020
21 if (not kwargs['update_fields']):
22 try:
Scott Bakerc1f9cce2014-10-09 12:03:22 -070023 policy_handler = getattr(model_policies, policy_name, None)
24 if policy_handler is not None:
25 policy_handler.handle(instance)
Sapan Bhatia640f2b72014-09-15 04:04:32 -040026 except:
27 pass