view data/plugin/script/account/create_many.py @ 653:127da830be6c

updated copyright
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Tue, 19 Apr 2016 16:29:54 +0200
parents ca006bf7dae8
children
line wrap: on
line source
# -*- coding: iso-8859-1 -*-
"""
MoinMoin - create a user account

@copyright: 2006 MoinMoin:ThomasWaldmann,
            2011-2016 MoinMoin:ReimarBauer
@license: GNU GPL, see COPYING for details.
"""
import sys
try:
    from pwgen import pwgen
except ImportError:
    print '!!!'
    print 'You need to "pip install pwgen"'
    print '(http://pypi.python.org/pypi/pwgen)'
    print '!!!'
    sys.exit()
from MoinMoin.script import MoinScript
from MoinMoin import user

class PluginScript(MoinScript):
    """\
Purpose:
========
This tool allows you to create many accounts via a command line interface.

Detailed Instructions:
======================
General syntax: moin [options] account create [create-options]

[options] usually should be:
    --config-dir=/path/to/my/cfg/ --wiki-url=http://wiki.example.org/

[create-options] see below:
    1. Verify that you have specified the right options.
       This script does no verification of email addresses or the like.

    2. To create users you need an input file: with lines of Wiki UserName, EMAIL
       moin ... account create --filename input_file.csv
"""

    def __init__(self, argv, def_values):
        MoinScript.__init__(self, argv, def_values)
        self.parser.add_option(
            "--filename", metavar="filename", dest="filename",
            help="comma separated file of WikiUsername, email."
        )

    def mainloop(self):
        # we don't expect non-option arguments
        if len(self.args) != 0:
            self.parser.error("incorrect number of arguments")

        flags_given = self.options.filename
        if not flags_given:
            self.parser.print_help()
            sys.exit(1)

        self.init_request()
        request = self.request

        text = file(self.options.filename, 'r').read()
        lines = text.split('\n')
        for line in lines:
            try:
                uname, email = line.split(',')
                uname = uname.strip()
                email = email.strip()
                if '@' in email:
                    password = pwgen(10, no_symbols=True)
                    if user.User(request, name=uname).exists():
                        print 'This username "%s" exists already!' % uname
                        return
                    # Email should be unique - see also MoinMoin.action.newaccount
                    if email and request.cfg.user_email_unique:
                        if user.get_by_email_address(request, email):
                            print 'This emailaddress "%s" belongs to someone else!' % email
                            return
                    u = user.User(request, None, uname, password=password)
                    u.email = email
                    u.aliasname = ''
                    print " %-20s %-25s %-35s" % (u.id, u.name, u.email), u.save()
                    print "- created."

            except ValueError:
                print line