def below_sidebar_portlets(context, request): if request.user: portlets = Portlet.by_user_and_position( request.user, PortletPositions.below_sidebar.name) if portlets: return render_portlets(portlets) return ''
def above_footer_portlets(context, request): if request.user: portlets = Portlet.by_user_and_position( request.user, PortletPositions.above_footer.name) if portlets: return render_portlets(portlets) return ''
def test_no_group(self): group = _make_group() _make_user(groups=[ group, ]) _make_portlet(name='foo', groups=[], position='above_content') portlets = Portlet.by_user_and_position(User.by_id(1), 'above_content') self.assertEqual(len(portlets), 0)
def __getitem__(self, key): portlet = Portlet.by_id(key) if portlet: portlet.__parent__ = self portlet.__name__ = key return portlet else: raise KeyError
def test_override_ordered_by(self): _make_portlet(name='foo', weight=3) _make_portlet(name='bar', weight=2) _make_portlet(name='baz', weight=-1) portlets = Portlet.get_all(order_by='weight') self.assertEqual(len(portlets), 3) self.assertEqual(portlets[0].name, 'baz') self.assertEqual(portlets[1].name, 'bar') self.assertEqual(portlets[2].name, 'foo')
def test_save_success(self): self.request.context = Portlet.by_id(1) result = self.view.save_success(self.APPSTRUCT) self.assertIsInstance(result, HTTPFound) self.assertEqual(result.location, '/portlet/1/edit/') portlet = Portlet.by_id(1) self.assertEqual(portlet.name, 'bar') self.assertEqual(portlet.groups, [Group.by_id(2), Group.by_id(3)]) self.assertEqual(portlet.exclude_groups, [ Group.by_id(4), ]) self.assertEqual(portlet.position, PortletPositions.above_content.name) self.assertEqual(portlet.weight, -10) self.assertEqual(portlet.html, u'Bar') self.assertEqual(self.request.session.pop_flash(), [u'Portlet "bar" modified.'])
def test_ordered_by_position_by_default(self): _make_portlet(name='foo', position='above_content') _make_portlet(name='bar', position='above_footer') _make_portlet(name='bla', position='above_sidebar') _make_portlet(name='baz', position='below_sidebar') portlets = Portlet.get_all() self.assertEqual(len(portlets), 4) self.assertEqual(portlets[0].name, 'foo') self.assertEqual(portlets[1].name, 'bar') self.assertEqual(portlets[2].name, 'bla') self.assertEqual(portlets[3].name, 'baz')
def test_user_with_multiple_portlets(self, MockedPortlet): request = testing.DummyRequest(user=mock.Mock(specs=())) MockedPortlet.by_user_and_position.return_value = [ Portlet(html=u'foö'), Portlet(html=u'bär') ] self.assertEqual( above_content_portlets(self.context, request), u'<div class="well">foö</div>\n<div class="well">bär</div>\n', ) self.assertEqual( above_sidebar_portlets(self.context, request), u'<div class="well">foö</div>\n<div class="well">bär</div>\n', ) self.assertEqual( below_sidebar_portlets(self.context, request), u'<div class="well">foö</div>\n<div class="well">bär</div>\n', ) self.assertEqual( above_footer_portlets(self.context, request), u'<div class="well">foö</div>\n<div class="well">bär</div>\n', )
def test_position_filter(self): group = _make_group() _make_user(groups=[ group, ]) _make_portlet(name='foo', groups=[ group, ], position='above_content') _make_portlet(name='bar', groups=[ group, ], position='above_footer') portlets = Portlet.by_user_and_position(User.by_id(1), 'above_content') self.assertEqual(len(portlets), 1) self.assertEqual(portlets[0].name, 'foo')
def test_appstruct_full_context(self): self.request.context = Portlet.by_id(1) self.assertEqual( self.view.appstruct(), { 'name': 'dummy', 'groups': [ '1', ], 'exclude_groups': [ '3', ], 'position': PortletPositions.below_sidebar.name, 'weight': -127, 'html': u'You are admin.', })
def test_submit_success(self): result = self.view.submit_success(self.APPSTRUCT) self.assertIsInstance(result, HTTPFound) self.assertEqual(result.location, '/portlet/1/edit/') portlet = Portlet.by_id(1) self.assertEqual(portlet.name, 'foo') self.assertEqual(portlet.groups, [Group.by_id(1)]) self.assertEqual(portlet.exclude_groups, [Group.by_id(2)]) self.assertEqual(portlet.position, PortletPositions.below_sidebar.name) self.assertEqual(portlet.weight, 10) self.assertEqual(portlet.html, u'Foo') self.assertEqual(self.request.session.pop_flash(), [u'Portlet "foo" added.'])
def test_user_in_group_and_exclude_group(self): group1 = _make_group() group2 = _make_group(name='bar') _make_user(groups=[ group1, group2, ]) _make_portlet(name='foo', groups=[ group1, ], exclude_groups=[ group2, ], position='above_content') portlets = Portlet.by_user_and_position(User.by_id(1), 'above_content') self.assertEqual(len(portlets), 0)
def add_demo_portlet(): """Create a dummy portlet.""" with transaction.manager: admins = Group.by_name('admins') enabled = Group.by_name('enabled') portlet = Portlet( name='dummy', groups=[ admins, ], exclude_groups=[ enabled, ], position=PortletPositions.below_sidebar.name, weight=-127, html=u'You are admin.', ) Session.add(portlet)
def _make_portlet(id=1, name='foo', groups=None, exclude_groups=None, position=PortletPositions.below_sidebar, weight=0, html=u'Foo Portlet'): if not groups: # pragma: no branch groups = [_make_group()] if exclude_groups is None: exclude_groups = [] return Portlet( id=id, name=name, groups=groups, exclude_groups=exclude_groups, position=position, weight=weight, html=html, )
def submit_success(self, appstruct): portlet = Portlet( name=appstruct.get('name'), groups=[ Group.by_id(group_id) for group_id in appstruct.get('groups') ], # noqa exclude_groups=[ Group.by_id(group_id) for group_id in appstruct.get('exclude_groups') ], # noqa position=appstruct.get('position'), weight=appstruct.get('weight'), html=appstruct.get('html'), ) Session.add(portlet) Session.flush() self.request.session.flash(u'Portlet "{}" added.'.format(portlet.name)) return HTTPFound(location=self.request.route_path( 'portlet_edit', portlet_id=portlet.id))
def test_portlets_limit(self): _make_portlet(name='foo') _make_portlet(name='bar') _make_portlet(name='baz') portlets = Portlet.get_all(limit=2) self.assertEqual(len(portlets), 2)
def test_invalid_position(self): portlets = Portlet.by_user_and_position(User(), 'foo') self.assertEqual(portlets, [])
def _make_portlet(name='foo', **kwargs): portlet = Portlet(name=name, **kwargs) Session.add(portlet) return portlet
def test_invalid_user(self): portlets = Portlet.by_user_and_position(None, 'above_content') self.assertEqual(portlets, [])
def test_filter_by(self): _make_portlet(name='foo', position='above_content') _make_portlet(name='bar', position='above_footer') portlets = Portlet.get_all(filter_by={'position': 'above_content'}) self.assertEqual(len(portlets), 1) self.assertEqual(portlets[0].name, 'foo')
def test_no_portlets(self): portlets = Portlet.get_all() self.assertEqual(len(portlets), 0)
def test_all_portlets(self): _make_portlet(name='foo') _make_portlet(name='bar') _make_portlet(name='baz') portlets = Portlet.get_all() self.assertEqual(len(portlets), 3)
def test_appstruct_empty_context(self): self.request.context = Portlet() self.assertEqual(self.view.appstruct(), {})