def respond_with_new_user(self, request, next_url, desuprofile): """ This implements the following case: 2. No Kompassi account is linked to this Desuprofile, and no Kompassi account matches the Desuprofile by email address. A new Kompassi account is created and logged in. """ User = get_user_model() password = create_temporary_password() # Kompassi has stricter rules for username validation than Desusite username = desuprofile.username.lower() try: valid_username(username) except DjangoValidationError: username = None else: try: User.objects.get(username=username) except User.DoesNotExist: # Username is free pass else: # Username clash with an existing account, use safe username username = None if username is None: username = "******".format(id=desuprofile.id) with transaction.atomic(): user = User( username=username, is_active=True, is_staff=False, is_superuser=False, ) user.set_password(password) user.save() person = Person( first_name=desuprofile.first_name.strip(), surname=desuprofile.last_name.strip(), nick=desuprofile.nickname.strip(), email=desuprofile.email.strip(), phone=desuprofile.phone.strip(), birth_date=( datetime.strptime(desuprofile.birth_date.strip(), '%Y-%m-%d').date() if desuprofile.birth_date else None ), notes='Luotu Desuprofiilista', user=user, ) person.save() connection = Connection( id=int(desuprofile.id), desuprofile_username=desuprofile.username, user=user, ) connection.save() person.apply_state_new_user(request, password) messages.success(request, 'Sinulle on luotu Desuprofiiliisi liitetty Kompassi-tunnus. Tervetuloa Kompassiin!') return respond_with_connection(request, next_url, connection)
def respond_with_new_user(self, request, next_url, desuprofile): User = get_user_model() password = create_temporary_password() # Kompassi has stricter rules for username validation than Desusite username = desuprofile.username.lower() try: valid_username(username) except DjangoValidationError: username = None with transaction.atomic(): try: User.objects.get(username=username) except User.DoesNotExist: # Username is free pass else: # Username clash with an existing account, use safe username username = None if username is None: username = "******".format(id=desuprofile.id) user = User( username=username, is_active=True, is_staff=False, is_superuser=False, ) user.set_password(password) user.save() person = Person( first_name=desuprofile.first_name, surname=desuprofile.last_name, nick=desuprofile.nickname, email=desuprofile.email, phone=desuprofile.phone, birth_date=datetime.strptime(desuprofile.birth_date, '%Y-%m-%d').date() if desuprofile.birth_date else None, notes=u'Luotu Desuprofiilista', user=user, ) person.save() connection = Connection( id=int(desuprofile.id), desuprofile_username=desuprofile.username, user=user, ) connection.save() person.setup_email_verification(request) if 'ipa_integration' in settings.INSTALLED_APPS: from ipa_integration.utils import create_user create_user(user, password) messages.success(request, u'Sinulle on luotu Desuprofiiliisi liitetty Kompassi-tunnus. Tervetuloa Kompassiin!') return respond_with_connection(request, next_url, connection)