src/reportlab/platypus/doctemplate.py
changeset 4279 f69571262c74
parent 4278 5aa80e7cdba8
child 4330 617ffa6bbdc8
--- a/src/reportlab/platypus/doctemplate.py	Wed Jun 08 10:24:06 2016 +0100
+++ b/src/reportlab/platypus/doctemplate.py	Wed Jul 06 14:34:24 2016 +0100
@@ -29,6 +29,7 @@
 """
 
 from reportlab.platypus.flowables import *
+from reportlab.platypus.flowables import _ContainerSpace
 from reportlab.lib.units import inch
 from reportlab.platypus.paragraph import Paragraph
 from reportlab.platypus.frames import Frame
@@ -167,13 +168,14 @@
 
     def wrap(self, availWidth, availHeight):
         '''Should never be called.'''
-        raise NotImplementedError
+        raise NotImplementedError('%s.wrap should never be called' % self.__class__.__name__)
 
     def draw(self):
         '''Should never be called.'''
-        raise NotImplementedError
+        raise NotImplementedError('%s.draw should never be called' % self.__class__.__name__)
 
 class NextFrameFlowable(ActionFlowable):
+    locChanger = 1                  #we cause a frame or page change
     def __init__(self,ix,resume=0):
         ActionFlowable.__init__(self,('nextFrame',ix,resume))
 
@@ -214,10 +216,10 @@
 class FrameActionFlowable(Flowable):
     _fixedWidth = _fixedHeight = 1
     def __init__(self,*arg,**kw):
-        raise NotImplementedError('Abstract Class')
+        raise NotImplementedError('%s.__init__ should never be called for abstract Class'%self.__class__.__name__)
 
     def frameAction(self,frame):
-        raise NotImplementedError('Abstract Class')
+        raise NotImplementedError('%s.frameAction should never be called for abstract Class'%self.__class__.__name__)
 
 class Indenter(FrameActionFlowable):
     """Increases or decreases left and right margins of frame.
@@ -237,6 +239,7 @@
         frame._rightExtraIndent += self.right
 
 class NotAtTopPageBreak(FrameActionFlowable):
+    locChanger = 1                  #we cause a frame or page change
     def __init__(self,nextTemplate=None):
         self.nextTemplate = nextTemplate
 
@@ -245,6 +248,7 @@
             frame.add_generated_content(PageBreak(nextTemplate=self.nextTemplate))
 
 class NextPageTemplate(ActionFlowable):
+    locChanger = 1                  #we cause a frame or page change
     """When you get to the next page, use the template specified (change to two column, for example)  """
     def __init__(self,pt):
         ActionFlowable.__init__(self,('nextPageTemplate',pt))
@@ -391,6 +395,10 @@
     def onDrawStr(self,value,*args):
         return onDrawStr(value,self,encode_label(args))
 
+def _ktAllow(f):
+    '''return true if allowed in containers like KeepTogether'''
+    return not (isinstance(f,(_ContainerSpace,DocIf,DocWhile)) or getattr(f,'locChanger',False))
+
 class BaseDocTemplate:
     """
     First attempt at defining a document template class.
@@ -766,9 +774,9 @@
         "implements keepWithNext"
         i = 0
         n = len(flowables)
-        while i<n and flowables[i].getKeepWithNext(): i += 1
+        while i<n and flowables[i].getKeepWithNext() and _ktAllow(flowables[i]): i += 1
         if i:
-            if i<n and not getattr(flowables[i],'locChanger',None): i += 1
+            if i<n and _ktAllow(flowables[i]): i += 1
             K = KeepTogether(flowables[:i])
             mbe = getattr(self,'_multiBuildEdits',None)
             if mbe: