def create_user(username, password, is_superuser=True): if ENABLE_ORGANIZATIONS.get(): organization = get_organization(email=username) lookup = {'email': username, 'organization': organization} else: lookup = {'username': username} LOG.info("Materializing user %s in the database" % lookup) user = User(**lookup) if password is None: user.set_unusable_password() else: user.set_password(password) if ENABLE_ORGANIZATIONS.get(): user.is_admin = is_superuser or not organization.organizationuser_set.exists( ) or not organization.is_multi_user user.is_superuser = not User.objects.exists() else: user.is_superuser = is_superuser user.save() return user
def create_user(username, password, is_superuser=True): if ENABLE_ORGANIZATIONS.get(): organization = get_organization(email=username) attrs = {'email': username, 'organization': organization} else: attrs = {'username': username} user = User(**attrs) if password is None: user.set_unusable_password() else: user.set_password(password) user.is_superuser = is_superuser if ENABLE_ORGANIZATIONS.get(): user.is_admin = is_superuser or not organization.organizationuser_set.exists() or not organization.is_multi_user user.save() ensure_has_a_group(user) user.save() LOG.info("User %s was created." % username) return user
def find_or_create_user(username, password=None): try: user = User.objects.get(username=username) LOG.debug("Found user %s in the db" % username) except User.DoesNotExist: LOG.info("Materializing user %s in the database" % username) user = User(username=username) if password is None: user.set_unusable_password() else: user.set_password(password) user.is_superuser = True user.save() return user