def beginDrafting(context, event): """When we enter the edit screen, set up the target key and draft cookie path. If there is exactly one draft for the given user id and target key, consider that to be the current draft. Also mark the request with IDrafting if applicable. """ storage = queryUtility(IDraftStorage) if storage is None or not storage.enabled: return request = getattr(context, 'REQUEST', None) if request is None: return current = ICurrentDraftManagement(request) # Update target key regardless - we could have a stale cookie current.targetKey = getObjectKey(context) if current.draftName is None: drafts = storage.getDrafts(current.userId, current.targetKey) if len(drafts) == 1: current.draftName = tuple(drafts.keys())[0] # Save the path now so that we can use it again later, even on URLs # (e.g. in AJAX dialogues) that are below this path. current.path = current.defaultPath current.mark() current.save()
def test_save(self): request = self.request response = request.response current = ICurrentDraftManagement(request) self.assertEquals(False, current.save()) self.failIf('plone.app.drafts.targetKey' in response.cookies) self.failIf('plone.app.drafts.draftName' in response.cookies) self.failIf('plone.app.drafts.userId' in response.cookies) self.failIf('plone.app.drafts.path' in response.cookies) current.targetKey = u"123" self.assertEquals(True, current.save()) self.assertEquals({'value': '123', 'quoted': True, 'path': '/'}, response.cookies['plone.app.drafts.targetKey']) self.failIf('plone.app.drafts.draftName' in response.cookies) self.failIf('plone.app.drafts.path' in response.cookies) current.targetKey = u"123" current.draftName = u"draft-1" self.assertEquals(True, current.save()) self.assertEquals({'value': '123', 'quoted': True, 'path': '/'}, response.cookies['plone.app.drafts.targetKey']) self.assertEquals({'value': 'draft-1', 'quoted': True, 'path': '/'}, response.cookies['plone.app.drafts.draftName']) self.failIf('plone.app.drafts.path' in response.cookies) current.targetKey = u"123" current.draftName = u"draft-1" current.path = '/test' self.assertEquals(True, current.save()) self.assertEquals({'value': '123', 'quoted': True, 'path': '/test'}, response.cookies['plone.app.drafts.targetKey']) self.assertEquals({'value': 'draft-1', 'quoted': True, 'path': '/test'}, response.cookies['plone.app.drafts.draftName']) self.assertEquals({'value': '/test', 'quoted': True, 'path': '/test'}, response.cookies['plone.app.drafts.path'])
def test_save(self): request = self.request response = request.response current = ICurrentDraftManagement(request) self.assertEqual(False, current.save()) self.assertFalse('plone.app.drafts.targetKey' in response.cookies) self.assertFalse('plone.app.drafts.draftName' in response.cookies) self.assertFalse('plone.app.drafts.userId' in response.cookies) self.assertFalse('plone.app.drafts.path' in response.cookies) current.targetKey = u'123' self.assertEqual(True, current.save()) self.assertEqual( {'value': '123', 'quoted': True, 'path': '/'}, response.cookies['plone.app.drafts.targetKey'], ) self.assertFalse('plone.app.drafts.draftName' in response.cookies) self.assertFalse('plone.app.drafts.path' in response.cookies) current.targetKey = u'123' current.draftName = u'draft-1' self.assertEqual(True, current.save()) self.assertEqual( {'value': '123', 'quoted': True, 'path': '/'}, response.cookies['plone.app.drafts.targetKey'], ) self.assertEqual( {'value': 'draft-1', 'quoted': True, 'path': '/'}, response.cookies['plone.app.drafts.draftName'], ) self.assertFalse('plone.app.drafts.path' in response.cookies) current.targetKey = u'123' current.draftName = u'draft-1' current.path = '/test' # clear data del self.request.response.cookies['plone.app.drafts.targetKey'] del self.request.response.cookies['plone.app.drafts.draftName'] self.assertEqual(True, current.save()) self.assertEqual( {'value': '123', 'quoted': True, 'path': '/test'}, response.cookies['plone.app.drafts.targetKey'], ) self.assertEqual( {'value': 'draft-1', 'quoted': True, 'path': '/test'}, response.cookies['plone.app.drafts.draftName'], ) self.assertEqual( {'value': '/test', 'quoted': True, 'path': '/test'}, response.cookies['plone.app.drafts.path'], )
def __init__(self, form, request, context): fti = queryUtility(IDexterityFTI, name=form.portal_type) if isDraftable(fti): current = ICurrentDraftManagement(request) if current.targetKey is None: beginDrafting(context, None) current.path = '/'.join(context.getPhysicalPath()) current.targetKey = IUUID(context) current.save() else: current.mark() if current.draft: context = DraftProxy(current.draft, context) alsoProvides(request, IEditFormDrafting) super(DefaultEditFormFieldWidgets, self).__init__(form, request, context) # noqa
def __init__(self, form, request, context): fti = queryUtility(IDexterityFTI, name=form.portal_type) if isDraftable(fti): current = ICurrentDraftManagement(request) if current.targetKey != '++add++{0}'.format(form.portal_type): beginDrafting(context, None) current.path = '/'.join(context.getPhysicalPath()) current.targetKey = '++add++{0}'.format(form.portal_type) current.save() else: current.mark() target = getattr(current.draft, '_draftAddFormTarget', None) if current.draft and target: context = DraftProxy(current.draft, target.__of__(context)) alsoProvides(request, IAddFormDrafting) super(DefaultAddFormFieldWidgets, self).__init__(form, request, context) # noqa
def getCurrentDraft(request, create=False): """Get the current draft as stored in the request. The request must have been set up via an ``ICurrentDraftManagement`` adapter. This should happen in the integration layer between the drafts storage and the draft edit form. If no draft is available, but a user id and target key have been given, a new draft will be created if ``create`` is True. If not found, return None. """ current = ICurrentDraftManagement(request, None) if current is None: return None draft = current.draft if draft is not None: return draft if create and current.userId and current.targetKey: storage = queryUtility(IDraftStorage) if storage is None: return None draft = storage.createDraft(current.userId, current.targetKey) current.draft = draft current.draftName = draft.__name__ current.save() return draft return None