0fb7fc65d81c — Peter Sanchez 13 years ago
Added managers and store_callback functionality
3 files changed, 29 insertions(+), 5 deletions(-)

M callback/callback.py
A => callback/managers.py
M callback/models.py
M callback/callback.py +9 -5
@@ 1,4 1,5 @@ 
 from django.db import models
+from models import CallbackMap
 from base import CallbackException, CallbackBase
 
 

          
@@ 26,15 27,18 @@ class CallbackManager(object):
         for model, cls in self._callbacks.items():
             if isinstance(dest_model, model):
                 return cls()
-        return None
+        raise CallbackException('Model class is not registered.')
+
+    def store_callback(self, model_instance):
+        if not model_instance.pk:
+            model_instance.save()
+
+        cls = self.get_callback_class(model_instance)
+        return CallbackMap.objects.store_callback(model_instance)
 
     def process_callback(self, callback_instance):
         model = callback_instance.content_object
         cls = self.get_callback_class(model)
-        if cls is None:
-            raise CallbackException(
-                'Model class is not registered.'
-            )
         try:
             cls.process(model)
         except:

          
A => callback/managers.py +17 -0
@@ 0,0 1,17 @@ 
+import random
+import hashlib
+from django.db import models
+from django.contrib.auth.models import User
+
+
+class CallbackMapManager(models.Manager):
+    def generate_hash(self):
+        salt = hashlib.sha1(str(random.random())).hexdigest()[:5]
+        random_string = User.objects.make_random_password()
+        return hashlib.sha1(salt + random_string).hexdigest()
+
+    def store_callback(self, model_instance):
+        hash_id = self.generate_hash()
+        while self.get_query_set().filter(hash_id=hash_id).exists():
+            hash_id = self.generate_hash()
+        return self.create(hash_id=hash_id, content_object=model_instance)

          
M callback/models.py +3 -0
@@ 2,6 2,7 @@ import datetime
 from django.db import models
 from django.contrib.contenttypes.models import ContentType
 from django.contrib.contenttypes import generic
+from managers import CallbackMapManager
 
 
 class CallbackMap(models.Model):

          
@@ 13,6 14,8 @@ class CallbackMap(models.Model):
     created_on = models.DateTimeField(default=datetime.datetime.now)
     updated_on = models.DateTimeField(default=datetime.datetime.now)
 
+    objects = CallbackMapManager()
+
     # Methods
     def save(self, *args, **kwargs):
         if self.id: