data/plugin/script/account/create_many.py
author Thomas Waldmann <tw AT waldmann-edv DOT de>
Mon, 25 Mar 2013 23:40:03 +0100
changeset 629 23bf696cf294
permissions -rw-r--r--
add a sendmail action, which can be use to submit POST data by E-Mail
     1 # -*- coding: iso-8859-1 -*-
     2 """
     3 MoinMoin - create a user account
     4 
     5 @copyright: 2006 MoinMoin:ThomasWaldmann,
     6             2011 MoinMoin:ReimarBauer
     7 @license: GNU GPL, see COPYING for details.
     8 """
     9 import sys
    10 try:
    11     from pwgen import pwgen
    12 except ImportError:
    13     print '!!!'
    14     print 'You need to "pip install pwgen"'
    15     print '(http://pypi.python.org/pypi/pwgen)'
    16     print '!!!'
    17     sys.exit()
    18 from MoinMoin.script import MoinScript
    19 from MoinMoin import user
    20 
    21 class PluginScript(MoinScript):
    22     """\
    23 Purpose:
    24 ========
    25 This tool allows you to create many accounts via a command line interface.
    26 
    27 Detailed Instructions:
    28 ======================
    29 General syntax: moin [options] account create [create-options]
    30 
    31 [options] usually should be:
    32     --config-dir=/path/to/my/cfg/ --wiki-url=http://wiki.example.org/
    33 
    34 [create-options] see below:
    35     1. Verify that you have specified the right options.
    36        This script does no verification of email addresses or the like.
    37 
    38     2. To create users you need an input file: with lines of Wiki UserName, EMAIL
    39        moin ... account create --filename input_file.csv
    40 """
    41 
    42     def __init__(self, argv, def_values):
    43         MoinScript.__init__(self, argv, def_values)
    44         self.parser.add_option(
    45             "--filename", metavar="filename", dest="filename",
    46             help="comma separated file of WikiUsername, email."
    47         )
    48 
    49     def mainloop(self):
    50         # we don't expect non-option arguments
    51         if len(self.args) != 0:
    52             self.parser.error("incorrect number of arguments")
    53 
    54         flags_given = self.options.filename
    55         if not flags_given:
    56             self.parser.print_help()
    57             sys.exit(1)
    58 
    59         self.init_request()
    60         request = self.request
    61 
    62         text = file(self.options.filename, 'r').read()
    63         lines = text.split('\n')
    64         for line in lines:
    65             try:
    66                 uname, email = line.split(',')
    67                 uname = uname.strip()
    68                 email = email.strip()
    69                 if '@' in email:
    70                     password = pwgen(10, no_symbols=True)
    71                     if user.User(request, name=uname).exists():
    72                         print 'This username "%s" exists already!' % uname
    73                         return
    74                     # Email should be unique - see also MoinMoin.action.newaccount
    75                     if email and request.cfg.user_email_unique:
    76                         if user.get_by_email_address(request, email):
    77                             print 'This emailaddress "%s" belongs to someone else!' % email
    78                             return
    79                     u = user.User(request, None, uname, password=password)
    80                     u.email = email
    81                     u.aliasname = ''
    82                     print " %-20s %-25s %-35s" % (u.id, u.name, u.email), u.save()
    83                     print "- created."
    84 
    85             except ValueError:
    86                 print line
    87