Attachment 'SubscribeTo.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 import re
  27 
  28 def execute (macro, args):
  29 	_ = macro.request.getText
  30 	unknownuname = {}
  31 	missingemail = {}
  32 	outofdateversion = 0
  33 	result = u''
  34 	allusers = re.split (r'[,;\s]+', args)
  35 	subscribeto = {}
  36 	page_name = macro.request.page.page_name
  37 	is_preview = macro.form.has_key ('button_preview')
  38 	p_warning = u'<p class="warning" style="margin: 0; padding: 0.4em; background: #FFFF80">' + macro.formatter.icon ("info") + u' '
  39 	p_notice = u'<p class="notice" style="margin: 0; padding: 0.4em; background: #81BBF2">' + macro.formatter.icon ("info") + u' '
  40 	p_info = u'<p class="info" style="margin: 0; padding: 0.4em; background: #E6EAF0">' + macro.formatter.icon ("info") + u' '
  41 	# TODO: Should be compiled only once, and cached in cfg
  42 	group_re = re.compile (macro.cfg.page_group_regex, re.UNICODE)
  43 	# By default templates are not subsribed. This can be changed by
  44 	# specifing the FQTN as parameter.
  45 	is_template = re.search (macro.cfg.page_template_regex, page_name, re.UNICODE)
  46 	# Proceed the list in the same order as specified by the user.
  47 	# This allow us to work with groups and users in the same way
  48 	# as specifing all members of a group.
  49 	for uname in allusers:
  50 		# Skip empty names
  51 		if not uname:
  52 			continue
  53 		# Determine what to do with the user/group members.
  54 		if uname[:1] == '-':
  55 			uname = uname[1:]
  56 			remove_action = True
  57 		else:
  58 			if uname[:1] == '+':
  59 				uname = uname[1:]
  60 			remove_action = False
  61 
  62 		# It's a special feature. If the FQTN (full-qualified template name)
  63 		# is one of the parameters we allow to subscribe to this template too.
  64 		if remove_action == False and uname == page_name:
  65 			is_template = False
  66 			continue
  67 
  68 		members = []
  69 		# Distinguish between user and group. Create a list of
  70 		# users which has to be subscribed to.
  71 		if group_re.search (uname):
  72 			# Recursively expand groups
  73 			groupdict = macro.request.dicts
  74 			if groupdict.hasgroup (uname):
  75 				members = groupdict.members (uname)
  76 			else:
  77 				unknownuname[uname] = 1
  78 		else:
  79 			members = [uname]
  80 
  81 		# Go through all members and proceed with the same action.
  82 		for uname in members:
  83 			uid = user.getUserId (macro.request, uname)
  84 			if not uid:
  85 				# Only if this user has to be added and does not exists put
  86 				# his name in our unknwonuname list. Otherwise the use does
  87 				# not matter.
  88 				if remove_action == False:
  89 					unknownuname[uname] = 1
  90 				# If this user was added before but we know now that should
  91 				# be skipped we update the unknownuname list afterward.
  92 				else:
  93 					if uname in subscribeto:
  94 						del subscribeto[uname]
  95 					if uname in unknownuname:
  96 						del unknownuname[uname]
  97 			else:
  98 				thisuser = user.User (macro.request, id = uid)
  99 				if not thisuser.email:
 100 					missingemail[uname] = 1
 101 				else:
 102 					subscribeto[uname] = (thisuser, remove_action)
 103 				# MoinMoin compatibility check
 104 				if not 'subscribePage' in dir (thisuser) and not 'subscribe' in dir (thisuser):
 105 					outofdateversion = 1
 106 					break
 107 	if not outofdateversion:
 108 		if unknownuname:
 109 			result += p_warning + _(u'This user(s)/group(s) are unknown by now: %s') % u', '.join (unknownuname.keys()) + u'</p>'
 110 		if missingemail:
 111 			result += p_warning + _(u'Follwing users(s) missing an email address in their profile: %s') % u', '.join (missingemail.keys()) + u'</p>'
 112 		if subscribeto:
 113 			addeduname = []
 114 			faileduname = []
 115 			subscribeduname = []
 116 			subscribe_status = (1 == 1)
 117 			if not is_template:
 118 				for uname, (thisuser, remove_action) in subscribeto.iteritems():
 119 					# Do not modify the subscribtion of this usser.
 120 					if remove_action == True:
 121 						continue
 122 					# Do _not_ subscribe these users while preview the page
 123 					if not thisuser.isSubscribedTo ([page_name]):
 124 						if not is_preview:
 125 							# Support for MoinMoin 1.3
 126 							if 'subscribePage' in dir (thisuser):
 127 								subscribe_status = thisuser.subscribePage (page_name)
 128 								if subscribe_status:
 129 									thisuser.save()
 130 							# Support for MoinMoin 1.5
 131 							elif 'subscribe' in dir (thisuser):
 132 								subscribe_status = thisuser.subscribe (page_name)
 133 							else:
 134 								outofdateversion = 1
 135 								break
 136 						if not subscribe_status:
 137 							faileduname.append (uname)
 138 						else:
 139 							addeduname.append (uname)
 140 					else:
 141 						subscribeduname.append (uname)
 142 			else:
 143 				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>'
 144 			if addeduname:
 145 				result += p_info + _(u'Following new user(s) will be subscribed to %s before saving: %s') % (page_name, u', '.join (addeduname)) + u'</p>'
 146 			if subscribeduname:
 147 				result += p_info + _(u'Following user(s) are already subsribed to %s: %s') % (page_name, u', '.join (subscribeduname)) + u'</p>'
 148 			if faileduname:
 149 				result += p_warning + _(u'This user(s) failed to subscribe to: %s') % u', '.join (faileduname) + u'</p>'
 150 
 151 	if outofdateversion:
 152 		result += p_warning + _(u'Sorry, out-of-date version of SubscribeTo marcro installed! Functionality disabled until an update occurs.') + u'</p>'
 153 
 154 	if is_preview:
 155 		return result
 156 	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.