Add a JavaScript toggle to membership management

While unsubscribing or moderating multiple members quickly isn’t a common task, it’s extremely tedious when it becomes necessary.

Unfortunately, all of the HTML in Mailman is created inside the Python files. To add HTML, we need to edit the Python. In this case, the place to edit is Mailman/Cgi/admin.py’s “membership_options” function.

Right around line 1,138 there are lines that read:

  • container.AddItem(Center(usertable))
  • # There may be additional chunks
  • if chunkindex is not None:

That “AddItem” line is adding the table of members. We can add extra code directly below that table and it will appear on the Membership Management page that lists members.

First, we need to add a new tag type. In Mailman/htmlformat.py there is a “Link” class around line 208. Find it, and below it add a ToggleLink class:

[toggle code]

  • class ToggleLink(Link):
    • def __init__(self, key):
      • text = "Toggle %s for all visible members" % key
      • confirmation = "Toggle %s for all visible members?" %key
      • keysize = len(key)+1
      • href = "javascript:if(confirm('%s')) {var el=document.forms[0].elements;for (var i=0; i<el.length;i++) if (el[i].type=='checkbox' && el[i].name.substr(-%i)=='_%s') void(el[i].checked=!el[i].checked);}" % (confirmation, keysize, key)
      • self.href = href
      • self.text = text
      • self.target = None

This class will accept a form key, and will piggyback off of the Link class’s ability to return a link; this link, however, will be a javascript: link.

Go back to admin.py, and underneath where the usertable is added, do an AddItem for two ToggleLinks, surrounded by a line break:

  • container.AddItem(Center(usertable))
  • container.AddItem(ToggleLink("unsub"))
  • container.AddItem('<br />')
  • container.AddItem(ToggleLink("mod"))
  • # There may be additional chunks
  • if chunkindex is not None:

It’s not the prettiest HTML in the world, but it gets the job done. This will add two links below the membership list that, when clicked, will toggle the “mod” checkboxes or the “unsub” checkboxes on and off.