Skip to content

Commit

Permalink
make changes so that webfinger.init() will not need to be called
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarbaugh committed Nov 18, 2009
1 parent b171e58 commit bcde70e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
18 changes: 7 additions & 11 deletions wellknown/__init__.py
Expand Up @@ -3,15 +3,17 @@
import mimetypes

_cache = { }

hostmeta = HostMeta()

def register(path, handler=None, template=None, content=None, content_type=None):
def get_resource(path):
return _cache.get(path, (None, None))

def register(path, handler=None, template=None, content=None, content_type=None, update=False):

if path in _cache:
raise ValueError(u"duplicate resource for %s" % path)

if content_type is None:
if content_type is None and not update:
content_type = mimetypes.guess_type(path)[0] or 'text/plain'

if handler:
Expand All @@ -22,11 +24,5 @@ def register(path, handler=None, template=None, content=None, content_type=None)
_cache[path] = (content, content_type)
else:
raise ValueError(u"either handler, template, or content must be specified")

def init():
from django.conf import settings
from wellknown.models import Resource
for res in Resource.objects.all():
register(res.path, content=res.content, content_type=res.content_type)
content_type = 'text/plain' if settings.DEBUG else 'application/xrd+xml'
register('host-meta', handler=hostmeta.render, content_type=content_type)

__all__ = ['register','hostmeta']
24 changes: 21 additions & 3 deletions wellknown/models.py
@@ -1,7 +1,7 @@
from django.db import models
from django.db.models.signals import post_save
from wellknown import hostmeta, register
import mimetypes
import wellknown

class Resource(models.Model):
path = models.CharField(max_length=128)
Expand All @@ -26,6 +26,24 @@ def save(self, **kwargs):

def save_handler(sender, **kwargs):
reg = kwargs['instance']
wellknown._cache[reg.path] = (reg.content, reg.content_type)
register(
reg.path,
content=reg.content,
content_type=reg.content_type,
update=True
)

post_save.connect(save_handler, sender=Resource)
post_save.connect(save_handler, sender=Resource)

#
# cache resources
#

for res in Resource.objects.all():
register(res.path, content=res.content, content_type=res.content_type)

#
# create default host-meta handler
#

register('host-meta', handler=hostmeta, content_type='application/xrd+xml')
2 changes: 1 addition & 1 deletion wellknown/resources.py
Expand Up @@ -24,6 +24,6 @@ def register_link(self, rels, uri=None, uri_template=None, title=None):

self._links.append(link)

def render(self, *args, **kwargs):
def __call__(self, *args, **kwargs):
data = {'hosts': self._hosts, 'links': self._links, 'lang': self._lang}
return render_to_string('wellknown/host-meta.xml', data)
12 changes: 11 additions & 1 deletion wellknown/views.py
@@ -1,3 +1,4 @@
from django.conf import settings
from django.http import HttpResponse, Http404
import wellknown

Expand All @@ -7,8 +8,11 @@
rules_list = None

def handle(request, path, *args, **kwargs):
""" Basic handler view to either display cached content
or make call to actual handler method for rendering.
"""

(handler_or_content, content_type) = wellknown._cache.get(path, (None, None))
(handler_or_content, content_type) = wellknown.get_resource(path)

if handler_or_content is None:
raise Http404()
Expand All @@ -21,9 +25,15 @@ def handle(request, path, *args, **kwargs):
return HttpResponse(content or '', content_type=content_type)

def crossdomain(request, *args, **kwargs):
""" View that overrides /crossdomain.xml to
handle as a well-known resource.
"""
return handle(request, 'crossdomain.xml', *args, **kwargs)

def robots(request, *args, **kwargs):
""" Handle /robots.txt as a well-known resource or
pass off request to django-robots.
"""
if rules_list: # use django-robots if it is installed
return rules_list(request, *args, **kwargs)
return handle(request, 'robots.txt', *args, **kwargs)

0 comments on commit bcde70e

Please sign in to comment.