Skip to content

Commit

Permalink
Merge branch 'dnouri-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdonc committed Jan 19, 2012
2 parents 077f3d1 + f4b7fd8 commit e168597
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
9 changes: 7 additions & 2 deletions pyramid/config/views.py
Expand Up @@ -513,8 +513,13 @@ def add(self, view, order, accept=None, phash=None):
self.views.sort(key=operator.itemgetter(0))
else:
subset = self.media_views.setdefault(accept, [])
subset.append((order, view, phash))
subset.sort()
for i, (s, v, h) in enumerate(list(subset)):
if phash == h:
subset[i] = (order, view, phash)
return
else:
subset.append((order, view, phash))
subset.sort()
accepts = set(self.accepts)
accepts.add(accept)
self.accepts = list(accepts) # dedupe
Expand Down
31 changes: 29 additions & 2 deletions pyramid/tests/test_config/test_views.py
Expand Up @@ -621,6 +621,25 @@ def view2(context, request):
request.accept = DummyAccept('text/html', 'text/html')
self.assertEqual(wrapper(None, request), 'OK2')

def test_add_views_with_accept_multiview_replaces_existing(self):
from pyramid.renderers import null_renderer
def view(context, request):
return 'OK'
def view2(context, request):
return 'OK2'
def view3(context, request):
return 'OK3'
config = self._makeOne(autocommit=True)
config.add_view(view=view, renderer=null_renderer)
config.add_view(view=view2, accept='text/html', renderer=null_renderer)
config.add_view(view=view3, accept='text/html', renderer=null_renderer)
wrapper = self._getViewCallable(config)
self.assertEqual(len(wrapper.media_views['text/html']), 1)
self.assertEqual(wrapper(None, None), 'OK')
request = DummyRequest()
request.accept = DummyAccept('text/html', 'text/html')
self.assertEqual(wrapper(None, request), 'OK3')

def test_add_view_exc_with_accept_multiview_replaces_existing_view(self):
from pyramid.renderers import null_renderer
from zope.interface import implementedBy
Expand Down Expand Up @@ -1960,9 +1979,9 @@ def test_add(self):
self.assertEqual(mv.views, [(99, 'view2', None), (100, 'view', None)])
mv.add('view3', 100, 'text/html')
self.assertEqual(mv.media_views['text/html'], [(100, 'view3', None)])
mv.add('view4', 99, 'text/html')
mv.add('view4', 99, 'text/html', 'abc')
self.assertEqual(mv.media_views['text/html'],
[(99, 'view4', None), (100, 'view3', None)])
[(99, 'view4', 'abc'), (100, 'view3', None)])
mv.add('view5', 100, 'text/xml')
self.assertEqual(mv.media_views['text/xml'], [(100, 'view5', None)])
self.assertEqual(set(mv.accepts), set(['text/xml', 'text/html']))
Expand All @@ -1985,6 +2004,14 @@ def test_add_with_phash(self):
self.assertEqual(mv.views, [(100, 'view', 'abc'),
(100, 'view', 'def')])

def test_add_with_phash_override_accept(self):
mv = self._makeOne()
mv.add('view2', 100, accept='text/html', phash='abc')
mv.add('view3', 100, accept='text/html', phash='abc')
mv.add('view4', 99, accept='text/html', phash='def')
self.assertEqual(mv.media_views['text/html'],
[(99, 'view4', 'def'), (100, 'view3', 'abc')])

def test_multiple_with_functions_as_views(self):
# this failed on py3 at one point, because functions aren't orderable
# and we were sorting the views via a plain sort() rather than
Expand Down

0 comments on commit e168597

Please sign in to comment.