GithubHelp home page GithubHelp logo

jcarbaugh / django-wellknown Goto Github PK

View Code? Open in Web Editor NEW
12.0 3.0 6.0 123 KB

Easy administration of /.well-known/ resources, robots.txt, and crossdomain.xml

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%

django-wellknown's Introduction

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 http://tools.ietf.org/html/draft-hammer-hostmeta-04

Requirements

python >= 2.5

django >= 1.0

django-robots (optional)

python-xrd

iso8601

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

Register resources

import wellknown
wellknown.register('host-meta', ...)

The first argument is the path that will be exposed for the resource. A path of host-meta will map to:

http://example.com/.well-known/host-meta

At least one of the following arguments is required:

  • content: a string or unicode representation of the resource, cached on startup
  • template: the path to a template that will be rendered for the resource, rendered and cached on startup
  • handler: a handler method that will be used to render the resource, invoked on each request

Handler methods must have the following signature and return a string or unicode object:

def handler(request, *args, **kwargs):
        pass

The register method takes one optional parameter, content_type. This is the content type that will be used to return the rendered resource. The following rules are used to determine the content_type to use:

  1. if a content_type parameter was passed to register, use it
  2. attempt to infer content_type from the path using the mimetypes package
  3. use text/plain if no content_type can be guessed

Model resources

Resources may be stored in the database to make it easy for non-technical users to edit. The following fields are available on a Resource:

  • path: the path that maps to the resource
  • content: the content that will be served when the resource is requested
  • content_type: the content_type with which the content will be returned, defaults to text/plain

When a resource is saved, it is updated in the cache.

host-meta

A default host-meta file is automatically handled by wellknown. To register an entry in host-meta:

wellknown.hostmeta.links.append(Link(rel=..., ...))

Refer to the python-xrd documentation for instructions on creating XRD Link elements.

The Host element of host-meta is inferred from the domain of the current Django Site object. To override the hosts, edit settings.py:

WELLKNOWN_HOSTMETA_SITES = ('example.com', 'www.example.com')

django-robots

wellknown will defer to django-robots if it is installed. To use django-robots to render robots.txt, add robots to INSTALLED_APPS in settings.py and wellknown will take care of the rest.

django-wellknown's People

Contributors

jcarbaugh avatar paulosman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

django-wellknown's Issues

syncdb seems to break

to reproduce I start with no db in my django app.
I have wellknown as an app directory.
I add wellkown to my settings.py installed_apps
I add wellkown urls.py to my main urls.py
I run python manage.py syncdb

I get an error:
django.db.utils.DatabaseError: no such table: django_site

the stack includes:
File "/home/mattkatz/Projects/django-federation/wellknown/models.py", line 11, in
wellknown.register('host-meta', handler=HostMeta(), content_type='application/xrd+xml')
File "/home/mattkatz/Projects/django-federation/wellknown/resources.py", line 16, in init
hosts = getattr(settings, "WELLKNOWN_HOSTMETA_HOSTS", (Site.objects.get_current().domain,))

commenting out
from resources import HostMeta
register('host-meta', handler=HostMeta(), content_type='application/xrd+xml')

fixes this error

commenting out
for res in Resource.objects.all():
wellknown.register(res.path, content=res.content, content_type=res.content_type)
fixes the following error when running syncdb:
django.db.utils.DatabaseError: no such table: wellknown_resource

django 1.2 python 2.6

HostMeta resource is being output as printable representation of xml.dom.minidom.Document instance

First of all, thanks for writing python-xrd and django-wellknown and making them available. Saved me a huge amount of time / effort.

Steps to reproduce:

  1. Install and configure django-wellknown
  2. Manually address http://github.com/jcarbaugh/django-wellknown/issues/issue/2
  3. Make a request to http://localhost:8000/.well-known/host-meta

Example output:

HTTP/1.0 200 OK
Date: Sat, 14 Aug 2010 03:27:58 GMT
Server: WSGIServer/0.1 Python/2.6.5
Content-Type: application/xrd+xml

<xml.dom.minidom.Document instance at 0x2a6e6c8>

Expected:

HTTP/1.0 200 OK
Date: Sat, 14 Aug 2010 03:51:25 GMT
Server: WSGIServer/0.1 Python/2.6.5
Content-Type: application/xrd+xml

<?xml version="1.0" ?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0" xmlns:hm="http://host-meta.net/ns/1.0">
    <hm:Host>example.com</hm:Host>
</XRD>

I didn't know whether to create this issue against python-xrd or django-wellknown, so bear with me. The core issue is that the default handler for the host-meta resource, wellknown.resources.HostMeta returns an xml.dom.minidom.Document instance instead of the XML that the DOM represents as a string. This is because the __call__ method returns the result of XRD.to_xml which in turn returns an xml.dom.minidom.Document instance.

So, depending on what you see as the source of the problem, there are a few different ways to fix this specific problem:

  1. Modify the _render_xml function in xrd.py of python-xrd to return XML instead of an xml.dom.minidom.Document instance:
def _render_xml(xrd):
    ...
    return doc.toxml()
  1. Modify the to_xml method of the XRD class in xrd.py to return XML:
def to_xml(self):
    doc = _render_xml(self)
    return doc.toxml()
  1. Modify the __call__ method in the HostMeta class in resources.py to do the call:
def __call__(self, *args, **kwargs):
    doc = self.to_xml()
    return doc.toxml()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.