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.

34 lines
1.5 KiB

  1. from api.models import Project, Role, RoleMapping, User
  2. from django.core.management.base import BaseCommand, CommandError
  3. class Command(BaseCommand):
  4. help = 'Non-interactively create a rolemapping'
  5. @classmethod
  6. def add_arguments(self, parser):
  7. parser.add_argument('--rolename', default=None,
  8. help='The name of the role.')
  9. parser.add_argument('--username', default=None,
  10. help='The name of the user.')
  11. parser.add_argument('--projectname', default=None,
  12. help='The name of the project.')
  13. def handle(self, *args, **options):
  14. rolename = options.get('rolename')
  15. username = options.get('username')
  16. projectname = options.get('projectname')
  17. if not rolename or not username or not projectname:
  18. raise CommandError('--rolename --projectname --username are required for the rolemapping')
  19. if rolename and projectname and username:
  20. try:
  21. role = Role.objects.get(name=rolename)
  22. user = User.objects.get(username=username)
  23. project = Project.objects.get(name=projectname)
  24. rolemapping = RoleMapping.objects.create(role_id=role.id, user_id=user.id, project_id=project.id)
  25. except Exception as ex:
  26. self.stderr.write(self.style.ERROR('Error occurred while creating rolemapping "%s"' % ex))
  27. else:
  28. self.stdout.write(self.style.SUCCESS('Rolemapping created successfully "%s"' % rolemapping.id))