You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
905 B

  1. from api.models import Role
  2. from django.core.management.base import BaseCommand
  3. from django.db import DatabaseError
  4. from django.conf import settings
  5. class Command(BaseCommand):
  6. help = 'Non-interactively create default roles'
  7. def handle(self, *args, **options):
  8. try:
  9. role_names = [settings.ROLE_PROJECT_ADMIN, settings.ROLE_ANNOTATOR, settings.ROLE_ANNOTATION_APPROVER]
  10. except KeyError as key_error:
  11. self.stderr.write(self.style.ERROR(f'Missing Key: "{key_error}"'))
  12. for role_name in role_names:
  13. role = Role()
  14. role.name = role_name
  15. try:
  16. role.save()
  17. except DatabaseError as db_error:
  18. self.stderr.write(self.style.ERROR(f'Datbase Error: "{db_error}"'))
  19. else:
  20. self.stdout.write(self.style.SUCCESS(f'Role created successfully "{role_name}"'))