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.

26 lines
992 B

2 years ago
  1. from django.conf import settings
  2. from django.core.management.base import BaseCommand
  3. from django.db import DatabaseError
  4. from ...models import Role
  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. if Role.objects.filter(name=role_name).exists():
  14. continue
  15. role = Role()
  16. role.name = role_name
  17. try:
  18. role.save()
  19. except DatabaseError as db_error:
  20. self.stderr.write(self.style.ERROR(f'Database Error: "{db_error}"'))
  21. else:
  22. self.stdout.write(self.style.SUCCESS(f'Role created successfully "{role_name}"'))