From 2f06faee7523df0f17ccfc4d3c568295734336c2 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Tue, 2 Feb 2016 04:19:50 +0100 Subject: manage.py: Replace contrib/create_accounts.py Replace the contrib script with a command in the manage.py. --- contrib/create_account.py | 81 ----------------------------------------------- manage.py | 44 +++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 83 deletions(-) delete mode 100755 contrib/create_account.py diff --git a/contrib/create_account.py b/contrib/create_account.py deleted file mode 100755 index 17e4646..0000000 --- a/contrib/create_account.py +++ /dev/null @@ -1,81 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -import sys -from os.path import dirname, abspath -sys.path.append(dirname(dirname(abspath(__file__)))) - -from account import AccountService, NoSuchUserError -from app import app -from flask import g, render_template, url_for -from utils import make_confirmation, send_mail - -""" -Create an account. - -The default operation is to send an activation mail to the given address. So -the only difference to the register form on the website is that the username -blacklist is not checked. -The user can click the link and enter a password to finish account creation. - -Usage: - $0 username mail Send an activation mail. - $0 -p username mail Only print the link. -""" - -def main(username, mail, print_only=False): - service = AccountService(app.config['LDAP_HOST'], app.config['LDAP_BASE_DN'], - app.config['LDAP_ADMIN_USER'], app.config['LDAP_ADMIN_PASS'], - app.all_services) - - try: - service.get_by_uid(username) - except NoSuchUserError: - pass - else: - raise CreationError(u'There is already a user named %s' % username) - - try: - u = service.get_by_mail(mail) - except NoSuchUserError: - pass - else: - raise CreationError(u'There is already a user with email %s (uid: %s)' % (mail, u.uid)) - - confirm_token = make_confirmation('register', (username, mail)) - confirm_link = url_for('register_complete', token=confirm_token, _external=True) - - if print_only: - print confirm_link - return - - body = render_template('mail/register.txt', username=username, - mail=mail, link=confirm_link) - - send_mail(mail, u'E-Mail-Adresse bestätigen', body, - sender=app.config.get('MAIL_CONFIRM_SENDER')) - - print 'Mail versandt.' - - - -class CreationError(ValueError): - pass - - -if __name__ == '__main__': - try: - sys.argv.remove('-p') - except ValueError: - print_only = False - else: - print_only = True - - if len(sys.argv) == 3: - #XXX: I have the strong feeling that could be done better - try: - with app.test_request_context(base_url='http://%s/' % (app.config['SERVER_NAME'] or 'localhost')): - main(sys.argv[1], sys.argv[2], print_only=print_only) - except CreationError, e: - print 'Error:', e - else: - print "Usage: %s username email" % sys.argv[0] diff --git a/manage.py b/manage.py index bbe654f..d7c7e54 100755 --- a/manage.py +++ b/manage.py @@ -1,9 +1,11 @@ #!/usr/bin/env python from flask import current_app -from flask.ext.script import Manager, Server, Shell, Option +from flask.ext.script import Manager, Server, Shell, Option, prompt_bool from accounts import create_app -from accounts.utils.console import Command, TablePrinter +from accounts.forms import RegisterForm +from accounts.utils.console import Command, ConsoleForm, TablePrinter +from accounts.backend.mail.dummy import DummyBackend as DummyMailBackend class ListServices(Command): @@ -43,6 +45,43 @@ class ListUsers(Command): for user in self._get_users(locked, only_locked)]) +class CreateUser(Command): + """Register a user.""" + name = 'create-user' + + option_list = ( + Option('--ignore-blacklist', action='store_true', + dest='ignore_blacklist', help='Ignore blackisted user names.'), + Option('--print', '-p', action='store_true', dest='print_only', + help='Do not send the activation link via mail, only print it.'), + Option(dest='username', metavar='USERNAME', + help='Username that should be registered.'), + Option(dest='mail', metavar='MAIL', + help='Mail address of the new user.'), + ) + + def run(self, username, mail, ignore_blacklist=False, print_only=False): + form = ConsoleForm(RegisterForm, username=username, mail=mail) + form.csrf_enabled = False + del form.question + + if ignore_blacklist and prompt_bool('Blacklist wirklich ignorieren?'): + current_app.username_blacklist = [] + + if not form.validate(): + form.print_errors() + return + + if print_only: + current_app.mail_backend = DummyMailBackend(current_app) + + current_app.mail_backend.send(mail, 'mail/register.txt', + username=username) + + if not print_only: + print('Mail versandt.') + + def main(): manager = Manager(create_app) manager.add_option('-c', '--config', dest='config', required=False) @@ -55,6 +94,7 @@ def main(): 'shell', Shell()) manager.add_command(ListServices) manager.add_command(ListUsers) + manager.add_command(CreateUser) manager.run() -- cgit v1.2.3-1-g7c22