Set observer/disabled=1 to run an observerless PlanetStack
diff --git a/planetstack/core/models/plcorebase.py b/planetstack/core/models/plcorebase.py
index dcc3c39..b64df5f 100644
--- a/planetstack/core/models/plcorebase.py
+++ b/planetstack/core/models/plcorebase.py
@@ -1,62 +1,59 @@
import os
from django.db import models
from django.forms.models import model_to_dict
-from observer.event_manager import EventSender
-
+# This is a no-op if observer_disabled is set to 1 in the config file
+from observer import *
class PlCoreBase(models.Model):
- created = models.DateTimeField(auto_now_add=True)
- updated = models.DateTimeField(auto_now=True)
- enacted = models.DateTimeField(null=True, default=None)
+ created = models.DateTimeField(auto_now_add=True)
+ updated = models.DateTimeField(auto_now=True)
+ enacted = models.DateTimeField(null=True, default=None)
- class Meta:
- abstract = True
- app_label = "core"
+ class Meta:
+ abstract = True
+ app_label = "core"
- def __init__(self, *args, **kwargs):
- super(PlCoreBase, self).__init__(*args, **kwargs)
- self.__initial = self._dict
+ def __init__(self, *args, **kwargs):
+ super(PlCoreBase, self).__init__(*args, **kwargs)
+ self.__initial = self._dict
- @property
- def diff(self):
- d1 = self.__initial
- d2 = self._dict
- diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]]
- return dict(diffs)
+ @property
+ def diff(self):
+ d1 = self.__initial
+ d2 = self._dict
+ diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]]
+ return dict(diffs)
- @property
- def has_changed(self):
- return bool(self.diff)
+ @property
+ def has_changed(self):
+ return bool(self.diff)
- @property
- def changed_fields(self):
- return self.diff.keys()
+ @property
+ def changed_fields(self):
+ return self.diff.keys()
- def get_field_diff(self, field_name):
- return self.diff.get(field_name, None)
+ def get_field_diff(self, field_name):
+ return self.diff.get(field_name, None)
- def delete(self, *args, **kwds):
- super(PlCoreBase, self).delete(*args, **kwds)
+ def delete(self, *args, **kwds):
+ super(PlCoreBase, self).delete(*args, **kwds)
- try:
- EventSender().fire({'delete_flag':True,'model':self.__name__})
- except:
- # Investigate later.
- pass
+ # This is a no-op if observer_disabled is set
+ notify_observer(model=self, delete=True)
- def save(self, *args, **kwargs):
- super(PlCoreBase, self).save(*args, **kwargs)
-
- # Tell the observer that the source database has been updated
- #EventSender().fire()
+ def save(self, *args, **kwargs):
+ super(PlCoreBase, self).save(*args, **kwargs)
+
+ # This is a no-op if observer_disabled is set
+ notify_observer()
- self.__initial = self._dict
+ self.__initial = self._dict
- @property
- def _dict(self):
- return model_to_dict(self, fields=[field.name for field in
- self._meta.fields])
+ @property
+ def _dict(self):
+ return model_to_dict(self, fields=[field.name for field in
+ self._meta.fields])
diff --git a/planetstack/observer/__init__.py b/planetstack/observer/__init__.py
index e69de29..b23caf2 100644
--- a/planetstack/observer/__init__.py
+++ b/planetstack/observer/__init__.py
@@ -0,0 +1,27 @@
+from planetstack.config import Config
+
+try:
+ observer_disabled = Config().observer_disabled
+except:
+ observer_disabled = False
+
+print_once = True
+
+if (not observer_disabled):
+ from .event_manager import EventSender
+
+ def notify_observer(model=None, delete=False):
+ try:
+ if (model and delete):
+ EventSender().fire({'delete_flag':delete,'model':model.__name__})
+ else:
+ EventSender().fire()
+ except Exception,e:
+ print "Exception in Observer. This should not disrupt the front end. %s"%str(e)
+
+else:
+ def notify_observer(model=None, delete=False):
+ if (print_once):
+ print "The observer is disabled"
+ print_once = False
+ return