Commits

Peter Sanchez  committed 4ed55e7

Base workings...

  • Participants
  • Parent commits eed1321

Comments (0)

Files changed (4)

File callback/__init__.py

+from callback import callback_manager
+from base import CallbackException, CallbackBase
+
+
 __version__ = '0.1'

File callback/base.py

+from django.conf import settings
+
+
+class CallbackException(Exception):
+    pass
+
+
+class CallbackBase(object):
+    def process(self, model_instance):
+        raise CallbackException('Not Implemented')

File callback/callback.py

+from django.db import models
+from base import CallbackException, CallbackBase
+
+
+class CallbackManager(object):
+    def __init__(self):
+        self._callbacks = {}
+
+    def register_callback(self, dest_model, dest_cls):
+        if not issubclass(dest_model, models.Model):
+            raise CallbackException(
+                'Cannot register model - not a '
+                'subclass of django.db.models.Model'
+            )
+        if not issubclass(dest_cls, CallbackBase):
+            raise CallbackException(
+                'Cannot register class - not a subclass of CallbackBase'
+            )
+
+        self._callbacks[dest_model] = dest_cls
+
+    def unregister_callback(self, dest_model):
+        self._callbacks.pop(dest_model, None)
+
+    def get_callback_class(self, dest_model):
+        for model, cls in self._callbacks.items():
+            if isinstance(dest_model, model):
+                return cls()
+        return None
+
+    def process_callback(self, callback_instance):
+        model = callback_instance.content_object
+        cls = self.process_callback(model)
+        if cls is None:
+            raise CallbackException(
+                'Model class is not registered.'
+            )
+        try:
+            cls.process(model)
+        except:
+            # Do something here
+            raise CallbackException(
+                'Error!!!'
+            )
+
+
+callback_manager = CallbackManager()

File callback/models.py

+import datetime
+from django.db import models
+from django.contrib.contenttypes.models import ContentType
+from django.contrib.contenttypes import generic
+
+
+class CallbackMap(models.Model):
+    hash_id = models.CharField(max_length=40)
+    content_type = models.ForeignKey(ContentType)
+    object_id = models.PositiveIntegerField()
+    content_object = generic.GenericForeignKey('content_type', 'object_id')
+
+    created_on = models.DateTimeField(default=datetime.datetime.now)
+    updated_on = models.DateTimeField(default=datetime.datetime.now)
+
+    # Methods
+    def save(self, *args, **kwargs):
+        if self.id:
+            self.updated_on = datetime.datetime.now()
+        super(CallbackMap, self).save(*args, **kwargs)