[entity] introduce a new 'adapters' registry
This changeset introduces the notion in adapters (as in Zope Component Architecture)
in a cubicweb way, eg using a specific registry of appobjects.
This allows nicer code structure, by avoid clutering entity classes and moving
code usually specific to a place of the ui (or something else) together with the
code that use the interface.
We don't use actual interface anymore, they are implied by adapters (which
may be abstract), whose reg id is an interface name.
Appobjects that used to 'implements(IFace)' should now be rewritten by:
* coding an IFaceAdapter(EntityAdapter) defining (implementing if desired)
the interface, usually with __regid__ = 'IFace'
* use "adaptable('IFace')" as selector instead
Also, the implements_adapter_compat decorator eases backward compatibility
with adapter's methods that may still be found on entities implementing
the interface.
Notice that unlike ZCA, we don't support automatic adapters chain (yagni?).
All interfaces defined in cubicweb have been turned into adapters, also
some new ones have been introduced to cleanup Entity / AnyEntity classes
namespace. At the end, the pluggable mixins mecanism should disappear in
favor of adapters as well.
# -*- coding: iso-8859-1 -*-
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of CubicWeb.
#
# CubicWeb is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>.
"""
"""
from socket import gethostname
from logilab.common.testlib import unittest_main, TestCase
from cubicweb.devtools.testlib import CubicWebTC, MAILBOX
from cubicweb.mail import construct_message_id, parse_message_id
class MessageIdTC(TestCase):
def test_base(self):
msgid1 = construct_message_id('testapp', 21)
msgid2 = construct_message_id('testapp', 21)
self.failIfEqual(msgid1, msgid2)
self.failIf('&' in msgid1)
self.failIf('=' in msgid1)
self.failIf('/' in msgid1)
self.failIf('+' in msgid1)
values = parse_message_id(msgid1, 'testapp')
self.failUnless(values)
# parse_message_id should work with or without surrounding <>
self.failUnlessEqual(values, parse_message_id(msgid1[1:-1], 'testapp'))
self.failUnlessEqual(values['eid'], '21')
self.failUnless('timestamp' in values)
self.failUnlessEqual(parse_message_id(msgid1[1:-1], 'anotherapp'), None)
def test_notimestamp(self):
msgid1 = construct_message_id('testapp', 21, False)
msgid2 = construct_message_id('testapp', 21, False)
values = parse_message_id(msgid1, 'testapp')
self.failUnlessEqual(values, {'eid': '21'})
def test_parse_message_doesnt_raise(self):
self.failUnlessEqual(parse_message_id('oijioj@bla.bla', 'tesapp'), None)
self.failUnlessEqual(parse_message_id('oijioj@bla', 'tesapp'), None)
self.failUnlessEqual(parse_message_id('oijioj', 'tesapp'), None)
def test_nonregr_empty_message_id(self):
for eid in (1, 12, 123, 1234):
msgid1 = construct_message_id('testapp', eid, 12)
self.assertNotEquals(msgid1, '<@testapp.%s>' % gethostname())
class RecipientsFinderTC(CubicWebTC):
def test(self):
urset = self.execute('CWUser X WHERE X login "admin"')
self.execute('INSERT EmailAddress X: X address "admin@logilab.fr", U primary_email X '
'WHERE U eid %(x)s', {'x': urset[0][0]})
self.execute('INSERT CWProperty X: X pkey "ui.language", X value "fr", X for_user U '
'WHERE U eid %(x)s', {'x': urset[0][0]})
self.commit() # commit so that admin get its properties updated
finder = self.vreg['components'].select('recipients_finder',
self.request(), rset=urset)
self.set_option('default-recipients-mode', 'none')
self.assertEquals(finder.recipients(), [])
self.set_option('default-recipients-mode', 'users')
self.assertEquals(finder.recipients(), [(u'admin@logilab.fr', 'fr')])
self.set_option('default-recipients-mode', 'default-dest-addrs')
self.set_option('default-dest-addrs', 'abcd@logilab.fr, efgh@logilab.fr')
self.assertEquals(finder.recipients(), [('abcd@logilab.fr', 'en'), ('efgh@logilab.fr', 'en')])
class StatusChangeViewsTC(CubicWebTC):
def test_status_change_view(self):
req = self.request()
u = self.create_user('toto', req=req)
u.cw_adapt_to('IWorkflowable').fire_transition('deactivate', comment=u'yeah')
self.failIf(MAILBOX)
self.commit()
self.assertEquals(len(MAILBOX), 1)
email = MAILBOX[0]
self.assertEquals(email.content,
'''
admin changed status from <activated> to <deactivated> for entity
'toto'
yeah
url: http://testing.fr/cubicweb/cwuser/toto
''')
self.assertEquals(email.subject, 'status changed cwuser #%s (admin)' % u.eid)
if __name__ == '__main__':
unittest_main()