diff --git a/.gitignore b/.gitignore index 653a3e8d010b65e50ee277da604b2fc202cd9fce..a2274b00d145b376cee7323396849040f0118584 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ SCIPOST_JOURNALS UPLOADS docs/_build +local_files \ No newline at end of file diff --git a/README.md b/README.md index 01c74263c9bc673d4ed41e217723b65e10bf25b1..4622ae3aecb21af5fb32d90aedb91d2961860898 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: @@ -67,6 +67,12 @@ To make sure everything is setup and configured well, run: (scipostenv) $ ./manage.py check ``` +### Create and run migrations +Now that everything is setup, we can setup the datastructures. +```shell +(scipostenv) $ ./manage.py migrate +``` + ### Create a superuser In order to use the admin site, you'll need a superuser. ```shell @@ -74,16 +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 - -```shell -(scipostenv) $ ./manage.py add_groups_and_permissions -``` +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. -### Create and run migrations -Now that everything is setup, we can setup the datastructures. ```shell -(scipostenv) $ ./manage.py migrate +(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 51d2f874401a483a89db1de75e2da83aab9b1d60..44eb26ce7a7d0a0d3ca1fea08d9ef0cc36380077 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