Sapan Bhatia | 0235e98 | 2014-09-03 13:14:40 -0400 | [diff] [blame] | 1 | from django.db.models.signals import post_save |
| 2 | from django.dispatch import receiver |
| 3 | import pdb |
Sapan Bhatia | 3a5980e | 2014-09-15 03:48:11 -0400 | [diff] [blame] | 4 | from core.models import * |
| 5 | import model_policies |
Sapan Bhatia | 0235e98 | 2014-09-03 13:14:40 -0400 | [diff] [blame] | 6 | |
Scott Baker | 79eaac2 | 2014-10-21 15:05:29 -0700 | [diff] [blame] | 7 | modelPolicyEnabled = True |
| 8 | |
| 9 | def EnableModelPolicy(x): |
| 10 | global modelPolicyEnabled |
| 11 | modelPolicyEnabled = x |
| 12 | |
Sapan Bhatia | 0235e98 | 2014-09-03 13:14:40 -0400 | [diff] [blame] | 13 | @receiver(post_save) |
Scott Baker | d56ccd7 | 2014-09-30 14:53:45 -0700 | [diff] [blame] | 14 | def post_save_handler(sender, instance, **kwargs): |
Sapan Bhatia | 0235e98 | 2014-09-03 13:14:40 -0400 | [diff] [blame] | 15 | sender_name = sender.__name__ |
| 16 | policy_name = 'model_policy_%s'%sender_name |
Scott Baker | 79eaac2 | 2014-10-21 15:05:29 -0700 | [diff] [blame] | 17 | |
| 18 | if not modelPolicyEnabled: |
| 19 | return |
Sapan Bhatia | 640f2b7 | 2014-09-15 04:04:32 -0400 | [diff] [blame] | 20 | |
| 21 | if (not kwargs['update_fields']): |
| 22 | try: |
Scott Baker | c1f9cce | 2014-10-09 12:03:22 -0700 | [diff] [blame] | 23 | policy_handler = getattr(model_policies, policy_name, None) |
| 24 | if policy_handler is not None: |
| 25 | policy_handler.handle(instance) |
Sapan Bhatia | 640f2b7 | 2014-09-15 04:04:32 -0400 | [diff] [blame] | 26 | except: |
| 27 | pass |