Reverted model policies to non-parallel version
Sapan Bhatia [Fri, 16 Jan 2015 22:17:42 +0000 (22:17 +0000)]
planetstack/model_policy.py

index e12b15c..7aef793 100644 (file)
@@ -6,6 +6,7 @@ from dependency_walker import *
 import model_policies
 from util.logger import logger
 
+
 modelPolicyEnabled = True
 
 def EnableModelPolicy(x):
@@ -17,7 +18,6 @@ def update_dep(d, o):
                if (d.updated < o.updated):
                        d.save(update_fields=['updated'])
        except AttributeError,e:
-               pdb.set_trace()
                raise e
        
 def delete_if_inactive(d, o):
@@ -25,24 +25,37 @@ def delete_if_inactive(d, o):
        d.delete()      
        return
 
-@receiver(post_save)
-def post_save_handler(sender, instance, **kwargs):
-        if not modelPolicyEnabled:
-            return
-
-       sender_name = sender.__name__
-       policy_name = 'model_policy_%s'%sender_name
-       
-       if (not kwargs['update_fields']):
+def execute_model_policy(policy_name, instance, update_fields_empty, deleted):
+       if (update_fields_empty):
                # Automatic dirtying
-               walk_inv_deps(update_dep, instance)
+               #walk_inv_deps(update_dep, instance)
 
                try:
                        policy_handler = getattr(model_policies, policy_name, None)
+                        logger.error("POLICY HANDLER: %s %s" % (policy_name, policy_handler))                       
                        if policy_handler is not None:
                                policy_handler.handle(instance)
                except:
                        logger.log_exc("Model Policy Error:") 
-       elif 'deleted' in kwargs['update_fields']:
+                       print "Policy Exceution Error"
+       elif deleted:
                walk_inv_deps(delete_if_inactive, instance)
+
+
+@receiver(post_save)
+def post_save_handler(sender, instance, **kwargs):
+        if not modelPolicyEnabled:
+            return
+
+       sender_name = sender.__name__
+       policy_name = 'model_policy_%s'%sender_name
+        if (not kwargs['update_fields']):
+               noargs = True
+               deleted = False
+       else:
+               noargs = False
+               deleted = True
+
+       execute_model_policy(policy_name, instance, noargs, deleted)
+