コード例 #1
0
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 ''
コード例 #2
0
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 ''
コード例 #3
0
 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)
コード例 #4
0
 def __getitem__(self, key):
     portlet = Portlet.by_id(key)
     if portlet:
         portlet.__parent__ = self
         portlet.__name__ = key
         return portlet
     else:
         raise KeyError
コード例 #5
0
 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')
コード例 #6
0
    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.'])
コード例 #7
0
 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')
コード例 #8
0
    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',
        )
コード例 #9
0
 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')
コード例 #10
0
 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.',
         })
コード例 #11
0
    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.'])
コード例 #12
0
 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)
コード例 #13
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)
コード例 #14
0
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,
    )
コード例 #15
0
    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))
コード例 #16
0
 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)
コード例 #17
0
 def test_invalid_position(self):
     portlets = Portlet.by_user_and_position(User(), 'foo')
     self.assertEqual(portlets, [])
コード例 #18
0
def _make_portlet(name='foo', **kwargs):
    portlet = Portlet(name=name, **kwargs)
    Session.add(portlet)
    return portlet
コード例 #19
0
 def test_invalid_user(self):
     portlets = Portlet.by_user_and_position(None, 'above_content')
     self.assertEqual(portlets, [])
コード例 #20
0
 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')
コード例 #21
0
 def test_no_portlets(self):
     portlets = Portlet.get_all()
     self.assertEqual(len(portlets), 0)
コード例 #22
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)
コード例 #23
0
 def test_appstruct_empty_context(self):
     self.request.context = Portlet()
     self.assertEqual(self.view.appstruct(), {})