def test_discard(self): request = self.request response = request.response current = ICurrentDraftManagement(request) current.discard() expires = 'Wed, 31-Dec-97 23:59:59 GMT' try: # expires date changed in ZPublisher.HTTPResponse # see https://github.com/zopefoundation/Zope/commit/77f483a22d6b0cb00883006cf38928cda77b75f9 # noqa zope_version = pkg_resources.get_distribution('Zope').version if parse_version(zope_version) >= parse_version('4.0b8'): expires = 'Wed, 31 Dec 1997 23:59:59 GMT' except Exception: pass deletedToken = { 'expires': expires, 'max_age': 0, 'path': '/', 'quoted': True, 'value': 'deleted' } self.assertEqual( deletedToken, response.cookies['plone.app.drafts.targetKey'], ) self.assertEqual( deletedToken, response.cookies['plone.app.drafts.draftName'], ) self.assertEqual( deletedToken, response.cookies['plone.app.drafts.path'], ) current.path = '/test' current.discard() deletedToken['path'] = '/test' self.assertEqual( deletedToken, response.cookies['plone.app.drafts.targetKey'], ) self.assertEqual( deletedToken, response.cookies['plone.app.drafts.draftName'], ) self.assertEqual( deletedToken, response.cookies['plone.app.drafts.path'], )
def test_discard(self): request = self.request response = request.response current = ICurrentDraftManagement(request) current.discard() expires = 'Wed, 31-Dec-97 23:59:59 GMT' try: # expires date changed in ZPublisher.HTTPResponse # see https://github.com/zopefoundation/Zope/commit/77f483a22d6b0cb00883006cf38928cda77b75f9 # noqa zope_version = pkg_resources.get_distribution('Zope').version if parse_version(zope_version) >= parse_version('4.0b8'): expires = 'Wed, 31 Dec 1997 23:59:59 GMT' except Exception: pass deletedToken = {'expires': expires, 'max_age': 0, 'path': '/', 'quoted': True, 'value': 'deleted'} self.assertEqual( deletedToken, response.cookies['plone.app.drafts.targetKey'], ) self.assertEqual( deletedToken, response.cookies['plone.app.drafts.draftName'], ) self.assertEqual( deletedToken, response.cookies['plone.app.drafts.path'], ) current.path = '/test' current.discard() deletedToken['path'] = '/test' self.assertEqual( deletedToken, response.cookies['plone.app.drafts.targetKey'], ) self.assertEqual( deletedToken, response.cookies['plone.app.drafts.draftName'], ) self.assertEqual( deletedToken, response.cookies['plone.app.drafts.path'], )
def discardDraftsOnCancel(context, event): """When the edit form is cancelled, discard any unused drafts and remove the drafting cookies. """ 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) if current.userId and current.targetKey: storage.discardDrafts(current.userId, current.targetKey) current.discard()
def test_discard(self): request = self.request response = request.response current = ICurrentDraftManagement(request) current.discard() deletedToken = {'expires': 'Wed, 31-Dec-97 23:59:59 GMT', 'max_age': 0, 'path': '/', 'quoted': True, 'value': 'deleted'} self.assertEqual( deletedToken, response.cookies['plone.app.drafts.targetKey'], ) self.assertEqual( deletedToken, response.cookies['plone.app.drafts.draftName'], ) self.assertEqual( deletedToken, response.cookies['plone.app.drafts.path'], ) current.path = '/test' current.discard() deletedToken['path'] = '/test' self.assertEqual( deletedToken, response.cookies['plone.app.drafts.targetKey'], ) self.assertEqual( deletedToken, response.cookies['plone.app.drafts.draftName'], ) self.assertEqual( deletedToken, response.cookies['plone.app.drafts.path'], )
def syncDraftOnSave(context, event): """When the edit form is saved, sync the draft (if set) and discard it. Also discard the drafting cookies. """ storage = queryUtility(IDraftStorage) if storage is None or not storage.enabled: return request = getattr(context, 'REQUEST', None) if request is None: return draft = getCurrentDraft(request) if draft is not None: syncDraft(draft, context) current = ICurrentDraftManagement(request) if current.userId and current.targetKey: storage.discardDrafts(current.userId, current.targetKey) current.discard()