Commits

Peter Sanchez committed 09f6a55

Updated do_action to limit the number of .save() calls used

  • Participants
  • Parent commits f1e0be2

Comments (0)

Files changed (1)

File djpubsubhubbub/models.py

         subscription, created = self.get_or_create(
             hub=hub, topic=topic)
         signals.pre_subscribe.send(sender=subscription, created=created)
-        subscription.set_expiration(lease_seconds)
+        subscription.set_expiration(lease_seconds, run_save=False)
+        token = subscription.generate_token(mode)
 
         if callback is None:
             try:
                 'callback': callback,
                 'topic': topic,
                 'verify': verify,
-                'verify_token': subscription.generate_token(mode),
+                'verify_token': token,
                 'lease_seconds': lease_seconds,
             }, headers, debug)
 
     def __str__(self):
         return str(unicode(self))
 
-    def set_expiration(self, lease_seconds):
+    def set_expiration(self, lease_seconds, run_save=True):
         self.lease_expires = datetime.now() + timedelta(
             seconds=lease_seconds)
-        self.save()
+        if run_save:
+            self.save()
 
-    def generate_token(self, mode):
+    def generate_token(self, mode, run_save=True):
         assert self.pk is not None, \
             'Subscription must be saved before generating token'
         token = mode[:20] + sha_constructor('%s%i%s' % (
                 settings.SECRET_KEY, self.pk, mode)).hexdigest()
         self.verify_token = token
-        self.save()
+        if run_save:
+            self.save()
         return token
     
     def save(self, *args, **kwargs):