Commits

Peter Sanchez committed 6d8fdc3

Added support for EasyConfig to customize headers and default callback URL. This is a backwards incompatible change.

  • Participants
  • Parent commits 76be099

Comments (0)

Files changed (2)

djpubsubhubbub/__init__.py

+from django.contrib.sites.models import Site
+from easyconfig import EasyConfig
+
+
 __version__ = '0.3'
+
+
+class Config(object):
+    ''' Base config class to easily pass headers, etc.
+    '''
+    config = EasyConfig('djpubsubhubbub.Config', 'PUBSUBHUBBUB_CONFIG')
+    
+    def get_extra_hub_headers(self, feed_url, hub_url):
+        return self.config.get_object('get_extra_hub_headers', {})
+
+    def get_default_callback_host(self, feed_url, hub_url):
+        return self.config.get_objects(
+            'get_default_callback_host',
+            str(Site.objects.get_current())
+        )

djpubsubhubbub/models.py

 from urllib import urlencode
 from datetime import datetime, timedelta
 
+from django.db import models
 from django.conf import settings
-from django.contrib.sites.models import Site
+from django.utils.hashcompat import sha_constructor
 from django.core.urlresolvers import reverse, NoReverseMatch
-from django.db import models
-from django.utils.hashcompat import sha_constructor
 
-from djpubsubhubbub import signals
+from djpubsubhubbub import Config, signals
 
 DEFAULT_LEASE_SECONDS = 2592000 # 30 days in seconds
 
 class SubscriptionManager(models.Manager):
 
     def do_action(self, topic, hub=None, callback=None, lease_seconds=None,
-                  mode='subscribe', verify='sync', headers={}, debug=False):
+                  mode='subscribe', verify='sync', debug=False):
+        config = Config()
         if hub is None:
             hub = self._get_hub(topic)
 
         signals.pre_subscribe.send(sender=subscription, created=created)
         subscription.set_expiration(lease_seconds, run_save=False)
         token = subscription.generate_token(mode)
+        headers = config.get_extra_hub_headers(topic, hub)
 
         if callback is None:
             try:
                 raise TypeError(
                     'callback cannot be None if there is not a reversible URL')
             else:
-                callback = 'http://' + str(Site.objects.get_current()) + \
+                callback = 'http://%s%s' % (
+                    config.get_default_callback_host(topic, hub),
                     callback_path
+                )
 
         response = self._send_request(hub, {
                 'mode': mode,