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 | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 4 | from dependency_walker import * |
Sapan Bhatia | fe9bc89 | 2014-09-15 03:48:11 -0400 | [diff] [blame] | 5 | import model_policies |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 6 | from util.logger import logger |
Sapan Bhatia | b3507b9 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 7 | from datetime import datetime |
| 8 | import time |
| 9 | from core.models import * |
| 10 | from django.db.transaction import atomic |
| 11 | from django.db.models import F, Q |
Sapan Bhatia | 7a1e6be | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 12 | |
Scott Baker | f342870 | 2014-10-21 15:05:29 -0700 | [diff] [blame] | 13 | modelPolicyEnabled = True |
| 14 | |
| 15 | def EnableModelPolicy(x): |
| 16 | global modelPolicyEnabled |
| 17 | modelPolicyEnabled = x |
| 18 | |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 19 | def update_dep(d, o): |
Sapan Bhatia | 6d1241c | 2014-12-21 02:32:50 -0500 | [diff] [blame] | 20 | try: |
| 21 | if (d.updated < o.updated): |
| 22 | d.save(update_fields=['updated']) |
| 23 | except AttributeError,e: |
Sapan Bhatia | 6d1241c | 2014-12-21 02:32:50 -0500 | [diff] [blame] | 24 | raise e |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 25 | |
| 26 | def delete_if_inactive(d, o): |
| 27 | #print "Deleting %s (%s)"%(d,d.__class__.__name__) |
Sapan Bhatia | b3507b9 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 28 | # d.delete() |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 29 | return |
| 30 | |
Sapan Bhatia | b3507b9 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 31 | @atomic |
| 32 | def execute_model_policy(instance, deleted): |
| 33 | # Automatic dirtying |
| 34 | walk_inv_deps(update_dep, instance) |
Sapan Bhatia | 7a1e6be | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 35 | |
Sapan Bhatia | b3507b9 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 36 | sender_name = instance.__class__.__name__ |
| 37 | policy_name = 'model_policy_%s'%sender_name |
| 38 | noargs = False |
| 39 | |
| 40 | if deleted: |
| 41 | walk_inv_deps(delete_if_inactive, instance) |
| 42 | else: |
Sapan Bhatia | 7a1e6be | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 43 | try: |
| 44 | policy_handler = getattr(model_policies, policy_name, None) |
Sapan Bhatia | b3507b9 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 45 | logger.error("POLICY HANDLER: %s %s" % (policy_name, policy_handler)) |
Sapan Bhatia | 7a1e6be | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 46 | if policy_handler is not None: |
| 47 | policy_handler.handle(instance) |
| 48 | except: |
| 49 | logger.log_exc("Model Policy Error:") |
| 50 | print "Policy Exceution Error" |
Sapan Bhatia | 7a1e6be | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 51 | |
Sapan Bhatia | b3507b9 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 52 | instance.policed=datetime.now() |
| 53 | instance.save(update_fields=['policed']) |
Sapan Bhatia | 7a1e6be | 2015-01-16 22:17:42 +0000 | [diff] [blame] | 54 | |
Sapan Bhatia | b3507b9 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 55 | def run_policy(): |
Sapan Bhatia | 3345719 | 2015-01-29 20:44:46 +0000 | [diff] [blame] | 56 | from core.models import Slice,Controller,Network,User,SlicePrivilege,Site,SitePrivilege,Image,ControllerSlice,ControllerUser,ControllerSite |
Sapan Bhatia | b3507b9 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 57 | while (True): |
| 58 | start = time.time() |
Sapan Bhatia | 3345719 | 2015-01-29 20:44:46 +0000 | [diff] [blame] | 59 | models = [Slice, Controller, Network, User, SlicePrivilege, Site, SitePrivilege, Image, ControllerSlice, ControllerSite, ControllerUser] |
Sapan Bhatia | b3507b9 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 60 | objects = [] |
| 61 | |
| 62 | for m in models: |
| 63 | res = m.objects.filter(Q(policed__lt=F('updated')) | Q(policed=None)) |
| 64 | objects.extend(res) |
Sapan Bhatia | 14356b7 | 2014-11-05 10:32:41 -0500 | [diff] [blame] | 65 | |
Sapan Bhatia | b3507b9 | 2015-01-23 16:09:28 +0000 | [diff] [blame] | 66 | for o in objects: |
| 67 | print "Working on %r"%o |
| 68 | execute_model_policy(o, False) |
| 69 | |
| 70 | |
| 71 | if (time.time()-start<1): |
| 72 | time.sleep(1) |