Back to index

moin  1.9.0~rc2
homepage.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003 MoinMoin - creates user homepage for existing accounts
00004 
00005 @copyright: 2009 MoinMoin:ReimarBauer
00006 @license: GNU GPL, see COPYING for details.
00007 """
00008 
00009 from MoinMoin import user
00010 from MoinMoin.Page import Page
00011 from MoinMoin.PageEditor import PageEditor
00012 from MoinMoin.script import MoinScript
00013 from MoinMoin.mail.sendmail import encodeSpamSafeEmail
00014 
00015 
00016 class PluginScript(MoinScript):
00017     """\
00018 Purpose:
00019 ========
00020 This tool allows you to create user homepages via a command line interface.
00021 
00022 Detailed Instructions:
00023 ======================
00024 General syntax: moin [options] account homepage [homepage-options]
00025 
00026 [options] usually should be:
00027     --config-dir=/path/to/my/cfg/ --wiki-url=wiki.example.org/
00028 
00029 1. Required is one of the options --name, --group or --all-users.
00030 To create the homepage of one user use the --name argument. For adding homepages to a group of users
00031 give the --group page argument. Or with --all-users you create homepages for ALL users.
00032 2. To respect ACLs  give the --user argument.
00033 3. Optionally you may want to use a template page by the --template_page argument.
00034 With e.g. #acl @ME@:read,write,delete,revert Default on the template page you can define
00035 acl rights for the user. @EMAIL@ becomes expanded to the users obfuscated mail address.
00036 """
00037 
00038     def __init__(self, argv, def_values):
00039         MoinScript.__init__(self, argv, def_values)
00040 
00041         self.parser.add_option(
00042             "-u", "--user", dest="homepage_creator",
00043             help="User as whom the homepage creation operation will be performed as."
00044             )
00045 
00046         self.parser.add_option(
00047             "-t", "--template_page", dest="template_page",
00048             help="The template page which should be used for the homepage creation"
00049             )
00050 
00051         self.parser.add_option(
00052             "-n", "--name", dest="user_homepage",
00053             help="The name of the user the homepage should be created for."
00054             )
00055 
00056         self.parser.add_option(
00057             "-g", "--group", dest="name_of_group_page",
00058             help="The name of the group page to select users for creating their homepages."
00059             )
00060 
00061         self.parser.add_option(
00062             "-a", "--all-users", dest="all_users", action="store_true",
00063             help="The name of the group page to select users for creating their homepages."
00064             )
00065 
00066     def write_homepage(self, account, homepage_text):
00067         # writes the homepage
00068         if account.exists() and not account.disabled and not Page(self.request, account.name).exists():
00069             userhomepage = PageEditor(self.request, account.name)
00070             try:
00071                 userhomepage.saveText(homepage_text, 0)
00072                 print "INFO homepage for %s created." % account.name
00073             except userhomepage.Unchanged:
00074                 print "You did not change the page content, not saved!"
00075             except userhomepage.NoAdmin:
00076                 print "You don't have enough rights to create the %s page" % account.name
00077         else:
00078             print "INFO homepage for %s already exists or account is disabled or user does not exist." % account.name
00079 
00080     def mainloop(self):
00081         # we don't expect non-option arguments
00082         self.init_request()
00083         request = self.request
00084         # Checks for a template page and sets homepage_default_text
00085         if self.options.template_page and Page(self.request, self.options.template_page).exists():
00086             homepage_default_text = Page(self.request, self.options.template_page).get_raw_body()
00087             # replace is needed because substitution is done for request.user
00088             # see option --user
00089             homepage_default_text = homepage_default_text.replace('@ME@', "%(username)s")
00090             homepage_default_text = homepage_default_text.replace('@EMAIL@', "<<MailTo(%(obfuscated_mail)s)>>")
00091         else:
00092             homepage_default_text = '''#acl %(username)s:read,write,delete,revert Default
00093 
00094 == %(username)s ==
00095 
00096 Email: <<MailTo(%(obfuscated_mail)s)>>
00097 ## You can even more obfuscate your email address by adding more uppercase letters followed by a leading and trailing blank.
00098 
00099 ----
00100 CategoryHomepage
00101 '''
00102         # Check for user
00103         if self.options.homepage_creator:
00104             uid = user.getUserId(request, self.options.homepage_creator)
00105             request.user = user.User(request, uid)
00106         # Check for Group definition
00107         members = []
00108         if self.options.user_homepage:
00109             members = [self.options.user_homepage, ]
00110         elif self.options.name_of_group_page:
00111             members = request.groups.get(self.options.name_of_group_page, [])
00112         elif self.options.all_users:
00113             uids = user.getUserList(request)
00114             members = [user.User(request, uid).name for uid in uids]
00115 
00116         if not members:
00117             print "No user selected!"
00118             return
00119 
00120         # loop through members for creating homepages
00121         for name in members:
00122             uid = user.getUserId(request, name)
00123             account = user.User(request, uid)
00124             homepage_text = homepage_default_text % {
00125                                                  "username": account.name,
00126                                                  "obfuscated_mail": encodeSpamSafeEmail(account.email)
00127                                                  }
00128             self.write_homepage(account, homepage_text)