def handle_add(self, *args, **options): if "username" not in options: raise CommandError("Username is not set") if "email" not in options: raise CommandError("Email is not set") if "pwgen" in options: passwd = "".join( random.choice(self.pwset) for _ in range(self.PWLEN)) else: passwd = None if not passwd: raise CommandError("Password is not set") permissions = set() if not options.get("template"): raise CommandError("template permission not set") for t in options["template"]: if t not in self.TEMPLATES: raise CommandError("Invalid template '%s'" % t) permissions.update(self.TEMPLATES[t]) if not permissions: raise CommandError("No permissions set") # Create user u = User(username=options["username"], email=options["email"], is_active=True) u.set_password(passwd) u.save() for p in permissions: try: perm = Permission.objects.get(name=p) except Permission.DoesNotExist: perm = Permission(name=p) perm.save() perm.users.add(u) print(passwd)
def save(self, *args, **kwargs): super().save(*args, **kwargs) # Create permission if required if self.permission_name: try: Permission.objects.get(name=self.effective_permission_name) except Permission.DoesNotExist: Permission(name=self.effective_permission_name).save()