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.

35 lines
1.5 KiB

2 years ago
2 years ago
2 years ago
2 years ago
  1. from django.core.management.base import BaseCommand, CommandError
  2. from django.contrib.auth.models import User
  3. from models import Project
  4. from roles.models import Role
  5. from ...models import Member
  6. class Command(BaseCommand):
  7. help = "Non-interactively create a member"
  8. @classmethod
  9. def add_arguments(self, parser):
  10. parser.add_argument("--rolename", default=None, help="The name of the role.")
  11. parser.add_argument("--username", default=None, help="The name of the user.")
  12. parser.add_argument("--projectname", default=None, 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 member")
  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. member = Member.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 member "%s"' % ex))
  27. else:
  28. self.stdout.write(self.style.SUCCESS('Member created successfully "%s"' % member.id))