Skip to content

Commit

Permalink
Merge pull request #24 from plone/minimal-cleanup
Browse files Browse the repository at this point in the history
Minimal cleanup
  • Loading branch information
gforcada committed Mar 12, 2016
2 parents 86dedee + 98df4a5 commit 638a61a
Show file tree
Hide file tree
Showing 21 changed files with 214 additions and 147 deletions.
2 changes: 1 addition & 1 deletion plone/app/iterate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@
'of plone.app.iterate. You may experience problems running this configuration. '
'plone.app.iterate now has dexterity suport built-in.')
except ImportError:
pass
pass
1 change: 1 addition & 0 deletions plone/app/iterate/archiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import interfaces


class ContentArchiver(object):

implements(interfaces.IObjectArchiver)
Expand Down
14 changes: 9 additions & 5 deletions plone/app/iterate/browser/cancel.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from plone.app.iterate.interfaces import ICheckinCheckoutPolicy
from plone.app.iterate.interfaces import CheckoutException


class Cancel(BrowserView):

index = ViewPageTemplateFile('cancel.pt')
Expand All @@ -40,20 +41,23 @@ def __call__(self):
context = aq_inner(self.context)

if self.request.form.has_key('form.button.Cancel'):
control = getMultiAdapter((context, self.request), name=u"iterate_control")
control = getMultiAdapter(
(context, self.request), name=u"iterate_control")
if not control.cancel_allowed():
raise CheckoutException(u"Not a checkout")

policy = ICheckinCheckoutPolicy(context)
baseline = policy.cancelCheckout()
baseline.reindexObject()

IStatusMessage(self.request).addStatusMessage(_(u"Checkout cancelled"), type='info')
view_url = baseline.restrictedTraverse("@@plone_context_state").view_url()
IStatusMessage(self.request).addStatusMessage(
_(u"Checkout cancelled"), type='info')
view_url = baseline.restrictedTraverse(
"@@plone_context_state").view_url()
self.request.response.redirect(view_url)
elif self.request.form.has_key('form.button.Keep'):
view_url = context.restrictedTraverse("@@plone_context_state").view_url()
view_url = context.restrictedTraverse(
"@@plone_context_state").view_url()
self.request.response.redirect(view_url)
else:
return self.index()

13 changes: 9 additions & 4 deletions plone/app/iterate/browser/checkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from plone.app.iterate.interfaces import ICheckinCheckoutPolicy
from plone.app.iterate.interfaces import CheckinException


class Checkin(BrowserView):

index = ViewPageTemplateFile('checkin.pt')
Expand All @@ -40,7 +41,8 @@ def __call__(self):
context = aq_inner(self.context)

if self.request.form.has_key('form.button.Checkin'):
control = getMultiAdapter((context, self.request), name=u"iterate_control")
control = getMultiAdapter(
(context, self.request), name=u"iterate_control")
if not control.checkin_allowed():
raise CheckinException(u"Not a checkout")

Expand All @@ -49,11 +51,14 @@ def __call__(self):
policy = ICheckinCheckoutPolicy(context)
baseline = policy.checkin(message)

IStatusMessage(self.request).addStatusMessage(_("Checked in"), type='info')
view_url = baseline.restrictedTraverse("@@plone_context_state").view_url()
IStatusMessage(self.request).addStatusMessage(
_("Checked in"), type='info')
view_url = baseline.restrictedTraverse(
"@@plone_context_state").view_url()
self.request.response.redirect(view_url)
elif self.request.form.has_key('form.button.Cancel'):
view_url = context.restrictedTraverse("@@plone_context_state").view_url()
view_url = context.restrictedTraverse(
"@@plone_context_state").view_url()
self.request.response.redirect(view_url)
else:
return self.index()
21 changes: 14 additions & 7 deletions plone/app/iterate/browser/checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,32 +58,39 @@ def __call__(self):
# We want to redirect to a specific template, else we might
# end up downloading a file
if 'form.button.Checkout' in self.request.form:
control = getMultiAdapter((context, self.request), name=u"iterate_control")
control = getMultiAdapter(
(context, self.request), name=u"iterate_control")
if not control.checkout_allowed():
raise CheckoutException(u"Not allowed")

location = self.request.form.get('checkout_location', None)
locator = None
try:
locator = [c['locator'] for c in self.containers() if c['name'] == location][0]
locator = [c['locator']
for c in self.containers() if c['name'] == location][0]
except IndexError:
IStatusMessage(self.request).addStatusMessage(_("Cannot find checkout location"),
type='error')
view_url = context.restrictedTraverse("@@plone_context_state").view_url()
view_url = context.restrictedTraverse(
"@@plone_context_state").view_url()
self.request.response.redirect(view_url)
return

policy = ICheckinCheckoutPolicy(context)
wc = policy.checkout(locator())

# we do this for metadata update side affects which will update lock info
# we do this for metadata update side affects which will update
# lock info
context.reindexObject('review_state')

IStatusMessage(self.request).addStatusMessage(_("Check-out created"), type='info')
view_url = wc.restrictedTraverse("@@plone_context_state").view_url()
IStatusMessage(self.request).addStatusMessage(
_("Check-out created"), type='info')
view_url = wc.restrictedTraverse(
"@@plone_context_state").view_url()
self.request.response.redirect(view_url)
elif 'form.button.Cancel' in self.request.form:
view_url = context.restrictedTraverse("@@plone_context_state").view_url()
view_url = context.restrictedTraverse(
"@@plone_context_state").view_url()
self.request.response.redirect(view_url)
else:
return self.index()
1 change: 1 addition & 0 deletions plone/app/iterate/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def available(self):
def __call__(self):
return getToolByName(self.context, 'portal_membership').getHomeFolder()


class ParentFolderLocator(object):
"""Locate the parent of the context, if the user has the
Add portal content permission.
Expand Down
100 changes: 50 additions & 50 deletions plone/app/iterate/copier.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,76 +42,76 @@
from interfaces import CheckinException


class ContentCopier( object ):
class ContentCopier(object):

interface.implements( interfaces.IObjectCopier )
component.adapts( interfaces.IIterateAware )
interface.implements(interfaces.IObjectCopier)
component.adapts(interfaces.IIterateAware)

def __init__( self, context ):
def __init__(self, context):
self.context = context

def copyTo( self, container ):
wc = self._copyBaseline( container )
def copyTo(self, container):
wc = self._copyBaseline(container)
wc_ref = wc.addReference(
self.context,
relationship=WorkingCopyRelation.relationship,
referenceClass=WorkingCopyRelation)
self._handleReferences( self.context, wc, "checkout", wc_ref )
self._handleReferences(self.context, wc, "checkout", wc_ref)
return wc, wc_ref

def merge( self ):
def merge(self):
baseline = self._getBaseline()

# delete the working copy reference to the baseline
wc_ref = self._deleteWorkingCopyRelation()

# reassemble references on the new baseline
self._handleReferences( baseline, self.context, "checkin", wc_ref )
self._handleReferences(baseline, self.context, "checkin", wc_ref)

# move the working copy to the baseline container, deleting
# the baseline
new_baseline = self._replaceBaseline( baseline )
new_baseline = self._replaceBaseline(baseline)

# patch the working copy with baseline info not preserved
# during checkout
self._reassembleWorkingCopy( new_baseline, baseline )
self._reassembleWorkingCopy(new_baseline, baseline)

return new_baseline

def _getBaseline( self ):
def _getBaseline(self):
# follow the working copy's reference back to the baseline
refs = self.context.getRefs( WorkingCopyRelation.relationship )
refs = self.context.getRefs(WorkingCopyRelation.relationship)

if not len(refs) == 1:
raise CheckinException( "Baseline count mismatch" )
raise CheckinException("Baseline count mismatch")

if not refs or refs[0] is None:
raise CheckinException( "Baseline has disappeared" )
raise CheckinException("Baseline has disappeared")

baseline = refs[0]
return baseline

def _replaceBaseline( self, baseline ):
def _replaceBaseline(self, baseline):
# move the working copy object to the baseline, returns the
# new baseline
baseline_id = baseline.getId()

# delete the baseline from the folder to make room for the
# committed working copy
baseline_container = aq_parent( aq_inner( baseline ) )
baseline_container = aq_parent(aq_inner(baseline))
# Check if we are a default_page, because this property of the
# container might get lost.
is_default_page = (
baseline_container.getProperty('default_page', '') == baseline_id)
baseline_pos = baseline_container.getObjectPosition(baseline_id)
baseline_container._delOb( baseline_id )
baseline_container._delOb(baseline_id)

# uninedxing the deleted baseline object from portal_catalog
portal_catalog = getToolByName(self.context, 'portal_catalog')
portal_catalog.unindexObject(baseline)

# delete the working copy from the its container
wc_container = aq_parent( aq_inner( self.context ) )
wc_container = aq_parent(aq_inner(self.context))

# trick out the at machinery to not delete references
self.context._v_cp_refs = 1
Expand All @@ -121,12 +121,12 @@ def _replaceBaseline( self, baseline ):
wc_container.manage_delObjects([wc_id])

# move the working copy back to the baseline container
working_copy = aq_base( self.context )
working_copy.setId( baseline_id )
baseline_container._setOb( baseline_id, working_copy )
working_copy = aq_base(self.context)
working_copy.setId(baseline_id)
baseline_container._setOb(baseline_id, working_copy)
baseline_container.moveObjectToPosition(baseline_id, baseline_pos)

new_baseline = baseline_container._getOb( baseline_id )
new_baseline = baseline_container._getOb(baseline_id)
if is_default_page:
# Restore default_page to container. Note that the property might
# have been removed by an event handler in the mean time.
Expand All @@ -136,15 +136,15 @@ def _replaceBaseline( self, baseline ):
baseline_container._setProperty('default_page', baseline_id)

# reregister our references with the reference machinery after moving
Referenceable.manage_afterAdd( new_baseline, new_baseline,
baseline_container)
Referenceable.manage_afterAdd(new_baseline, new_baseline,
baseline_container)

notify(ObjectMovedEvent(new_baseline, wc_container,
wc_id, baseline_container, baseline_id))

return new_baseline

def _reassembleWorkingCopy( self, new_baseline, baseline ):
def _reassembleWorkingCopy(self, new_baseline, baseline):
# reattach the source's workflow history, try avoid a dangling ref
try:
new_baseline.workflow_history = PersistentMapping(
Expand All @@ -155,11 +155,11 @@ def _reassembleWorkingCopy( self, new_baseline, baseline ):

# reset wf state security directly
workflow_tool = getToolByName(self.context, 'portal_workflow')
wfs = workflow_tool.getWorkflowsFor( self.context )
wfs = workflow_tool.getWorkflowsFor(self.context)
for wf in wfs:
if not isinstance( wf, DCWorkflowDefinition ):
if not isinstance(wf, DCWorkflowDefinition):
continue
wf.updateRoleMappingsFor( new_baseline )
wf.updateRoleMappingsFor(new_baseline)

# Reattach the source's uid, this will update wc refs to point
# back to the new baseline. This may introduce duplicate
Expand All @@ -169,15 +169,15 @@ def _reassembleWorkingCopy( self, new_baseline, baseline ):
# reattach the source's history id, to get the previous
# version ancestry
histid_handler = getToolByName(self.context, 'portal_historyidhandler')
huid = histid_handler.getUid( baseline )
histid_handler.setUid( new_baseline, huid, check_uniqueness=False )
huid = histid_handler.getUid(baseline)
histid_handler.setUid(new_baseline, huid, check_uniqueness=False)

return new_baseline

def _recursivelyReattachUIDs(self, baseline, new_baseline):
original_refs = len(new_baseline.getRefs())
original_back_refs = len(new_baseline.getBRefs())
new_baseline._setUID( baseline.UID() )
new_baseline._setUID(baseline.UID())
new_refs = len(new_baseline.getRefs())
new_back_refs = len(new_baseline.getBRefs())
if original_refs != new_refs:
Expand Down Expand Up @@ -209,57 +209,57 @@ def _removeDuplicateReferences(self, item, backrefs=False):
if brain.getObject() is None:
reference_tool.uncatalog_object(brain.getPath())

def _deleteWorkingCopyRelation( self ):
def _deleteWorkingCopyRelation(self):
# delete the wc reference keeping a reference to it for its annotations
refs = self.context.getReferenceImpl(WorkingCopyRelation.relationship)
wc_ref = refs[0]
self.context.deleteReferences( WorkingCopyRelation.relationship )
self.context.deleteReferences(WorkingCopyRelation.relationship)
return wc_ref

#################################
## Checkout Support Methods
# Checkout Support Methods

def _copyBaseline( self, container ):
def _copyBaseline(self, container):
# copy the context from source to the target container
source_container = aq_parent( aq_inner( self.context ) )
source_container = aq_parent(aq_inner(self.context))
clipboard = source_container.manage_copyObjects([self.context.getId()])
result = container.manage_pasteObjects( clipboard )
result = container.manage_pasteObjects(clipboard)

# get a reference to the working copy
target_id = result[0]['new_id']
target = container._getOb( target_id )
target = container._getOb(target_id)
return target

def _handleReferences( self, baseline, wc, mode, wc_ref ):
def _handleReferences(self, baseline, wc, mode, wc_ref):

annotations = IAnnotations( wc_ref )
annotations = IAnnotations(wc_ref)

baseline_adapter = interfaces.ICheckinCheckoutReference( baseline )
baseline_adapter = interfaces.ICheckinCheckoutReference(baseline)

# handle forward references
for relationship in baseline.getRelationships():
# look for a named relation adapter first
adapter = component.queryAdapter(
baseline, interfaces.ICheckinCheckoutReference, relationship )
baseline, interfaces.ICheckinCheckoutReference, relationship)

if adapter is None: # default
adapter = baseline_adapter

references = baseline.getReferenceImpl( relationship )
references = baseline.getReferenceImpl(relationship)

mode_method = getattr( adapter, mode )
mode_method( baseline, wc, references, annotations )
mode_method = getattr(adapter, mode)
mode_method(baseline, wc, references, annotations)

mode = mode + "BackReferences"

# handle backward references
for relationship in baseline.getBRelationships():
adapter = component.queryAdapter(
baseline, interfaces.ICheckinCheckoutReference, relationship )
baseline, interfaces.ICheckinCheckoutReference, relationship)
if adapter is None:
adapter = baseline_adapter

references = baseline.getBackReferenceImpl( relationship )
references = baseline.getBackReferenceImpl(relationship)

mode_method = getattr( adapter, mode )
mode_method( baseline, wc, references, annotations )
mode_method = getattr(adapter, mode)
mode_method(baseline, wc, references, annotations)
Loading

0 comments on commit 638a61a

Please sign in to comment.