def register(request): if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): credentials = form.cleaned_data username = credentials['username'] password = credentials['password'] user = authenticate(request, **credentials) username_taken = user_username_taken(username=username) if user is not None: login(request, user) return redirect('chat:index') elif user is None and not username_taken: user = user_create(username=username, password=password) login(request, user) return redirect('chat:index') else: msg = 'Username taken or wrong password' messages.warning(request, message=msg) else: for attr, err_list in form.errors.items(): for err in err_list: err_msg = f'{attr}: {err}' messages.warning(request, message=err_msg) context = {'form': LoginForm} return render(request, 'registration/login.html', context)
def test_form_error_for_wrong_password(self): user = User.objects.create_user(email='*****@*****.**', password='******', first_name='Test') form = LoginForm(data={'email': '*****@*****.**', 'password': '******'}) self.assertFalse(form.is_valid()) self.assertEqual(form.errors['email'][0], "Login is invalid. Please try again")
def login_user(request): if request.user.is_authenticated: return HttpResponseRedirect("/landing") try: if request.method == 'POST': login_form = LoginForm(request.POST or None) register_form = RegisterForm(request.POST or None) if login_form.is_valid(): username = login_form.cleaned_data['lusername'] password = login_form.cleaned_data['lpassword'] user = authenticate(request, username=username, password=password) if user is not None: login(request, user) return HttpResponseRedirect("/landing") else: return render( request, 'login.html', { 'login': LoginForm(), "register": RegisterForm(), "incorrect": True }) elif register_form.is_valid(): name = register_form.cleaned_data['name'] lastname = register_form.cleaned_data.get('lastname', None) username = register_form.cleaned_data['username'] email = register_form.cleaned_data['email'] user_informations = UserInformations() user_informations.firstname = name user_informations.lastname = lastname user_informations.username = username user_informations.email = email user_informations.save() confirm_token = ConfirmToken() confirm_token.token = ConfirmTool.generateConfirmToken() confirm_token.user = user_informations confirm_token.save() return HttpResponseRedirect("/confirm/%s" % confirm_token.token) return render(request, 'login.html', { 'login': LoginForm(), "register": RegisterForm() }) except Exception: traceback.print_exc() return notfound(request)
def chat(request): if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['last_name'] request.session['first_name'] = first_name request.session['last_name'] = last_name request.session['is_login'] = True return render_to_response('chat/chat.html', { 'first_name': first_name, 'last_name': last_name, 'jabber_id': settings.JABBER_ID, 'jabber_pass': settings.JABBER_PASSWORD, 'jabber_recipient': settings.JABBER_RECIPIENT, 'http_bind': settings.JABBER_HTTP_BIND_URL, }, context_instance=RequestContext(request)) else: return render_to_response('chat/login_form.html', {'form': form }, context_instance=RequestContext(request)) else: if request.session.get('is_login', False): first_name = request.session.get('first_name', False) last_name = request.session.get('last_name', False) return render_to_response('chat/chat.html', { 'first_name': first_name, 'last_name': last_name, 'jabber_id': settings.JABBER_ID, 'jabber_pass': settings.JABBER_PASSWORD, 'jabber_recipient': settings.JABBER_RECIPIENT, 'http_bind': settings.JABBER_HTTP_BIND_URL, }, context_instance=RequestContext(request)) else: form = LoginForm() return render_to_response('chat/login_form.html', {'form': form }, context_instance=RequestContext(request))
def login_page(request): form = LoginForm(request.POST or None) context = { 'form': form } print(request.user.is_authenticated) if form.is_valid(): print(form.cleaned_data) username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user = authenticate(request, username=username, password=password) if user is not None: login(request, user) return redirect('/') else: print("error.......") return render(request, "auth/login.html", context=context)
def chat(request): if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['last_name'] request.session['first_name'] = first_name request.session['last_name'] = last_name request.session['is_login'] = True return render_to_response( 'chat/chat.html', { 'first_name': first_name, 'last_name': last_name, 'jabber_id': settings.JABBER_ID, 'jabber_pass': settings.JABBER_PASSWORD, 'jabber_recipient': settings.JABBER_RECIPIENT, 'http_bind': settings.JABBER_HTTP_BIND_URL, }, context_instance=RequestContext(request)) else: return render_to_response('chat/login_form.html', {'form': form}, context_instance=RequestContext(request)) else: if request.session.get('is_login', False): first_name = request.session.get('first_name', False) last_name = request.session.get('last_name', False) return render_to_response( 'chat/chat.html', { 'first_name': first_name, 'last_name': last_name, 'jabber_id': settings.JABBER_ID, 'jabber_pass': settings.JABBER_PASSWORD, 'jabber_recipient': settings.JABBER_RECIPIENT, 'http_bind': settings.JABBER_HTTP_BIND_URL, }, context_instance=RequestContext(request)) else: form = LoginForm() return render_to_response('chat/login_form.html', {'form': form}, context_instance=RequestContext(request))
def home(request): if not request.user.is_authenticated: if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] password = form.cleaned_data['password'] user = authenticate(email=email, password=password) if user: login(request, user) return redirect('/') form = LoginForm() return render(request, 'chat/home.html', {'form': form}) else: #find roooms with user in them rooms = Room.objects.get_group_rooms(request.user) pms = Friend.objects.unread_requests(user=request.user) pms = len(pms) return render(request, 'chat/home.html', {'rooms': rooms, 'pms': pms})
def register(request): if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): user = form.save(commit=False) username = form.cleaned_data['username'] password = form.cleaned_data['password1'] user.set_password(password) user.save() user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) return redirect('chats') else: form = LoginForm() context = {'form': form} return render(request, 'chat/user_register.html', context)
def post(self, request, *args, **kwargs): form = LoginForm(request.POST) # данные, переданные пользователем # Проверка формы на загрузку if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password'] # проверка на существование такого пользователя user = authenticate(username=username, password=password) if user is not None: login(request, user) return redirect(reverse('chat_get')) # reverse - возращает абс. ссылку из chat\urls.py # Возвращает перенаправление на URL указанный через аргументы. return render(request, 'login.html', { 'form': form, 'form_action': reverse('login') })
def test_form_validation(self): user = User.objects.create_user(email='*****@*****.**', password='******', first_name='Test') form = LoginForm(data={'email': '*****@*****.**', 'password': '******'}) self.assertTrue(form.is_valid())