Submitted by : simon at: 2003-10-26T21:32:20+00:00 (14 years ago)
Name :
Category : Severity : Status :
Optional subject :  
Optional comment :

The "comment" box as a quick append mechanism can attract the features of "prepend", but that then warrants the desire to insert at a specific position or text marker.

The following code is a first draft modification of the "append" function which can do this.

(In my testing I added a checkbox to the comment form, along with a parameter to comment to switch to the "insert" instead of "append" function when called.)

Code:

security.declareProtected(Permissions.Comment, 'insert')
def insert(self, text='', separator='\n\n', REQUEST=None, log='',
            insertpoint='-1',insertmarker='<a name="insertpt" />'):
    """
    Inserts some text to an existing zwiki page by calling edit;
    may result in mail notifications to subscribers.
    """
    text = str(text)
    if text:
        oldtext = self.read()
        # find insertion point
        if str(insertpoint).isdigit():  # try with the input param (and has to be positive)
            insertpoint = int(str(insertpoint),10)
        else:
            insertpoint = -1
        if insertpoint == -1: # not using (or bad) given insertpoint, look for marker
            insertpoint = oldtext.find(insertmarker)
        if insertpoint == -1: # marker not found, pre-pend
            insertpoint = 0
        else:
            insertpoint = insertpoint + len(insertmarker) # gather insertion point (after, as insertion is the counter to the "append" function)
        # usability hack: scroll to bottom after adding a comment
        if REQUEST:
            REQUEST.set('redirectURL',REQUEST['URL1']+'#insertpt') # perhaps this should try to snag the "name" or "id" tag in the marker.
        # slicing snakes around 'index out of range' errors
        self.edit(text=oldtext[:insertpoint]+text+separator+oldtext[insertpoint:],
                  REQUEST=REQUEST,log=log)

Notes...

I haven't tested the insert by numeric position.

Seperator is a strange issue. Worst case scenario may require a pre,and post seperator parameter (Did I mention I truly appreciate Python's dynamic parameters in this context?) Uses: Page blogs - most recent addition on top, seperating propery change and comments (not recommended!), perhaps changing the issue additions to go in reverse order... , Entry forms (FAQ, list building)...

How to integrate into footer? Primarily leave that to customization, but perhaps highlighting if the page includes the insertion marker or a property.

Default marker probably not compatible with reStructuredText

Marker must exactly match. First marker found is used.

Tangent note: I'm surprised that a blank COMMENT entry (text) actually posts an item to a page. This may be a seperate issue, correctable by expanding on the "if text:" condition. (But then what do you say to the user?)