===============================================================
loops - Linked Objects for Organization and Processing Services
===============================================================
($Id$)
Let's do some basic setup
>>> from zope.app.testing.setup import placefulSetUp, placefulTearDown
>>> site = placefulSetUp(True)
>>> from zope import component, interface
and set up a simple loops site with a concept manager and some concepts
(with all the type machinery, what in real life is done via standard
ZCML setup):
>>> from loops.organize.setup import SetupManager
>>> component.provideAdapter(SetupManager, name='organize')
>>> from loops.organize.personal.setup import SetupManager
>>> component.provideAdapter(SetupManager, name='organize.personal')
>>> from loops.tests.setup import TestSite
>>> t = TestSite(site)
>>> concepts, resources, views = t.setup()
Favorites - Managed by a Tracking Storage
=========================================
>>> loopsRoot = concepts.getLoopsRoot()
>>> records = loopsRoot.getRecordManager()
>>> favorites = records['favorites']
User management setup
---------------------
In order to be able to login and store favorites and other personal data
we have to prepare our environment. We need some basic adapter registrations,
and a pluggable authentication utility with a principal folder.
>>> from loops.organize.tests import setupUtilitiesAndAdapters
>>> setupData = setupUtilitiesAndAdapters(loopsRoot)
>>> from zope.app.appsetup.bootstrap import ensureUtility
>>> from zope.app.authentication.authentication import PluggableAuthentication
>>> from zope.app.security.interfaces import IAuthentication
>>> ensureUtility(site, IAuthentication, '', PluggableAuthentication,
... copy_to_zlog=False, asObject=True)
<...PluggableAuthentication...>
>>> pau = component.getUtility(IAuthentication, context=site)
>>> from zope.app.authentication.principalfolder import PrincipalFolder
>>> from zope.app.authentication.interfaces import IAuthenticatorPlugin
>>> pFolder = PrincipalFolder('users.')
>>> pau['users'] = pFolder
>>> pau.authenticatorPlugins = ('users',)
So we can now register a user ...
>>> from zope.app.authentication.principalfolder import InternalPrincipal
>>> pFolder['john'] = InternalPrincipal('john', 'xx', u'John')
>>> from zope.app.authentication.principalfolder import FoundPrincipalFactory
>>> component.provideAdapter(FoundPrincipalFactory)
... and create a corresponding person.
>>> from loops.concept import Concept
>>> johnC = concepts['john'] = Concept(u'John')
>>> person = concepts['person']
>>> johnC.conceptType = person
>>> from loops.common import adapted
>>> adapted(johnC).userId = 'users.john'
Finally, we log in as the newly created user.
>>> from zope.app.authentication.principalfolder import Principal
>>> pJohn = Principal('users.john', 'xxx', u'John')
>>> from loops.tests.auth import login
>>> login(pJohn)
One step is still missing: As we are now working with a real principal
the security checks e.g. in views are active. So we have to provide
our user with the necessary permissions.
>>> grantPermission = setupData.rolePermissions.grantPermissionToRole
>>> assignRole = setupData.principalRoles.assignRoleToPrincipal
>>> grantPermission('zope.View', 'zope.Member')
>>> assignRole('zope.Member', 'users.john')
Working with the favorites storage
----------------------------------
The setup has provided us with a few resources, so there are objects we
can remember as favorites.
>>> list(resources.keys())
[u'd001.txt', u'd002.txt', u'd003.txt']
>>> from loops import util
>>> d001Id = util.getUidForObject(resources['d001.txt'])
>>> d003Id = util.getUidForObject(resources['d003.txt'])
>>> johnCId = util.getUidForObject(johnC)
We do not access the favorites storage directly but by using an adapter.
>>> from loops.organize.personal.favorite import Favorites
>>> component.provideAdapter(Favorites)
>>> from loops.organize.personal.interfaces import IFavorites
>>> favAdapted = IFavorites(favorites)
The adapter provides convenience methods for accessing the favorites storage.
>>> favAdapted.add(resources['d001.txt'], johnC)
'0000001'
So we are now ready to query the favorites.
>>> favs = favorites.query(userName=johnCId)
>>> favs
[<Favorite ['29', 1, '35', '...']: {}>]
>>> list(favAdapted.list(johnC))
['29']
>>> util.getObjectForUid(favs[0].taskId) is resources['d001.txt']
True
User interface
--------------
>>> from loops.view import Node
>>> home = views['home'] = Node()
>>> from loops.tests.auth import TestRequest
>>> from loops.organize.personal.browser.configurator import PortletConfigurator
>>> portletConf = PortletConfigurator(home, TestRequest())
>>> len(portletConf.viewProperties)
1
>>> from loops.organize.personal.browser.favorite import FavoriteView
>>> view = FavoriteView(home, TestRequest())
Let's now trigger the saving of a favorite.
>>> d002Id = util.getUidForObject(resources['d002.txt'])
>>> request = TestRequest(form=dict(id=d002Id))
>>> view = FavoriteView(home, request)
>>> view.add()
>>> len(favorites.query(userName=johnCId))
2
>>> d002Id = util.getUidForObject(resources['d001.txt'])
>>> request = TestRequest(form=dict(id=d002Id))
>>> view = FavoriteView(home, request)
>>> view.remove()
>>> len(favorites.query(userName=johnCId))
1