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 * |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 5 | from dependency_walker import * |
Sapan Bhatia | 3a5980e | 2014-09-15 03:48:11 -0400 | [diff] [blame] | 6 | import model_policies |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 7 | from util.logger import logger |
Sapan Bhatia | 0235e98 | 2014-09-03 13:14:40 -0400 | [diff] [blame] | 8 | |
Sapan Bhatia | e2ace18 | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 9 | |
Scott Baker | 79eaac2 | 2014-10-21 15:05:29 -0700 | [diff] [blame] | 10 | modelPolicyEnabled = True |
| 11 | |
| 12 | def EnableModelPolicy(x): |
| 13 | global modelPolicyEnabled |
| 14 | modelPolicyEnabled = x |
| 15 | |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 16 | def update_dep(d, o): |
Sapan Bhatia | 142314e | 2014-12-21 02:32:50 -0500 | [diff] [blame] | 17 | try: |
| 18 | if (d.updated < o.updated): |
| 19 | d.save(update_fields=['updated']) |
| 20 | except AttributeError,e: |
Sapan Bhatia | 142314e | 2014-12-21 02:32:50 -0500 | [diff] [blame] | 21 | raise e |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 22 | |
| 23 | def delete_if_inactive(d, o): |
| 24 | #print "Deleting %s (%s)"%(d,d.__class__.__name__) |
| 25 | d.delete() |
| 26 | return |
| 27 | |
Sapan Bhatia | e2ace18 | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 28 | def execute_model_policy(policy_name, instance, update_fields_empty, deleted): |
| 29 | if (update_fields_empty): |
| 30 | # Automatic dirtying |
| 31 | #walk_inv_deps(update_dep, instance) |
| 32 | |
| 33 | try: |
| 34 | policy_handler = getattr(model_policies, policy_name, None) |
| 35 | logger.error("POLICY HANDLER: %s %s" % (policy_name, policy_handler)) |
| 36 | if policy_handler is not None: |
| 37 | policy_handler.handle(instance) |
| 38 | except: |
| 39 | logger.log_exc("Model Policy Error:") |
| 40 | print "Policy Exceution Error" |
| 41 | elif deleted: |
| 42 | walk_inv_deps(delete_if_inactive, instance) |
| 43 | |
| 44 | |
Sapan Bhatia | 0235e98 | 2014-09-03 13:14:40 -0400 | [diff] [blame] | 45 | @receiver(post_save) |
Scott Baker | d56ccd7 | 2014-09-30 14:53:45 -0700 | [diff] [blame] | 46 | def post_save_handler(sender, instance, **kwargs): |
Scott Baker | 79eaac2 | 2014-10-21 15:05:29 -0700 | [diff] [blame] | 47 | if not modelPolicyEnabled: |
| 48 | return |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 49 | |
| 50 | sender_name = sender.__name__ |
| 51 | policy_name = 'model_policy_%s'%sender_name |
Sapan Bhatia | e2ace18 | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 52 | if (not kwargs['update_fields']): |
| 53 | noargs = True |
| 54 | deleted = False |
| 55 | else: |
| 56 | noargs = False |
| 57 | deleted = True |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 58 | |
Sapan Bhatia | e2ace18 | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 59 | execute_model_policy(policy_name, instance, noargs, deleted) |
| 60 | |
Sapan Bhatia | 475c597 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 61 | |