def create_user_profile(realm, email, password, active, bot_type, full_name, short_name, bot_owner, is_mirror_dummy, tos_version, timezone, tutorial_status=UserProfile.TUTORIAL_WAITING, enter_sends=False): # type: (Realm, Text, Optional[Text], bool, Optional[int], Text, Text, Optional[UserProfile], bool, Text, Optional[Text], Optional[Text], bool) -> UserProfile now = timezone_now() email = UserManager.normalize_email(email) user_profile = UserProfile(email=email, is_staff=False, is_active=active, full_name=full_name, short_name=short_name, last_login=now, date_joined=now, realm=realm, pointer=-1, is_bot=bool(bot_type), bot_type=bot_type, bot_owner=bot_owner, is_mirror_dummy=is_mirror_dummy, tos_version=tos_version, timezone=timezone, tutorial_status=tutorial_status, enter_sends=enter_sends, onboarding_steps=ujson.dumps([]), default_language=realm.default_language) if bot_type or not active: password = None user_profile.set_password(password) user_profile.api_key = random_api_key() return user_profile
def create_user(self, username, vk_user_id=None, fb_user_id=None, gender=None, birth_date=None, email=None, password=None, **extra_fields): """ Creates and saves a User with the given username, email and password. """ now = timezone.now() if not username: raise ValueError('The given username must be set') email = UserManager.normalize_email(email) user = self.model(username=username, vk_user_id=vk_user_id, fb_user_id=fb_user_id, gender=gender, birth_date=birth_date, email=email, is_staff=False, is_active=True, is_superuser=False, last_login=now, date_joined=now, **extra_fields) user.set_password(password) user.save(using=self._db) return user
def create_user(self, username="", email=None, password=None, commit=True, **extra_fields): now = timezone.now() if not email: raise ValueError('The given email must be set') email = UserManager.normalize_email(email) username = username if username else email user = self.model(username=username, email=email, is_staff=False, is_active=True, is_superuser=False, last_login=now, **extra_fields) if password: user.set_password(password) else: user.set_unusable_password() if commit: user.save(using=self._db) return user
def create_user_profile(realm: Realm, email: str, password: Optional[str], active: bool, bot_type: Optional[int], full_name: str, short_name: str, bot_owner: Optional[UserProfile], is_mirror_dummy: bool, tos_version: Optional[str], timezone: Optional[str], tutorial_status: str = UserProfile.TUTORIAL_WAITING, enter_sends: bool = False) -> UserProfile: now = timezone_now() email = UserManager.normalize_email(email) user_profile = UserProfile(is_staff=False, is_active=active, full_name=full_name, short_name=short_name, last_login=now, date_joined=now, realm=realm, is_bot=bool(bot_type), bot_type=bot_type, bot_owner=bot_owner, is_mirror_dummy=is_mirror_dummy, tos_version=tos_version, timezone=timezone, tutorial_status=tutorial_status, enter_sends=enter_sends, onboarding_steps=ujson.dumps([]), default_language=realm.default_language, twenty_four_hour_time=realm.default_twenty_four_hour_time, delivery_email=email) if bot_type or not active: password = None if user_profile.email_address_is_realm_public(): # If emails are visible to everyone, we can set this here and save a DB query user_profile.email = get_display_email_address(user_profile, realm) user_profile.set_password(password) user_profile.api_key = generate_api_key() return user_profile
def create_user_profile(realm: Realm, email: str, password: Optional[str], active: bool, bot_type: Optional[int], full_name: str, short_name: str, bot_owner: Optional[UserProfile], is_mirror_dummy: bool, tos_version: Optional[str], timezone: Optional[str], tutorial_status: Optional[str] = UserProfile.TUTORIAL_WAITING, enter_sends: bool = False) -> UserProfile: now = timezone_now() email = UserManager.normalize_email(email) user_profile = UserProfile(is_staff=False, is_active=active, full_name=full_name, short_name=short_name, last_login=now, date_joined=now, realm=realm, pointer=-1, is_bot=bool(bot_type), bot_type=bot_type, bot_owner=bot_owner, is_mirror_dummy=is_mirror_dummy, tos_version=tos_version, timezone=timezone, tutorial_status=tutorial_status, enter_sends=enter_sends, onboarding_steps=ujson.dumps([]), default_language=realm.default_language, twenty_four_hour_time=realm.default_twenty_four_hour_time, delivery_email=email) if bot_type or not active: password = None if realm.email_address_visibility == Realm.EMAIL_ADDRESS_VISIBILITY_EVERYONE: # If emails are visible to everyone, we can set this here and save a DB query user_profile.email = get_display_email_address(user_profile, realm) user_profile.set_password(password) user_profile.api_key = generate_api_key() return user_profile
def create_user_profile(realm, email, password, active, bot_type, full_name, short_name, bot_owner, is_mirror_dummy, tos_version): # type: (Realm, text_type, text_type, bool, Optional[int], text_type, text_type, Optional[UserProfile], bool, Optional[text_type]) -> UserProfile now = timezone.now() email = UserManager.normalize_email(email) enable_stream_desktop_notifications = (realm.domain != 'zulip.com') user_profile = UserProfile(email=email, is_staff=False, is_active=active, full_name=full_name, short_name=short_name, last_login=now, date_joined=now, realm=realm, pointer=-1, is_bot=bool(bot_type), bot_type=bot_type, is_mirror_dummy=is_mirror_dummy, tos_version=tos_version, enable_stream_desktop_notifications=enable_stream_desktop_notifications, onboarding_steps=ujson.dumps([]), default_language=realm.default_language) if bot_owner is not None: # `user_profile.bot_owner = bot_owner` doesn't work on python 3.4 user_profile.bot_owner_id = bot_owner.id if bot_type or not active: password = None user_profile.set_password(password) user_profile.api_key = random_api_key() return user_profile
def create_user_profile(realm, email, password, active, bot_type, full_name, short_name, bot_owner, is_mirror_dummy): # type: (Realm, text_type, text_type, bool, Optional[int], text_type, text_type, Optional[UserProfile], bool) -> UserProfile now = timezone.now() email = UserManager.normalize_email(email) enable_stream_desktop_notifications = (realm.domain != 'zulip.com') user_profile = UserProfile( email=email, is_staff=False, is_active=active, full_name=full_name, short_name=short_name, last_login=now, date_joined=now, realm=realm, pointer=-1, is_bot=bool(bot_type), bot_type=bot_type, bot_owner=bot_owner, is_mirror_dummy=is_mirror_dummy, enable_stream_desktop_notifications=enable_stream_desktop_notifications, onboarding_steps=ujson.dumps([])) if bot_type or not active: password = None user_profile.set_password(password) user_profile.api_key = random_api_key() return user_profile
def create_user_profile(realm, email, password, active, bot_type, full_name, short_name, bot_owner, is_mirror_dummy, tos_version): # type: (Realm, Text, Text, bool, Optional[int], Text, Text, Optional[UserProfile], bool, Optional[Text]) -> UserProfile now = timezone.now() email = UserManager.normalize_email(email) user_profile = UserProfile(email=email, is_staff=False, is_active=active, full_name=full_name, short_name=short_name, last_login=now, date_joined=now, realm=realm, pointer=-1, is_bot=bool(bot_type), bot_type=bot_type, is_mirror_dummy=is_mirror_dummy, tos_version=tos_version, onboarding_steps=ujson.dumps([]), default_language=realm.default_language) if bot_owner is not None: # `user_profile.bot_owner = bot_owner` doesn't work on python 3.4 user_profile.bot_owner_id = bot_owner.id if bot_type or not active: password = None user_profile.set_password(password) user_profile.api_key = random_api_key() return user_profile
def create_user_profile(realm: Realm, email: str, password: Optional[str], active: bool, bot_type: Optional[int], full_name: str, short_name: str, bot_owner: Optional[UserProfile], is_mirror_dummy: bool, tos_version: Optional[str], timezone: Optional[str], tutorial_status: Optional[str] = UserProfile.TUTORIAL_WAITING, enter_sends: bool = False) -> UserProfile: now = timezone_now() email = UserManager.normalize_email(email) user_profile = UserProfile(email=email, is_staff=False, is_active=active, full_name=full_name, short_name=short_name, last_login=now, date_joined=now, realm=realm, pointer=-1, is_bot=bool(bot_type), bot_type=bot_type, bot_owner=bot_owner, is_mirror_dummy=is_mirror_dummy, tos_version=tos_version, timezone=timezone, tutorial_status=tutorial_status, enter_sends=enter_sends, onboarding_steps=ujson.dumps([]), default_language=realm.default_language, twenty_four_hour_time=realm.default_twenty_four_hour_time, delivery_email=email) if bot_type or not active: password = None user_profile.set_password(password) user_profile.api_key = random_api_key() return user_profile
def create_user_profile(realm, email, password, active, bot_type, full_name, short_name, bot_owner, is_mirror_dummy, tos_version, tutorial_status=UserProfile.TUTORIAL_WAITING, enter_sends=False): # type: (Realm, Text, Optional[Text], bool, Optional[int], Text, Text, Optional[UserProfile], bool, Optional[Text], Optional[Text], bool) -> UserProfile now = timezone_now() email = UserManager.normalize_email(email) user_profile = UserProfile(email=email, is_staff=False, is_active=active, full_name=full_name, short_name=short_name, last_login=now, date_joined=now, realm=realm, pointer=-1, is_bot=bool(bot_type), bot_type=bot_type, bot_owner=bot_owner, is_mirror_dummy=is_mirror_dummy, tos_version=tos_version, tutorial_status=tutorial_status, enter_sends=enter_sends, onboarding_steps=ujson.dumps([]), default_language=realm.default_language) if bot_type or not active: password = None user_profile.set_password(password) user_profile.api_key = random_api_key() return user_profile
def create_user_profile(realm: Realm, email: str, password: Optional[str], active: bool, bot_type: Optional[int], full_name: str, short_name: str, bot_owner: Optional[UserProfile], is_mirror_dummy: bool, tos_version: Optional[str], timezone: Optional[str], tutorial_status: Optional[str] = UserProfile.TUTORIAL_WAITING, enter_sends: bool = False) -> UserProfile: now = timezone_now() email = UserManager.normalize_email(email) user_profile = UserProfile(email=email, is_staff=False, is_active=active, full_name=full_name, short_name=short_name, last_login=now, date_joined=now, realm=realm, pointer=-1, is_bot=bool(bot_type), bot_type=bot_type, bot_owner=bot_owner, is_mirror_dummy=is_mirror_dummy, tos_version=tos_version, timezone=timezone, tutorial_status=tutorial_status, enter_sends=enter_sends, onboarding_steps=ujson.dumps([]), default_language=realm.default_language, twenty_four_hour_time=realm.default_twenty_four_hour_time, delivery_email=email) if bot_type or not active: password = None user_profile.set_password(password) user_profile.api_key = generate_api_key() return user_profile
def create_user(self, email, password=None, is_staff=False, is_active=True, **extra_fields): # Создает пользователся с данными: имя, email, пароль email = UserManager.normalize_email(email) user = self.model(email=email, is_active=is_active, is_staff=is_staff, **extra_fields) if password: user.set_password(password) user.save() return user
def create_user(self, email, password=None, **extra_fields): now = timezone.now() email = BaseUserManager.normalize_email(email) user = self.model(email=email, is_staff=False, is_active=True, is_superuser=False, last_login=now, date_joined=now, **extra_fields) user.set_password(password) user.save(using=self._db) return user
def create_django_user(self, new_user_object): user_Mgr = UserManager() username = new_user_object['user_name'] email = new_user_object['email_address'] password = new_user_object['user_password'] email = user_Mgr.normalize_email(email) django_user = User.objects.create_user(username, email, password) return django_user
def create_user(self, email, password=None): if not email: raise ValueError('Users must have an email address') user = self.model(username=UserManager.normalize_email(email), ) user.set_password(password) user.save(using=self._db) return user
def create_user(self, email, password=None, is_staff=False, is_active=True,**extra_fields): email = UserManager.normalize_email(email) extra_fields.pop('username', None) user = self.model(email=email, is_active=is_active, is_staff=is_staff, **extra_fields) if password: user.set_password(password) user.save() return user
def create_user(self, email=None, password=None, **extra_fields): now = timezone.now() if not email: raise ValueError('The given email must be set') email = UserManager.normalize_email(email) user = self.model(email=email, is_staff=False, is_active=True, is_superuser=False, last_login=now, **extra_fields) user.set_password(password) user.save(using=self._db) return user
def create_user(self, username, email, password=None, **kwargs): now = timezone.now() if not email: raise ValueError('The given email address must be set') email = UserManager.normalize_email(email) user = self.model(username=username, email=email, date_joined=now, is_superuser=False, is_staff=False, **kwargs) user.set_password(password) user.save(using=self._db) return user
def create_user(self, email=None, password=None, **extra_fields): now = datetime.utcnow() if not email: raise ValueError('Email address must be given') email = UserManager.normalize_email(email) user = self.model(email=email, is_staff=False, is_active=False, is_superuser=False, last_login=now, reg_code=self.create_reg_code(), **extra_fields) user.set_password(password) user.save(using=self._db) return user
def normalize_email(value): """ Normalize an email address by lowercasing domain part. Return None if the given value doesn't appear to be an email address. """ if email_re.search(smart_unicode(value)): return UserManager.normalize_email(value) return None
def create_user(self, username, email, password=None): #,**extra_fields): user = self.model() if not email: raise ValueError('Users must have an email address.') email = UserManager.normalize_email(email) user = self.model(username=username, email=email) #user = self.model(username=username, email=email, is_staff=False, is_active=True, is_superuser=False) user.set_password(password) user.save(using=self._db) return user
def create_user_profile( realm: Realm, email: str, password: Optional[str], active: bool, bot_type: Optional[int], full_name: str, bot_owner: Optional[UserProfile], is_mirror_dummy: bool, tos_version: Optional[str], timezone: Optional[str], tutorial_status: str = UserProfile.TUTORIAL_WAITING, enter_sends: bool = False, force_id: Optional[int] = None, force_date_joined: Optional[datetime] = None, ) -> UserProfile: if force_date_joined is None: date_joined = timezone_now() else: date_joined = force_date_joined email = UserManager.normalize_email(email) extra_kwargs = {} if force_id is not None: extra_kwargs["id"] = force_id user_profile = UserProfile( is_staff=False, is_active=active, full_name=full_name, last_login=date_joined, date_joined=date_joined, realm=realm, is_bot=bool(bot_type), bot_type=bot_type, bot_owner=bot_owner, is_mirror_dummy=is_mirror_dummy, tos_version=tos_version, timezone=timezone, tutorial_status=tutorial_status, enter_sends=enter_sends, onboarding_steps=orjson.dumps([]).decode(), default_language=realm.default_language, delivery_email=email, **extra_kwargs, ) if bot_type or not active: password = None if user_profile.email_address_is_realm_public(): # If emails are visible to everyone, we can set this here and save a DB query user_profile.email = get_display_email_address(user_profile) user_profile.set_password(password) user_profile.api_key = generate_api_key() return user_profile
def create_user(self, email, password=None): """ Creates and saves a User with the given email """ if not email: raise ValueError('Users must have an email address') user = self.model(email=UserManager.normalize_email(email), ) user.set_password(password) user.save(using=self._db) return user
def create_user(cls, username, email, password=None, **extra_fields): now = timezone.now() if not username: raise ValueError('The given username must be set') email = UserManager.normalize_email(email) user = CustomEmployer(username=username, email=email, is_staff=False, is_active=True, is_superuser=False, last_login=now, date_joined=now, **extra_fields) user.set_password(password) user.save() return user
def create_user(self, email, password=None, **extra_fields): now = timezone.now() if not email: raise ValueError('Users must have an email address') user = self.model( email=UserManager.normalize_email(email), is_staff=False, is_active=True, is_superuser=False, last_login=now, date_joined=now, **extra_fields) user.set_password(password) user.save(using=self._db) return user
def create_user(self, email, password=None, **extra_fields): now = timezone.now() if not email: raise ValueError(u'رایانامه باید تعیین شود') email = UserManager.normalize_email(email) user = self.model(email=email, is_active=True, is_admin=False, last_login=now, date_joined=now, **extra_fields) user.set_password(password) user.save(using=self._db) return user
def create_user(self, email, password=None, **extra_fields): if not email: raise ValueError('The given email must be set') email = UserManager.normalize_email(email) user = self.model(email=email, is_staff=False, is_active=True, last_login=now(), **extra_fields) user.set_password(password) user.save() return user
def create_user(self, email, password=None): """ Creates and saves a User with the given email """ if not email: raise ValueError('Users must have an email address') user = self.model( email=UserManager.normalize_email(email), ) user.set_password(password) user.save(using=self._db) return user
def create_user(self, email=None, password=None): #if not username: # raise ValueError('Users must have a username') email = UserManager.normalize_email(email) user = self.model(email=email, password=password) #user = User(username=username, email=email) #user.username = username user.is_superuser = False user.set_password(password) #user.is_active = True user.save(using=self._db) return user
def create_user(self, email, password=None, **extra_fields): """ Creates and saves a User with the given username, email and password. """ now = timezone.now() if not email: raise ValueError('The given email must be set') email = UserManager.normalize_email(email) user = self.model(email=email, is_staff=False, is_superuser=False, last_login=now, date_joined=now, **extra_fields) user.set_password(password) user.save(using=self._db) return user
def create_user(self, email, password=None, **extra_fields): now = timezone.now() if not email: raise ValueError('Users must have an email address') user = self.model(email=UserManager.normalize_email(email), is_staff=False, is_active=True, is_superuser=False, last_login=now, date_joined=now, **extra_fields) user.set_password(password) user.save(using=self._db) return user
def create_user(self, usuario, password, nombre,apellidos,email=""): """ Creates and saves user with the given username and password. """ # if not email: # raise ValueError('Favor de proporcionar un correo valido') if not usuario: raise ValueError('Favor de proporcionar un usuario valido') usuario = self.model( email=UserManager.normalize_email(email),nombre=nombre,apellidos=apellidos,usuario=usuario ) usuario.set_password(password) usuario.save(using=self._db) return usuario
def create_user(self, usuario, password, nombre,apellidos="",email=""): """ Creates and saves user with the given username and password. """ # if not email: # raise ValueError('Favor de proporcionar un correo valido') if not usuario: raise ValueError('Favor de proporcionar un usuario valido') usuario = self.model( email=UserManager.normalize_email(email),nombre=nombre,apellidos=apellidos,usuario=usuario ) usuario.set_password(password) usuario.save(using=self._db) return usuario
def create_user(self, name, email, password, superuser=False, **extra_fields): now = timezone.now() #Required fields if not name or not email or not password: raise ValueError('Name, Email or Password cannot be empty') #Normalize and validate email email = UserManager.normalize_email(email) validate_email(email) user = self.model(name=name, email=email, is_active=True, is_staff=superuser, is_superuser=superuser, last_login=now, **extra_fields) user.set_password(password) user.save(using=self._db) return user
def create_user(self, username, email, password=None): ''' create_user()参数必须是USERNAME_FIELD 和 所有的REQUIRED_FIELD ''' if not email: raise ValueError('用户必须输入Email地址') user = self.model( username=username, email=UserManager.normalize_email(email), is_staff=False, is_active=True, is_superuser=False ) user.set_password(password) user.save(using=self._db) return user
def create_user(self, email, username=None, password=None, first_name=""): """ Creates and saves a User with the given email, username if given """ if not email: raise ValueError('Users must have an email address') if not username: username = hashlib.md5(email).hexdigest() user = self.model( email=UserManager.normalize_email(email), username=username, first_name=first_name, ) user.set_password(password) user.save(using=self._db) return user
def create_user(self, email, username=None, password=None, first_name=""): """ Creates and saves a User with the given email, username if given """ if not email: raise ValueError('Users must have an email address') if not username: username = hashlib.md5(email).hexdigest() user = self.model( email = UserManager.normalize_email(email), username = username, first_name = first_name, ) user.set_password(password) user.save(using=self._db) return user
def create_user(self, username, email=None, password=None): """ Creates and saves a User with the given username, email and password. """ now = timezone.now() if not username: raise ValueError('The given username must be set') email = UserManager.normalize_email(email) try: user = self.get(email=email) except self.model.DoesNotExist: user = self.model(username=username, email=email, is_staff=False, is_active=True, is_superuser=False, last_login=now, date_joined=now) user.set_password(password) user.save(using=self._db) return user
def __create_user(self, person_nr, name, email, phone_nr, is_utn_member, password, is_superuser, is_staff): now = timezone.now() if not person_nr: raise ValueError('Person number is required.') email = UserManager.normalize_email(email) user = self.model( person_nr=person_nr, email=email, name=name, phone_nr=phone_nr, password=password, is_utn_member=is_utn_member, is_superuser=is_superuser, is_staff=is_superuser, last_login=now) user.set_password(password) user.save() return user
def create_user(self, username, email=None, password=None, **extra_fields): """ Creates and saves a User with the given username, email and password. """ now = timezone.now() if not username: raise ValueError('The given username must be set') email = UserManager.normalize_email(email) user = self.model(username=username, email=email, is_staff=False, is_active=True, is_superuser=False, last_login=now, date_joined=now, **extra_fields) user.set_password(password) user.save(using=self._db) return user
def sign_up(request): if request.method == 'POST': user_form = MystradeUserForm(data = request.POST, expected_mystery = ANTIBOT_FIELD_VALUE) password_form = SetPasswordForm(data = request.POST, user = None) if user_form.is_valid() and password_form.is_valid(): email = UserManager.normalize_email(user_form.cleaned_data['email']) user = get_user_model()(username = user_form.cleaned_data['username'], first_name = user_form.cleaned_data['first_name'], last_name = user_form.cleaned_data['last_name'], email = email, send_notifications = user_form.cleaned_data['send_notifications'], timezone = user_form.cleaned_data['timezone'], bio = user_form.cleaned_data['bio'], contact = user_form.cleaned_data['contact'], palette = user_form.cleaned_data['palette'], is_active = False, is_staff = False, is_superuser = False, date_joined = now()) user.set_password(password_form.cleaned_data['new_password1']) user.save() # create an activation key and send it to the new user activation_key = _generate_activation_key(user) utils.send_notification_email("registration_activation", email, {'activation_url': request.build_absolute_uri(reverse('activation', args = [user.id, activation_key]))}) return render(request, 'profile/registration_complete.html') else: user_form = MystradeUserForm() user_form['send_notifications'].field.initial = True password_form = SetPasswordForm(user = None) # during sign up, user is not a MystradeUser yet request.user.DEFAULT_PALETTE = get_user_model().DEFAULT_PALETTE return render(request, 'profile/editprofile.html', {'user_form': user_form, 'password_form': password_form, 'sign_up': True, 'palettes': get_user_model().PALETTES})
def create_user_profile(realm, email, password, active, bot, full_name, short_name, bot_owner, is_mirror_dummy): now = timezone.now() email = UserManager.normalize_email(email) enable_stream_desktop_notifications = (realm.domain != 'zulip.com') user_profile = UserProfile(email=email, is_staff=False, is_active=active, full_name=full_name, short_name=short_name, last_login=now, date_joined=now, realm=realm, pointer=-1, is_bot=bot, bot_owner=bot_owner, is_mirror_dummy=is_mirror_dummy, enable_stream_desktop_notifications=enable_stream_desktop_notifications, onboarding_steps=ujson.dumps([])) if bot or not active: password = None user_profile.set_password(password) user_profile.api_key = random_api_key() return user_profile
def test_create_user_email_domain_normalize(self): returned = UserManager.normalize_email('*****@*****.**') self.assertEqual(returned, '*****@*****.**')
def test_create_user_email_domain_normalize_rfc3696(self): # According to http://tools.ietf.org/html/rfc3696#section-3 # the "@" symbol can be part of the local part of an email address returned = UserManager.normalize_email(r'Abc\@[email protected]') self.assertEqual(returned, r'Abc\@[email protected]')
def update_or_create_user(username=None, password=None, email=None, api_key=None, groups=None, is_super_user=False, first_name=None, last_name=None): """ Update/Create the user matching the username :param username: the User.username :param password: the password :param email: the user email :param api_key: the optional api_key, it will be generated otherwise :param groups: The names of the groups that this user belongs to :param is_super_user: Default false, True to make a superuser :param first_name: the user first name :param last_name: the user last name :return: """ from footprint.main.publishing.user_publishing import on_user_post_save logger.info("Updating or creating user %s" % username) user_manager = get_user_model().objects user = get_single_value_or_create( user_manager.filter(username=username), lambda: user_manager.create_user(username, email, password) if \ not is_super_user else \ user_manager.create_superuser(username, email, password)) # Update these in case the configuration was updated user.email = UserManager.normalize_email(email) if password: user.set_password(password) if first_name: user.first_name = first_name if last_name: user.last_name = last_name if is_super_user: user.is_superuser = True user.save() # Add the user to the groups. # Sometimes the users will reference Groups of sibling scenarios that # haven't been created yet. In this case just create a name-only # instance of the group. if groups: user.groups.clear() for group in groups: user.groups.add(Group.objects.get_or_create(name=group)[0]) logger.info("Assigned user %s to groups %s", user.email, ', '.join(groups)) # Make sure the user has permission to update every class # We limit permissions on individual instances new_permissions = set(Permission.objects.all()) - set( user.user_permissions.all()) user.user_permissions.add(*new_permissions) api_key_instance = ApiKey.objects.get_or_create(user=user)[0] if api_key and api_key_instance.key != api_key: api_key_instance.key = api_key api_key_instance.save() # Invoke post-save publishing outside of footprint_init. footprint_init has a ConfigEntity- # centric way of publishing that this would clash with if not settings.FOOTPRINT_INIT: on_user_post_save(sender=get_user_model(), instance=user) return {'user': user, 'api_key': api_key_instance}
def test_create_user_email_domain_normalize(self): returned = UserManager.normalize_email("*****@*****.**") self.assertEqual(returned, "*****@*****.**")
def update_or_create_user( username=None, password=None, email=None, api_key=None, groups=None, is_super_user=False, first_name=None, last_name=None, ): """ Update/Create the user matching the username :param username: the User.username :param password: the password :param email: the user email :param api_key: the optional api_key, it will be generated otherwise :param groups: The names of the groups that this user belongs to :param is_super_user: Default false, True to make a superuser :param first_name: the user first name :param last_name: the user last name :return: """ from footprint.main.publishing.user_publishing import on_user_post_save logger.info("Updating or creating user %s" % username) user_manager = get_user_model().objects user = get_single_value_or_create( user_manager.filter(username=username), lambda: user_manager.create_user(username, email, password) if not is_super_user else user_manager.create_superuser(username, email, password), ) # Update these in case the configuration was updated user.email = UserManager.normalize_email(email) if password: user.set_password(password) if first_name: user.first_name = first_name if last_name: user.last_name = last_name if is_super_user: user.is_superuser = True user.save() # Add the user to the groups. # Sometimes the users will reference Groups of sibling scenarios that # haven't been created yet. In this case just create a name-only # instance of the group. if groups: user.groups.clear() for group in groups: user.groups.add(Group.objects.get_or_create(name=group)[0]) logger.info("Assigned user %s to groups %s", user.email, ", ".join(groups)) # Make sure the user has permission to update every class # We limit permissions on individual instances new_permissions = set(Permission.objects.all()) - set(user.user_permissions.all()) user.user_permissions.add(*new_permissions) api_key_instance = ApiKey.objects.get_or_create(user=user)[0] if api_key and api_key_instance.key != api_key: api_key_instance.key = api_key api_key_instance.save() # Invoke post-save publishing outside of footprint_init. footprint_init has a ConfigEntity- # centric way of publishing that this would clash with if not settings.FOOTPRINT_INIT: on_user_post_save(sender=get_user_model(), instance=user) return {"user": user, "api_key": api_key_instance}
def test_create_user_email_domain_normalize_with_whitespace(self): returned = UserManager.normalize_email('email\ [email protected]') self.assertEqual(returned, 'email\ [email protected]')
def test_create_user_email_domain_normalize_rfc3696(self): # According to https://tools.ietf.org/html/rfc3696#section-3 # the "@" symbol can be part of the local part of an email address returned = UserManager.normalize_email(r"Abc\@[email protected]") self.assertEqual(returned, r"Abc\@[email protected]")
def clean_email(self): return UserManager.normalize_email(self.cleaned_data["email"])
def test_create_user_email_domain_normalize_with_whitespace(self): returned = UserManager.normalize_email(r"email\ [email protected]") self.assertEqual(returned, r"email\ [email protected]")
def normalize_email(cls, email): return DjangoUserManager.normalize_email(email).lower()