view website/issues/extensions/local_replace.py @ 5548:fea11d05110e

Avoid errors from selecting "no selection" on multilink (issue2550722). As discussed in issue 2550722 there are various cases where selecting "no selection" on a multilink can result in inappropriate errors from Roundup: * If selecting "no selection" produces a null edit (a value was set in the multilink in an edit with an error, then removed again, along with all other changes, in the next form submission), so the page is rendered from the form contents including the "-<id>" value for "no selection" for the multilink. * If creating an item with a nonempty value for a multilink has an error, and the resubmission changes that multilink to "no selection" (and this in turn has subcases, according to whether the creation then succeeds or fails on the resubmission, which need fixes in different places in the Roundup code). All of these cases have in common that it is expected and OK to have a "-<id>" value for a submission for a multilink when <id> is not set in that multilink in the database (because the original attempt to set <id> in that multilink had an error), so the hyperdb.py logic to give an error in that case is thus removed. In the subcase of the second case where the resubmission with "no selection" has an error, the templating code tries to produce a menu entry for the "-<id>" multilink value, which also results in an error, hence the templating.py change to ignore such values in the list for a multilink.
author Joseph Myers <jsm@polyomino.org.uk>
date Thu, 27 Sep 2018 11:33:01 +0000
parents 9af22cfa3a2b
children 042c50d5e06e
line wrap: on
line source

from __future__ import print_function
import re

hg_url_base = r'http://sourceforge.net/p/roundup/code/ci/'

substitutions = [ (re.compile(r'debian:\#(?P<id>\d+)'),
                   r'<a href="http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=\g<id>">debian#\g<id></a>' ),
                  (re.compile(r'\#(?P<ws>\s*)(?P<id>\d+)'),
                   r"<a href='issue\g<id>'>#\g<ws>\g<id></a>" ),
                  (re.compile(r'(?P<prews>^|\s+)issue(?P<ws>\s*)(?P<id>\d+)'),
                   r"\g<prews><a href='issue\g<id>'>issue\g<ws>\g<id></a>" ),
                  # matching the typical number:hash format of hg's own output
                  # and then use use hash instead of the number
                  (re.compile(r'(?P<prews>(^|\s+))(?P<revstr>(rev|hg|changeset:   ))(?P<revnumber>\d+):(?P<refhash>[0-9a-fA-F]{12,40})(?P<post>\W+|$)'),
                      r'\g<prews><a href="' + hg_url_base + '\g<refhash>">\g<revstr>\g<revnumber>:\g<refhash></a>\g<post>'),
                  # matching hg revison number or hash
                  (re.compile(r'(?P<prews>(^|\s+))(?P<revstr>(revision|rev|r)\s?)(?P<revision>([1-9][0-9]*)|[0-9a-fA-F]{4,40})(?P<post>\W+|$)'),
                   r'\g<prews><a href="' + hg_url_base + '\g<revision>">\g<revstr>\g<revision></a>\g<post>'),
                  ]

def local_replace(message):

    for cre, replacement in substitutions:
        message = cre.sub(replacement, message)

    return message


def init(instance):
    instance.registerUtil('localReplace', local_replace)

def quicktest(msgstr, should_replace = True):
    testcount['run'] += 1
    replacedstr = local_replace(msgstr)

    if not (not replacedstr == msgstr ) == should_replace:
        print("(fail)", end=' ')
        testcount['failed'] += 1

    if replacedstr == msgstr:
        print( "'%s'" % (msgstr,))
    else:
        print("'%s' -> '%s'" % (msgstr, replacedstr))

if "__main__" == __name__:
    testcount = {'run':0 , 'failed': 0}
    print("Replacement examples:")
    quicktest(" debian:#222")
    quicktest(" #555")
    quicktest("issue333")
    quicktest(" revision 222")
    quicktest(" r 222")
    quicktest(" wordthatendswithr 222", False)
    quicktest(" references", False)
    quicktest(" too many spaces r  222", False)
    quicktest("re-evaluate", False)
    quicktest("rex140eb", False)
    quicktest("rev 012", False) # too short for a hg hash
    quicktest("rev 0123")
    quicktest("re140eb")
    quicktest(" r7140eb")
    quicktest(" rev7140eb ")
    quicktest("rev7140eb")
    quicktest("rev7140eb,")
    quicktest("rev4891:ad3d628e73f2")
    quicktest("hg4891:ad3d628e73f2")
    quicktest("changeset:   4542:46239c21a1eb")
    print()
    print(testcount)

Roundup Issue Tracker: http://roundup-tracker.org/