def post(self, request, *args, **kwargs): form = CreateUserForm(self.registration_type, request.POST) if form.is_valid(): appuser = AppUser.objects.get(uuid=request.POST['appuseruuid']) if not settings.MAKE_USERNAME_EMAIL: username = request.POST['username'] else: username = request.POST['email'] if self.registration_type == 'invitation': user = User(email=request.POST['email'], username=username) user.save() appuser.user = user appuser.save() user = appuser.user SessionManager.register_user( user, username=username, password=request.POST['password'], first_name=request.POST['first_name'], last_name=request.POST['last_name'] ) messages.success(request, 'Registration complete! Please log in to continue.') UserToken.objects.filter( appuser=appuser, token_type__in=['registration', 'invitation'] ).all().delete() self.appuser.post_process_registration(self.registration_type, user) return redirect(reverse('session_manager_login')) else: self.context.update({ 'form': form, }) return HttpResponse(self.template.render(self.context, request)) self.context.update({ 'form': form, }) return HttpResponse(self.template.render(self.context, request))
def clean(self): super(CreateUserForm, self).clean() data = self.cleaned_data errors = [] clean_password = data['password'] pw_errors = _get_password_errors(clean_password) if pw_errors: errors.append('Invalid password: <ul>{}</ul>'.format( ''.join(pw_errors))) clean_username = self.cleaned_data['username'] un_errors = [] if len(clean_username) < 3: un_errors.append('<li>Must be at least 3 characters.</li>') un_invalid_char = False for char in clean_username: un_invalid_char = not char.isalpha() and not char.isnumeric( ) and char != '_' if un_invalid_char: un_errors.append( '<li>Letters, numbers, and underscores only, please.</li>') user_exists = SessionManager.get_user_by_username( username=clean_username) if user_exists: un_errors.append( '<li>A user with this username already exists.</li>') if un_errors: errors.append('Invalid username: <ul>{}</ul>'.format( ''.join(un_errors))) if errors: raise ValidationError(mark_safe('<br />'.join(errors))) return data
def clean(self): """ Enforces username and password requirements """ super(CreateUserForm, self).clean() data = self.cleaned_data errors = [] has_alpha = False has_number = False has_special = False clean_password = data['password'] password_errors = [] if len(clean_password) < 8: password_errors.append('<li>Must be at least 8 characters.</li>') for char in clean_password: if char.isalpha(): has_alpha = True if char.isnumeric(): has_number = True if char in special_chars: has_special = True if not has_alpha: password_errors.append( '<li>Must contain at least one letter.</li>') if not has_number: password_errors.append( '<li>Must contain at least one number.</li>') if not has_special: password_errors.append( '<li>Must contain at least one special character ({}).</li>'. format(', '.join(special_chars))) if password_errors: errors.append('Invalid password: <ul>{}</ul>'.format( ''.join(password_errors))) clean_username = self.cleaned_data['username'] username_errors = [] if len(clean_username) < 3: username_errors.append('<li>Must be at least 3 characters.</li>') username_invalid_char = False for char in clean_username: username_invalid_char = not char.isalpha() and not char.isnumeric( ) and char != '_' if username_invalid_char: username_errors.append( '<li>Letters, numbers, and underscores only, please.</li>') user_exists = SessionManager.get_user_by_username( username=clean_username) if user_exists: username_errors.append( '<li>A user with this username already exists.</li>') if username_errors: errors.append('Invalid username: <ul>{}</ul>'.format( ''.join(username_errors))) if errors: raise ValidationError(mark_safe('<br />'.join(errors))) return data