Attachment 'homepage.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3 MoinMoin - creates user homepage for existing accounts
   4 
   5 @copyright: 2009 MoinMoin:ReimarBauer
   6 @license: GNU GPL, see COPYING for details.
   7 """
   8 
   9 from MoinMoin import user, wikidicts
  10 from MoinMoin.Page import Page
  11 from MoinMoin.PageEditor import PageEditor
  12 from MoinMoin.script import MoinScript
  13 from MoinMoin.mail.sendmail import encodeSpamSafeEmail
  14 
  15 
  16 class PluginScript(MoinScript):
  17     """\
  18 Purpose:
  19 ========
  20 This tool allows you to create user homepages via a command line interface.
  21 For a wiki using hierarchical acls enabled it can become difficult to create
  22 for a user his homepage because if there isn't default write right given.
  23 It creates all user homepages if you don't give restrictions.
  24 
  25 Detailed Instructions:
  26 ======================
  27 General syntax: moin [options] account homepage [create-options]
  28 
  29 [options] usually should be:
  30     --config-dir=/path/to/my/cfg/ --wiki-url=wiki.example.org/
  31 
  32 1. Required is one of the options --name, --group or --all-users.
  33 The --name argument could be added to the above examples to create the homepage
  34 only for this name or the --group argument to create only homepages for members
  35 of the given group. Or with --all-users you create homepages for ALL users.
  36 2. Optionally, the --user argument could be added to the above examples,
  37 causing the script to respect ACLs. The homepage creation is then done by this user name.
  38 3. Optionally, the --template_page argument could be added to the above examples,
  39 to give a template for the page creation. With e.g. #acl @ME@:read,write,delete,revert Default
  40 on the page you can define acl rights for the user.
  41 @EMAIL@ becomes expanded to the users obfuscated mail address.
  42 """
  43 
  44     def __init__(self, argv, def_values):
  45         MoinScript.__init__(self, argv, def_values)
  46 
  47         self.parser.add_option(
  48             "-u", "--user", dest="homepage_creater",
  49             help="User as whom the homepage creation operation will be performed as."
  50             )
  51 
  52         self.parser.add_option(
  53             "-t", "--template_page", dest="template_page",
  54             help="The template page which should be used for the homepage creation"
  55             )
  56 
  57         self.parser.add_option(
  58             "-n", "--name", dest="user_homepage",
  59             help="The name of the user the homepage should be created for."
  60             )
  61 
  62         self.parser.add_option(
  63             "-g", "--group", dest="name_of_group_page",
  64             help="The name of the group page to select users for creating their homepages."
  65             )
  66 
  67         self.parser.add_option(
  68             "-a", "--all-users", dest="all_users",
  69             help="The name of the group page to select users for creating their homepages."
  70             )
  71 
  72     def create_homepage(self, homepage_default_text, account):
  73         homepage_text = homepage_default_text % {
  74                                                  "username": account.name,
  75                                                  "obfuscated_mail": encodeSpamSafeEmail(account.email)
  76                                                  }
  77         self.write_homepage(account, homepage_text)
  78 
  79     def write_homepage(self, account, homepage_text):
  80         # writes the homepage
  81         if  account.exists() and not account.disabled and not Page(self.request, account.name).exists():
  82             userhomepage = PageEditor(self.request, account.name)
  83             try:
  84                 userhomepage.saveText(homepage_text, 0)
  85                 print "INFO homepage for %s created." % account.name
  86             except userhomepage.Unchanged:
  87                 print "You did not change the page content, not saved!"
  88             except userhomepage.NoAdmin:
  89                 print "You don't have enough rights to create the %s page" % account.name
  90         else:
  91             print "INFO homepage for %s already exists or account is disabled or user does not exist." % account.name
  92 
  93     def mainloop(self):
  94         # we don't expect non-option arguments
  95         self.init_request()
  96         request = self.request
  97         # Checks for a template page and sets homepage_default_text
  98         if self.options.template_page and Page(self.request, self.options.template_page).exists():
  99             homepage_default_text = Page(self.request, self.options.template_page).get_raw_body()
 100             # replace is needed because substitution is done for request.user
 101             # see option --user
 102             homepage_default_text = homepage_default_text.replace('@ME@', "%(username)s")
 103             homepage_default_text = homepage_default_text.replace('@EMAIL@', "<<MailTo(%(obfuscated_mail)s)>>")
 104         else:
 105             homepage_default_text = '''#acl %(username)s:read,write,delete,revert Default
 106 
 107 == %(username)s ==
 108 
 109 Email: <<MailTo(%(obfuscated_mail)s)>>
 110 ## You can even more obfuscate your email address by adding more uppercase letters followed by a leading and trailing blank.
 111 
 112 ----
 113 CategoryHomepage
 114 '''
 115         # Check for user
 116         if self.options.homepage_creater:
 117             uid = user.getUserId(request, self.options.homepage_creater)
 118             request.user = user.User(request, uid)
 119         # Check for Group definition
 120         members = []
 121         if self.options.user_homepage:
 122             members = [self.options.user_homepage, ]
 123         elif self.options.name_of_group_page:
 124             user_group = wikidicts.Group(request, self.options.name_of_group_page)
 125             members = user_group.members()
 126         elif self.options.all_users:
 127             uids = user.getUserList(request)
 128             members = [user.User(request, uid).name for uid in uids]
 129 
 130         if not members:
 131             print "See additional options because no user was found or look for a typo."
 132             return
 133 
 134         # loop though members for creating homepages
 135         for name in members:
 136             uid = user.getUserId(request, name)
 137             account = user.User(request, uid)
 138             self.create_homepage(homepage_default_text, account)

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] (2009-03-16 21:05:54, 5.8 KB) [[attachment:homepage.py]]
 All files | Selected Files: delete move to page copy to page

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