Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9969498
Showing
10 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.contrib import admin | ||
from wellknown.models import Registration | ||
|
||
class RegistrationAdmin(admin.ModelAdmin): | ||
pass | ||
|
||
admin.site.register(Registration, RegistrationAdmin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
"""} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |