def signup(request): # Author: Sergio Galán context_dict = {} if request.method == 'POST': # Comprobamos que se ha confirmado la contraseña correctamente if request.POST.get('password') != request.POST.get('password2'): signup_form = SignupForm(data=request.POST) # Se carga el error correspondiente signup_form.add_error('password2', SIGNUP_ERROR_PASSWORD) context_dict['user_form'] = signup_form # Volvemos a cargar el formulario con los errores return render(request, "mouse_cat/signup.html", context_dict) user_form = SignupForm(data=request.POST) # Comprobamos que el registro sea válido if user_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() # Además de registralo, lo dejamos logueado y redireccionamos # al index django.contrib.auth.login(request, user) return redirect(reverse('index')) # Si el registro no es válido else: if "username" in user_form.errors: # Quitamos el error por defecto y ponemos el de los tests user_form.errors['username'] = [] user_form.add_error('username', SIGNUP_ERROR_USER) context_dict['user_form'] = user_form # Volvemos a renderizar el formulario con los errores return render(request, "mouse_cat/signup.html", context_dict) # En las peticiones get devolvemos el formulario correspondiente else: context_dict['user_form'] = SignupForm() return render(request, "mouse_cat/signup.html", context_dict)
def signup(request): """ signup (main author: Rafael Sanchez) ---------- Input parameters: request: received request. In its body receives two strings, username and the user password ---------- Returns: It renders "mouse_cat/signup.html" template ---------- Raises: None ---------- Description: Case method 'POST': If the fields validation is good, it creates a new user and it is autheticated in the application. Case method 'GET': It renders "mouse_cat/signup.html" in order to ahow signup form. It both cases the user is required to be anonymous, i.e, not logged """ if request.method == 'POST': user_form = SignupForm(data=request.POST) if user_form.is_valid(): cd = user_form.cleaned_data else: return render(request, "mouse_cat/signup.html", {'user_form': user_form}) if cd['password'] != cd['password2']: user_form.add_error( 'password2', 'Password and Repeat password are not the same') return render(request, "mouse_cat/signup.html", {'user_form': user_form}) try: validate_password(cd['password']) except ValidationError as err: user_form.errors['password'] = err.messages # user_form.add_error('password', ' '.join(err.messages)) return render(request, "mouse_cat/signup.html", {'user_form': user_form}) # try: user = user_form.save() user.set_password(user.password) user.save() login(request, user) return render(request, "mouse_cat/signup.html") context_dict = {'user_form': SignupForm()} return render(request, "mouse_cat/signup.html", context_dict)
def signup_service(request): user_form = SignupForm() if request.method == 'POST': user_form = SignupForm(data=request.POST) if user_form.is_valid(): try: validate_password(request.POST.get('password'), user=None, password_validators=None) except ValidationError as e: for error in e.error_list: user_form.add_error(None, error) return render(request, 'mouse_cat/signup.html', {'user_form': user_form}) if request.POST.get('password') != request.POST.get('password2'): user_form.add_error(None, 'La clave y su repetición no coinciden.') return render(request, 'mouse_cat/signup.html', {'user_form': user_form}) user = user_form.save() if user is not None: user.set_password(user.password) user.save() login(request, user) request.session['counter'] = 0 return redirect(reverse('landing')) else: user_form.add_error(None, 'Error de registro.') return render(request, 'mouse_cat/signup.html', {'user_form': user_form}) else: user_form.add_error(None, 'Usuario duplicado.') return render(request, 'mouse_cat/signup.html', {'user_form': user_form}) else: return render(request, 'mouse_cat/signup.html', {'user_form': user_form})
def signup_service(request): user_form = SignupForm(data=request.POST) if request.method == 'POST': user_form = SignupForm(data=request.POST) if user_form.is_valid(): if user_form.cleaned_data.get( 'password') != user_form.cleaned_data.get('password2'): user_form.add_error( "password", "Password and Repeat password are not the same|La clave y su repetición no coinciden" ) return render(request, 'mouse_cat/signup.html', {'user_form': user_form}) user = user_form.save() user.set_password(user.password) request.session['counter'] = 0 user.save() return render(request, 'mouse_cat/signup.html', {'user_form': user_form}) else: user_form.add_error( "username", "A user with that username already exists|Usuario duplicado") user_form.add_error( "password", "(?=.too short)(?=.at least 6 characters)(?=.*too common)") return render(request, 'mouse_cat/signup.html', {'user_form': user_form}) else: return render(request, 'mouse_cat/signup.html', {'user_form': user_form})
def signup(request): if request.method == 'POST': signup_form = SignupForm(data=request.POST) signup_form.is_valid() # username = request.POST.get('username') password = request.POST.get('password') password2 = request.POST.get('password2') if password != password2: signup_form.add_error('password', 'La clave y su repetición no coinciden') return render(request, "mouse_cat/signup.html", {'user_form': signup_form}) try: validate_password(password) except ValidationError: err = '(?=.*too short)(?=.*at least 6 characters)(?=.*too common)' signup_form.add_error('password', err) # signup_form.errors['password'] = validerror.messages return render(request, "mouse_cat/signup.html", {'user_form': signup_form}) try: user = signup_form.save() user.set_password(user.password) user.save() login(request, user) return render(request, 'mouse_cat/index.html') except ValueError: signup_form.add_error('username', 'Usuario duplicado') return render(request, "mouse_cat/signup.html", {'user_form': signup_form}) else: signup_form = SignupForm() return render(request, 'mouse_cat/signup.html', {'user_form': signup_form})