Attachment 'SubscribeTo-moin19.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - Automaticaly subscribe user(s) on save
   4 
   5     [[SubscribeTo(UserName1[,+GroupName1[,-UserName2]])]]
   6     
   7     Will add those users and/or group of users as subscribers while saving the
   8     page. These users are not subscribed for templates nor while preview the
   9     page. To subscribe to this template the FQTN (full-qualified template name)
  10     has to be specified as one of the parameters.
  11 
  12     Using (-) and (+) it's possible to create a flexible list of users will
  13     be subscribed to this page. E.g. select a long group and remove with (-)
  14     some group members. Or select a short group and extend the list by adding
  15     (+) additional users. By default users are added if no (-) or (+) is
  16     specified. (-) does not remove the subscribtion for the specified user.
  17     (-) does not change the status of the subscription for the specified user.
  18 
  19     @copyright: (C) 2005,2006  Raphael Bossek
  20     @license: GNU GPL 2, see COPYING for details
  21     @version: 20060402
  22     @url: http://www.solutions4linux.de/cgi-bin/view/Main/MoinMoinSubscribeTo
  23 """
  24 
  25 from MoinMoin import user
  26 from MoinMoin.datastruct import WikiGroups
  27 import re
  28 
  29 def execute (macro, args):
  30 	_ = macro.request.getText
  31 	unknownuname = {}
  32 	missingemail = {}
  33 	outofdateversion = 0
  34 	result = u''
  35 	allusers = re.split (r'[,;\s]+', args)
  36 	subscribeto = {}
  37 	page_name = macro.request.page.page_name
  38 	is_preview = macro.request.form.has_key ('button_preview')
  39 	p_warning = u'<p class="warning" style="margin: 0; padding: 0.4em; background: #FFFF80">' + macro.formatter.icon ("info") + u' '
  40 	p_notice = u'<p class="notice" style="margin: 0; padding: 0.4em; background: #81BBF2">' + macro.formatter.icon ("info") + u' '
  41 	p_info = u'<p class="info" style="margin: 0; padding: 0.4em; background: #E6EAF0">' + macro.formatter.icon ("info") + u' '
  42         wikigroups = WikiGroups(macro.request)
  43 	# TODO: Should be compiled only once, and cached in cfg
  44 	group_re = re.compile (macro.cfg.page_group_regex, re.UNICODE)
  45 	# By default templates are not subsribed. This can be changed by
  46 	# specifing the FQTN as parameter.
  47 	is_template = re.search (macro.cfg.page_template_regex, page_name, re.UNICODE)
  48 	# Proceed the list in the same order as specified by the user.
  49 	# This allow us to work with groups and users in the same way
  50 	# as specifing all members of a group.
  51 	for uname in allusers:
  52 		# Skip empty names
  53 		if not uname:
  54 			continue
  55 		# Determine what to do with the user/group members.
  56 		if uname[:1] == '-':
  57 			uname = uname[1:]
  58 			remove_action = True
  59 		else:
  60 			if uname[:1] == '+':
  61 				uname = uname[1:]
  62 			remove_action = False
  63 
  64 		# It's a special feature. If the FQTN (full-qualified template name)
  65 		# is one of the parameters we allow to subscribe to this template too.
  66 		if remove_action == False and uname == page_name:
  67 			is_template = False
  68 			continue
  69 
  70 		members = []
  71 		# Distinguish between user and group. Create a list of
  72 		# users which has to be subscribed to.
  73 		if group_re.search (uname):
  74 			# Recursively expand groups
  75 			if uname in wikigroups: 
  76 				members = wikigroups._retrieve_members(uname)
  77 			else:
  78 				unknownuname[uname] = 1
  79 		else:
  80 			members = [uname]
  81 
  82 		# Go through all members and proceed with the same action.
  83 		for uname in members:
  84 			uid = user.getUserId (macro.request, uname)
  85 			if not uid:
  86 				# Only if this user has to be added and does not exists put
  87 				# his name in our unknwonuname list. Otherwise the use does
  88 				# not matter.
  89 				if remove_action == False:
  90 					unknownuname[uname] = 1
  91 				# If this user was added before but we know now that should
  92 				# be skipped we update the unknownuname list afterward.
  93 				else:
  94 					if uname in subscribeto:
  95 						del subscribeto[uname]
  96 					if uname in unknownuname:
  97 						del unknownuname[uname]
  98 			else:
  99 				thisuser = user.User (macro.request, id = uid)
 100 				if not thisuser.email:
 101 					missingemail[uname] = 1
 102 				else:
 103 					subscribeto[uname] = (thisuser, remove_action)
 104 				# MoinMoin compatibility check
 105 				if not 'subscribePage' in dir (thisuser) and not 'subscribe' in dir (thisuser):
 106 					outofdateversion = 1
 107 					break
 108 	if not outofdateversion:
 109 		if unknownuname:
 110 			result += p_warning + _(u'This user(s)/group(s) are unknown by now: %s') % u', '.join (unknownuname.keys()) + u'</p>'
 111 		if missingemail:
 112 			result += p_warning + _(u'Follwing users(s) missing an email address in their profile: %s') % u', '.join (missingemail.keys()) + u'</p>'
 113 		if subscribeto:
 114 			addeduname = []
 115 			faileduname = []
 116 			subscribeduname = []
 117 			subscribe_status = (1 == 1)
 118 			if not is_template:
 119 				for uname, (thisuser, remove_action) in subscribeto.iteritems():
 120 					# Do not modify the subscribtion of this usser.
 121 					if remove_action == True:
 122 						continue
 123 					# Do _not_ subscribe these users while preview the page
 124 					if not thisuser.isSubscribedTo ([page_name]):
 125 						if not is_preview:
 126 							# Support for MoinMoin 1.3
 127 							if 'subscribePage' in dir (thisuser):
 128 								subscribe_status = thisuser.subscribePage (page_name)
 129 								if subscribe_status:
 130 									thisuser.save()
 131 							# Support for MoinMoin 1.5
 132 							elif 'subscribe' in dir (thisuser):
 133 								subscribe_status = thisuser.subscribe (page_name)
 134 							else:
 135 								outofdateversion = 1
 136 								break
 137 						if not subscribe_status:
 138 							faileduname.append (uname)
 139 						else:
 140 							addeduname.append (uname)
 141 					else:
 142 						subscribeduname.append (uname)
 143 			else:
 144 				result += p_notice + _(u'This template will not be subscribed!') + u' ' + _(u'Follwing user(s)/group(s) are remembered to be subscribed later with a regular page: %s') % u', '.join (subscribeto.keys()) + u'</p>'
 145 			if addeduname:
 146 				result += p_info + _(u'Following new user(s) will be subscribed to %s before saving: %s') % (page_name, u', '.join (addeduname)) + u'</p>'
 147 			if subscribeduname:
 148 				result += p_info + _(u'Following user(s) are already subsribed to %s: %s') % (page_name, u', '.join (subscribeduname)) + u'</p>'
 149 			if faileduname:
 150 				result += p_warning + _(u'This user(s) failed to subscribe to: %s') % u', '.join (faileduname) + u'</p>'
 151 
 152 	if outofdateversion:
 153 		result += p_warning + _(u'Sorry, out-of-date version of SubscribeTo marcro installed! Functionality disabled until an update occurs.') + u'</p>'
 154 
 155 	if is_preview:
 156 		return result
 157 	return u''

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2011-07-19 13:51:21, 6.2 KB) [[attachment:SubscribeTo-moin19.py]]
  • [get | view] (2006-04-02 19:32:50, 6.1 KB) [[attachment:SubscribeTo.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.