Revision c3e8f508 api/emitter.py

b/api/emitter.py
3 3
# Copyright © 2010 Greek Research and Technology Network
4 4
#
5 5

  
6
import re
7
from xml.dom import minidom
8
from django.conf.urls.defaults import url
6 9
from piston.emitters import Emitter, Mimer
7 10
from piston.resource import Resource as BaseResource
8
from xml.dom import minidom
9
import re
10 11

  
11 12
_accept_re = re.compile(r'([^\s;,]+)(?:[^,]*?;\s*q=(\d*(?:\.\d+)?))?')
12 13

  
......
47 48
        """
48 49

  
49 50
        em = request.GET.get('format', 'json')
50
        if 'emitter_format' in kwargs:
51
        if 'emitter_format' in kwargs and \
52
           kwargs["emitter_format"] is not None:
51 53
            em = kwargs.pop('emitter_format')
52 54
        elif 'HTTP_ACCEPT' in request.META:
53 55
            accepts = parse_accept_header(request.META['HTTP_ACCEPT'])
......
116 118

  
117 119
Emitter.register('xml', OSXMLEmitter, 'application/xml')
118 120
Mimer.register(lambda *a: None, ('application/xml',))
121

  
122
def url_with_format(regex, *args, **kwargs):
123
    """
124
    An extended url() that adds an .json/.xml suffix to the end to avoid DRY
125
    """
126
    if regex[-1] == '$' and regex[-2] != '\\':
127
        regex = regex[:-1]
128
    regex = regex + r'(\.(?P<emitter_format>json|xml))?$'
129
    return url(regex, *args, **kwargs)

Also available in: Unified diff