              self._auth = self._authenticate()
          return self._auth
  
      def _authenticate(self):
          for authenticator_cls in self.authenticators:
              authenticator = authenticator_cls(self)
              auth = authenticator.authenticate(self.request)
              if auth:
                  return auth
          return None
  
      # TODO?
 
</s> remove """If python-markdown is installed expose an apply_markdown(text) function,
to convert markeddown text into html.  Otherwise just set apply_markdown to None.

See: http://www.freewisdom.org/projects/python-markdown/
"""

__all__ = ['apply_markdown']

try:
    import markdown
    import re
    
    class CustomSetextHeaderProcessor(markdown.blockprocessors.BlockProcessor):
        """Override markdown's SetextHeaderProcessor, so that ==== headers are <h2> and ---- headers are <h3>.
        
        We use <h1> for the resource name."""
    
        # Detect Setext-style header. Must be first 2 lines of block.
        RE = re.compile(r'^.*?\n[=-]{3,}', re.MULTILINE)
    
        def test(self, parent, block):
            return bool(self.RE.match(block))
    
        def run(self, parent, blocks):
            lines = blocks.pop(0).split('\n')
            # Determine level. ``=`` is 1 and ``-`` is 2.
            if lines[1].startswith('='):
                level = 2
            else:
                level = 3
            h = markdown.etree.SubElement(parent, 'h%d' % level)
            h.text = lines[0].strip()
            if len(lines) > 2:
                # Block contains additional lines. Add to  master blocks for later.
                blocks.insert(0, '\n'.join(lines[2:]))
            
    def apply_markdown(text):
        """Simple wrapper around markdown.markdown to apply our CustomSetextHeaderProcessor,
        and also set the base level of '#' style headers to <h2>."""
        extensions = ['headerid(level=2)']
        safe_mode = False,
        output_format = markdown.DEFAULT_OUTPUT_FORMAT

        md = markdown.Markdown(extensions=markdown.load_extensions(extensions),
                               safe_mode=safe_mode, 
                               output_format=output_format)
        md.parser.blockprocessors['setextheader'] = CustomSetextHeaderProcessor(md.parser)
        return md.convert(text)

except:
    apply_markdown = None </s> add  </s> remove     # python >= ?
    from urlparse import parse_qs
 </s> add     import markdown
    import re
    
    class CustomSetextHeaderProcessor(markdown.blockprocessors.BlockProcessor):
        """Override markdown's SetextHeaderProcessor, so that ==== headers are <h2> and ---- headers are <h3>.
        
        We use <h1> for the resource name."""
    
        # Detect Setext-style header. Must be first 2 lines of block.
        RE = re.compile(r'^.*?\n[=-]{3,}', re.MULTILINE)
    
        def test(self, parent, block):
            return bool(self.RE.match(block))
    
        def run(self, parent, blocks):
            lines = blocks.pop(0).split('\n')
            # Determine level. ``=`` is 1 and ``-`` is 2.
            if lines[1].startswith('='):
                level = 2
            else:
                level = 3
            h = markdown.etree.SubElement(parent, 'h%d' % level)
            h.text = lines[0].strip()
            if len(lines) > 2:
                # Block contains additional lines. Add to  master blocks for later.
                blocks.insert(0, '\n'.join(lines[2:]))
            
    def apply_markdown(text):
        """Simple wrapper around markdown.markdown to apply our CustomSetextHeaderProcessor,
        and also set the base level of '#' style headers to <h2>."""
        extensions = ['headerid(level=2)']
        safe_mode = False,
        output_format = markdown.DEFAULT_OUTPUT_FORMAT

        md = markdown.Markdown(extensions=markdown.load_extensions(extensions),
                               safe_mode=safe_mode, 
                               output_format=output_format)
        md.parser.blockprocessors['setextheader'] = CustomSetextHeaderProcessor(md.parser)
        return md.convert(text)
 </s> remove from django.conf.urls.defaults import patterns, url
from django import http
from django.test import TestCase
from djangorestframework.compat import View
from djangorestframework.emitters import BaseEmitter
from djangorestframework.mixins import ResponseMixin
from djangorestframework.response import Response

DUMMYSTATUS = 200
DUMMYCONTENT = 'dummycontent'

EMITTER_A_SERIALIZER = lambda x: 'Emitter A: %s' % x
EMITTER_B_SERIALIZER = lambda x: 'Emitter B: %s' % x

class MockView(ResponseMixin, View):
    def get(self, request):
        response = Response(DUMMYSTATUS, DUMMYCONTENT)
        return self.emit(response)

class EmitterA(BaseEmitter):
    media_type = 'mock/emittera'

    def emit(self, output, verbose=False):
        return EMITTER_A_SERIALIZER(output)

class EmitterB(BaseEmitter):
    media_type = 'mock/emitterb'

    def emit(self, output, verbose=False):
        return EMITTER_B_SERIALIZER(output)


urlpatterns = patterns('',
    url(r'^$', MockView.as_view(emitters=[EmitterA, EmitterB])),
)


class EmitterIntegrationTests(TestCase):
    """End-to-end testing of emitters using an EmitterMixin on a generic view."""

    urls = 'djangorestframework.tests.emitters'

    def test_default_emitter_serializes_content(self):
        """If the Accept header is not set the default emitter should serialize the response."""
        resp = self.client.get('/')
        self.assertEquals(resp['Content-Type'], EmitterA.media_type)
        self.assertEquals(resp.content, EMITTER_A_SERIALIZER(DUMMYCONTENT))
        self.assertEquals(resp.status_code, DUMMYSTATUS)

    def test_default_emitter_serializes_content_on_accept_any(self):
        """If the Accept header is set to */* the default emitter should serialize the response."""
        resp = self.client.get('/', HTTP_ACCEPT='*/*')
        self.assertEquals(resp['Content-Type'], EmitterA.media_type)
        self.assertEquals(resp.content, EMITTER_A_SERIALIZER(DUMMYCONTENT))
        self.assertEquals(resp.status_code, DUMMYSTATUS)

    def test_specified_emitter_serializes_content_default_case(self):
        """If the Accept header is set the specified emitter should serialize the response.
        (In this case we check that works for the default emitter)"""
        resp = self.client.get('/', HTTP_ACCEPT=EmitterA.media_type)
        self.assertEquals(resp['Content-Type'], EmitterA.media_type)
        self.assertEquals(resp.content, EMITTER_A_SERIALIZER(DUMMYCONTENT))
        self.assertEquals(resp.status_code, DUMMYSTATUS)

    def test_specified_emitter_serializes_content_non_default_case(self):
        """If the Accept header is set the specified emitter should serialize the response.
        (In this case we check that works for a non-default emitter)"""
        resp = self.client.get('/', HTTP_ACCEPT=EmitterB.media_type)
        self.assertEquals(resp['Content-Type'], EmitterB.media_type)
        self.assertEquals(resp.content, EMITTER_B_SERIALIZER(DUMMYCONTENT))
        self.assertEquals(resp.status_code, DUMMYSTATUS)
    
    def test_unsatisfiable_accept_header_on_request_returns_406_status(self):
        """If the Accept header is unsatisfiable we should return a 406 Not Acceptable response."""
        resp = self.client.get('/', HTTP_ACCEPT='foo/bar')
        self.assertEquals(resp.status_code, 406) </s> add  </s> remove # parse_qs 
 </s> add  </s> remove     authenticators = ()
 </s> add     authentication = ()
        for authentication_cls in self.authentication:
            authentication = authentication_cls(self)
            auth = authentication.authenticate(self.request)
              self._auth = self._authenticate()
          return self._auth
  
      def _authenticate(self):
        for authentication_cls in self.authentication:
            authentication = authentication_cls(self)
            auth = authentication.authenticate(self.request)
        for authentication_cls in self.authentication:
            authentication = authentication_cls(self)
            auth = authentication.authenticate(self.request)
        for authentication_cls in self.authentication:
            authentication = authentication_cls(self)
            auth = authentication.authenticate(self.request)
              if auth:
                  return auth
          return None
  
      # TODO?
keep keep keep keep replace replace replace keep keep keep keep keep
 More refactoring - move various less core stuff into utils etc 
djangorestframework/mixins.py