Page MenuHomeSoftware Heritage

No OneTemporary

diff --git a/swh/deposit/create_user.py b/swh/deposit/cli.py
old mode 100755
new mode 100644
similarity index 57%
rename from swh/deposit/create_user.py
rename to swh/deposit/cli.py
index 371668d2..c0f83a89
--- a/swh/deposit/create_user.py
+++ b/swh/deposit/cli.py
@@ -1,56 +1,79 @@
-#!/usr/bin/env python3
-
-# Copyright (C) 2017 The Software Heritage developers
+# Copyright (C) 2017-2019 The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information
import click
from swh.deposit.config import setup_django_for
+from swh.deposit.models import DepositClient, DepositCollection
+
+CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
-@click.command(
- help='Create a user with some needed information (password, collection)')
+
+@click.group(context_settings=CONTEXT_SETTINGS)
@click.option('--platform', default='development',
help='development or production platform')
+@click.pass_context
+def cli(ctx, platform):
+ setup_django_for(platform)
+
+
+@cli.group('user')
+@click.pass_context
+def user(ctx):
+ """Manipulate user."""
+ pass
+
+
+@user.command('create')
@click.option('--username', required=True, help="User's name")
-@click.option('--password', required=True, help="Desired user's password.")
+@click.option('--password', required=True,
+ help="Desired user's password (plain).")
@click.option('--firstname', default='', help="User's first name")
@click.option('--lastname', default='', help="User's last name")
@click.option('--email', default='', help="User's email")
@click.option('--collection', help="User's collection")
-def main(platform, username, password, firstname, lastname, email, collection):
- setup_django_for(platform)
+def create_user(username, password, firstname, lastname, email, collection):
+ """Create a user with some needed information (password, collection)
+
+ If the collection does not exist, the creation process is stopped.
- from swh.deposit.models import DepositClient, DepositCollection
+ The password is stored encrypted using django's utilies.
+
+ """
try:
collection = DepositCollection.objects.get(name=collection)
except DepositCollection.DoesNotExist:
raise ValueError(
'Collection %s does not exist, skipping' % collection)
# user create/update
try:
user = DepositClient.objects.get(username=username)
- print('User %s exists, updating information.' % user)
+ click.echo_via_pager('User %s exists, updating information.' % user)
user.set_password(password)
except DepositClient.DoesNotExist:
- print('Create new user %s' % username)
+ click.echo_via_pager('Create new user %s' % username)
user = DepositClient.objects.create_user(
username=username,
password=password)
user.collections = [collection.id]
user.first_name = firstname
user.last_name = lastname
user.email = email
user.is_active = True
user.save()
- print('Information registered for user %s' % user)
+ click.echo_via_pager('Information registered for user %s' % user)
+
+
+def main():
+ return cli(auto_envvar_prefix='SWH_DEPOSIT')
if __name__ == '__main__':
main()

File Metadata

Mime Type
text/x-diff
Expires
Fri, Jul 4, 12:39 PM (2 w, 2 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3355669

Event Timeline