def signin(request): if request.user.is_authenticated(): return HttpResponseRedirect('/') form = SignInForm(request.POST or None) if form.is_valid(): username_or_email = form.cleaned_data['username_or_email'] password = form.cleaned_data['password'] user_obj = None try: user_obj = User.objects.get(username=username_or_email) except: user_obj = User.objects.get(email=username_or_email) if user_obj: if check_password(password, user_obj.password): user = authenticate(username=user_obj.username, password=password) if user: if user.is_active: login(request, user) request.session['username'] = user_obj.username # Append the user to online users set in redis. # Also create a notifications object which will be used # to initiate and send notifications redis_obj = StrictRedis(db=9) redis_obj.sadd('online:users', user_obj.username) next = request.GET.get('next', '') if next: return HttpResponseRedirect(next) else: return HttpResponseRedirect(reverse('profile', args=(str(user_obj.username),))) else: messages.info(request, 'Please activate your account first.') return HttpResponseRedirect(reverse('accounts.views.signin.')) return render(request, 'accounts/signin.html', { 'form': form, })
def create_order(request): loginform = SignInForm() addressForm = AddressForm() context = {'loginform': loginform, 'addressForm': addressForm} cart_obj = Cart.objects.new_or_get( request) #getting existing cart obj or creating a new one bill_obj = BillingProfile.objects.get_or_new( request) #getting existing cart obj or creating a new one if cart_obj and bill_obj: # if both cart_obj and bill_obj are created then either return existing order obj or create a new one # hence if in case you click on the order button again it will just return the existing one order_obj = Order.objects.get_or_new(cart_obj, bill_obj) context['order_obj'] = order_obj add_list = Address.objects.filter(billing_profile=bill_obj) # above we have obtained a query list of address objects from billing profile # note : a billing profile can have many address objects context['addList'] = add_list return render(request, "orders/placeorder.html", context)
def sign_in_view(request): # generate_booking_data() old_user = request.user # print(old_user.user_id) if not old_user.is_anonymous: return redirect("search:search") if request.method == "POST": sign_in_form = SignInForm(data=request.POST) if sign_in_form.is_valid(): user = sign_in_form.get_user() login(request, user) if user.is_admin: return redirect("dashboard:dashboard") else: return redirect("search:search") else: return render(request, "accounts/signin.html", {"form": sign_in_form}) else: sign_in_form = SignInForm() return render(request, "accounts/signin.html", {"form": sign_in_form})
def test_invalid_data(self): form = SignInForm(data={}) self.assertFalse(form.is_valid()) self.assertEqual(len(form.errors), 3)
def setUpTestData(cls): cls.form = SignInForm()