From 8719d4d7ac0c98f75df6b8f027646ec193542e2b Mon Sep 17 00:00:00 2001 From: Jorran Wit <jorrandewit@outlook.com> Date: Sat, 10 Dec 2016 06:49:49 +0100 Subject: [PATCH] Added arguments to management command to ease initial install. --- README.md | 12 +++---- .../commands/add_groups_and_permissions.py | 35 +++++++++++++++++-- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 683938382..4622ae3ae 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,10 @@ higher. Python dependencies are listed in `requirements.txt`. ## Getting started +### Database +Make sure that Postgres is installed and running, and that a database and user are set up for it. A +good guide how to do this can be found [here](https://djangogirls.gitbooks.io/django-girls-tutorial-extensions/content/optional_postgresql_installation/) (NOTE: stop before the 'Update settings' part). + ### Python version Make sure you're using Python 3.5. If you need to use multiple versions of Python, use [pyenv](https://github.com/yyuu/pyenv). @@ -24,10 +28,6 @@ Now install dependencies: (scipostenv) $ pip install -r requirements.txt ``` -### Database -Make sure that Postgres is installed and running, and that a database and user are set up for it. A -good guide how to do this can be found [here](https://djangogirls.gitbooks.io/django-girls-tutorial-extensions/content/optional_postgresql_installation/) (NOTE: stop before the 'Update settings' part). - ### Host-specific settings In this project, host-specific settings are defined in the `scipost-host-settings.json` file in the directory *above* the project root. The structure is as follows: @@ -80,10 +80,10 @@ In order to use the admin site, you'll need a superuser. ``` ### Create groups and permissions -Groups and their respective permissions are created using the management command +Groups and their respective permissions are created using the management command. Since users depend on the *Contributor* object to work properly, setup the first (admin) user using the `-u` and `-a` arguments. ```shell -(scipostenv) $ ./manage.py add_groups_and_permissions +(scipostenv) $ ./manage.py add_groups_and_permissions -u=<username> -a ``` ### Run development server diff --git a/scipost/management/commands/add_groups_and_permissions.py b/scipost/management/commands/add_groups_and_permissions.py index 51d2f8744..44eb26ce7 100644 --- a/scipost/management/commands/add_groups_and_permissions.py +++ b/scipost/management/commands/add_groups_and_permissions.py @@ -1,14 +1,22 @@ from django.core.management.base import BaseCommand, CommandError -from django.contrib.auth.models import Group, Permission +from django.contrib.auth.models import Group, Permission, User from django.contrib.contenttypes.models import ContentType from scipost.models import Contributor class Command(BaseCommand): help = 'Defines groups and permissions' + + def add_arguments(self, parser): + """ Append arguments optionally for setup of Contributor roles """ + parser.add_argument('-u', '--setup-user', metavar='<username>', type=str, required=False, help='Username to make registered contributor') + parser.add_argument('-a', '--make-admin', required=False, action='store_true', help='Grant admin permissions to user (superuser only)') + parser.add_argument('-t', '--make-tester', required=False, action='store_true', help='Grant test permissions to user') def handle(self, *args, **options): + """ Append all user Groups and setup a Contributor roles to user """ + # Create Groups SciPostAdmin, created = Group.objects.get_or_create(name='SciPost Administrators') AdvisoryBoard, created = Group.objects.get_or_create(name='Advisory Board') @@ -196,4 +204,27 @@ class Command(BaseCommand): can_draft_registration_invitations, ) - self.stdout.write(self.style.SUCCESS('Successfully created groups and permissions')) + self.stdout.write(self.style.SUCCESS('Successfully created groups and permissions.')) + + if options['setup_user']: + # Username is given, check options + try: + user = User.objects.get(username=str(options['setup_user'])) + user.groups.add(RegisteredContributors) + self.stdout.write(self.style.SUCCESS('Successfully setup %s as contributor.' % user)) + except User.DoesNotExist: + self.stdout.write(self.style.WARNING('User <%s> not found.' % options['update_user'])) + return + + if user.is_superuser and options['make_admin']: + # Setup admin contributor + user.groups.add(SciPostAdmin) + self.stdout.write(self.style.SUCCESS('Successfully made %s admin.' % user)) + elif options['make_admin']: + # Make admin failed, user not a superuser + self.stdout.write(self.style.WARNING('User %s is not a superuser.' % user)) + + if options['make_tester']: + # Setup test contributor + user.groups.add(Testers) + self.stdout.write(self.style.SUCCESS('Successfully made %s tester.' % user)) \ No newline at end of file -- GitLab