Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarbaugh committed Nov 12, 2009
0 parents commit 9969498
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
*.pyc
10 changes: 10 additions & 0 deletions LICENSE
@@ -0,0 +1,10 @@
Copyright (c) 2009, Jeremy Carbaugh
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of django-wellknown nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 changes: 43 additions & 0 deletions README.rst
@@ -0,0 +1,43 @@
================
django-wellknown
================

Django application to provide easy administration of site-meta URIs. Includes robots.txt and crossdomain.xml.

http://tools.ietf.org/html/draft-nottingham-site-meta-03

Requirements
============

python >= 2.5

django >= 1.0

django-robots (optional)

Installation
============

Be sure to add ``wellknown`` to ``INSTALLED_APPS`` in settings.py. Additionally, add the following entry to urls.py::

urls(r'^', include('wellknown.urls')),

Run ``./manage.py syncdb``

Usage
=====

Preload cache
-------------

To load well-known URIs stored in the database::

import wellknown
wellknown.init()



django-robots
-------------

wellknown will defer to `django-robots <http://bitbucket.org/jezdez/django-robots/>`_ if it is installed. To use django-robots to render robots.txt, add ``robots`` to ``INSTALLED_APPS`` in settings.py. wellknown will take care of the rest.
Empty file added requirements.txt
Empty file.
27 changes: 27 additions & 0 deletions wellknown/__init__.py
@@ -0,0 +1,27 @@
from django.template.loader import render_to_string
import mimetypes

_cache = { }

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

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

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

if handler:
_cache[path] = (handler, content_type)
elif template:
_cache[path] = (render_to_string(template), content_type)
elif content:
_cache[path] = (content, content_type)
else:
raise ValueError(u"either handler, template, or content must be specified")


def init():
from wellknown.models import Registration
for reg in Registration.objects.all():
register(reg.path, content=reg.content, content_type=reg.content_type)
7 changes: 7 additions & 0 deletions wellknown/admin.py
@@ -0,0 +1,7 @@
from django.contrib import admin
from wellknown.models import Registration

class RegistrationAdmin(admin.ModelAdmin):
pass

admin.site.register(Registration, RegistrationAdmin)
31 changes: 31 additions & 0 deletions wellknown/models.py
@@ -0,0 +1,31 @@
from django.db import models
from django.db.models.signals import post_save
import mimetypes
import wellknown

class Registration(models.Model):
path = models.CharField(max_length=128)
content = models.TextField(blank=True)
content_type = models.CharField(max_length=128, blank=True)

class Meta:
ordering = ('path',)

def __unicode__(self):
return self.path

def save(self, **kwargs):
self.path = self.path.strip('/')
if not self.content_type:
self.content_type = mimetypes.guess_type(self.path)[0] or 'text/plain'
super(Registration, self).save(**kwargs)

#
# update registrations when models are saved
#

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

post_save.connect(save_handler, sender=Registration)
23 changes: 23 additions & 0 deletions wellknown/tests.py
@@ -0,0 +1,23 @@
"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".
Replace these with more appropriate tests for your application.
"""

from django.test import TestCase

class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.failUnlessEqual(1 + 1, 2)

__test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2.
>>> 1 + 1 == 2
True
"""}

7 changes: 7 additions & 0 deletions wellknown/urls.py
@@ -0,0 +1,7 @@
from django.conf.urls.defaults import *

urlpatterns = patterns('wellknown.views',
url(r'^\.well-known/(?P<path>.*)', 'handle', name='wellknown'),
url(r'^crossdomain\.xml$', 'crossdomain', name='crossdomain.xml'),
url(r'^robots\.txt$', 'robots', name='robots.txt'),
)
29 changes: 29 additions & 0 deletions wellknown/views.py
@@ -0,0 +1,29 @@
from django.http import HttpResponse, Http404
import wellknown

try:
from robots.views import rules_list
except ImportError:
rules_list = None

def handle(request, path, *args, **kwargs):

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

if handler_or_content is None:
raise Http404()

if callable(handler_or_content):
content = handler_or_content(request, path, content_type=content_type, *args, **kwargs)
else:
content = handler_or_content

return HttpResponse(content or '', content_type=content_type)

def crossdomain(request, *args, **kwargs):
return handle(request, 'crossdomain.xml', *args, **kwargs)

def robots(request, *args, **kwargs):
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 9969498

Please sign in to comment.