Exemple #1
0
def login(request):

    if request.user.username:
        return redirect(dashBoard)
    message = ''
    form = LoginForm()
    if request.method == 'POST':
        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 None:
                message = 'Invalid login details!'
            else:
                UserLogin(request, user)

                request.session['city'] = 'Bangalore'
                request.session['address'] = 'BTM'

                return redirect(dashBoard)

    return render(request, 'site/login.html', {
        'form': form,
        'message': message
    })
Exemple #2
0
def login(request):
	username_temp = request.POST.get('username', '')
	password_temp = request.POST.get('password', '')
	user = Authenticate(request, username = username_temp, password = password_temp)
	if user is not None:
		UserLogin(request, user)
		return JsonResponse( {'message': 'Successful login!', 'balance': user.profile.balance}, status = 200 )
	else:
		return JsonResponse( {'error': 'Invalid details!'}, status = 401 )
Exemple #3
0
def register(request):
    data = {
        "status": "success",
        "message": "",
    }
    if request.method == "POST":
        username = request.POST.get("username")
        user_firstname = request.POST.get("firstname")
        user_lastname = request.POST.get("lastname")
        password = request.POST.get("password")
        email = request.POST.get("email")
        user_gender = request.POST.get("gender")
        if username and user_firstname and user_lastname and password and email and user_gender:
            if not is_valid_email(email):
                data.update({"status": "error", "message": "An invalid email"})
                return HttpResponse(json.dumps(data),
                                    mimetype="application/json")

            flag, acct = Account.objects.new_user(username, user_firstname, user_lastname, password, \
                            email, user_gender)
            if flag:
                user = authenticate(username=username, password=password)
                UserLogin(request, user)
                from tastypie.models import ApiKey
                api = update_api_key(user)
                data.update({
                    "message": "Register successfully",
                    "data": {
                        "api_key": api.key,
                        "username": username,
                    }
                })
            else:
                data.update({
                    "status":
                    "error",
                    "message":
                    "The user with this username already exists"
                })
            return HttpResponse(json.dumps(data), mimetype="application/json")
        else:
            data.update({
                "status":
                "error",
                "message":
                "username or user_firstname or user_lastname or password or email or user_gender is null"
            })
            return HttpResponse(json.dumps(data), mimetype="application/json")
    else:
        data.update({
            "status": "error",
            "message": "only support POST calling"
        })
    return HttpResponse(json.dumps(data), mimetype="application/json")
Exemple #4
0
def facebook_connection(request):
    data = {
        "status": "success",
        "message": "",
    }
    if request.method == "POST":
        user_firstname = request.POST.get("firstname")
        user_lastname = request.POST.get("lastname")
        email = request.POST.get("email")
        user_gender = request.POST.get("gender")
        facebook_id = request.POST.get("facebook_id")
        facebook_token = request.POST.get("facebook_token")
        if  user_firstname and user_lastname and email and user_gender and \
                facebook_id and facebook_token:
            if not is_valid_email(email):
                data.update({"status": "error", "message": "An invalid email"})
                return HttpResponse(json.dumps(data),
                                    mimetype="application/json")

            flag, acct = Account.objects.facebook_connection(
                facebook_id, facebook_token, user_firstname, user_lastname,
                email, user_gender)
            if flag:
                user = authenticate(username=facebook_id, password=facebook_id)
                UserLogin(request, user)
                from tastypie.models import ApiKey
                api = update_api_key(user)
                data.update({
                    "message": "Facebook connection successfully",
                    "data": {
                        "api_key": api.key,
                        "username": facebook_id,
                    }
                })
            else:
                data.update({
                    "status": "error",
                    "message": "user's account has issues"
                })
            return HttpResponse(json.dumps(data), mimetype="application/json")
        else:
            data.update({
                "status":
                "error",
                "message":
                "username or user_firstname or user_lastname or password or email or user_gender is null"
            })
            return HttpResponse(json.dumps(data), mimetype="application/json")
    else:
        data.update({
            "status": "error",
            "message": "only support POST calling"
        })
    return HttpResponse(json.dumps(data), mimetype="application/json")
Exemple #5
0
def login(request):
    data = {
        "status": "success",
        "message": "",
    }
    if request.method == "POST":
        username = request.POST.get("username")
        password = request.POST.get("password")
        if username:
            if password:
                user = authenticate(username=username, password=password)
                if user and user.is_active:
                    UserLogin(request, user)
                    from tastypie.models import ApiKey
                    api = update_api_key(user)
                    data.update({
                        "message": "Login successfully",
                        "data": {
                            "api_key": api.key,
                            "username": user.username,
                        }
                    })
                    return HttpResponse(json.dumps(data),
                                        mimetype="application/json")
                else:
                    data.update({
                        "status": "error",
                        "message": "incorrect username or password"
                    })
                    return HttpResponse(json.dumps(data),
                                        mimetype="application/json")
            else:
                data.update({
                    "status": "error",
                    "message": "password can't be blank"
                })
                return HttpResponse(json.dumps(data),
                                    mimetype="application/json")
        else:
            data.update({
                "status": "error",
                "message": "username can't be blank"
            })
            return HttpResponse(json.dumps(data), mimetype="application/json")
    else:
        data.update({
            "status": "error",
            "message": "only support POST calling"
        })
    return HttpResponse(json.dumps(data), mimetype="application/json")
Exemple #6
0
def login(request):
    if request.method == 'POST':
        form = AuthenticationForm(data=request.POST)

        if form.is_valid():
            username = form.cleaned_data.get('username')
            user = User.objects.get(username=username)
            UserLogin(request, user)
            try:
                next_page = request.GET['next']
            except:
                return redirect('home')
            else:
                return HttpResponseRedirect(next_page)

    else:
        form = AuthenticationForm()

    context = {'title': "Login", 'form': form}
    return render(request, 'users/login.html.django', context)
Exemple #7
0
def register(request):
    if request.method == 'POST':
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            user = User.objects.get(username=username)
            UserLogin(request, user)
            messages.success(request,
                             f'Account "{username}" Created Successfully!')
            try:
                next_page = request.GET['next']
            except:
                return redirect('home')
            else:
                return HttpResponseRedirect(next_page)

    else:
        form = UserRegisterForm()

    context = {'title': "Register", 'form': form}
    return render(request, 'users/register.html.django', context)
Exemple #8
0
def login(request):
    if (request.method == "GET"):
        if request.user.is_authenticated:
            return redirect("/")
        else:
            return render(request, "accounts/login.html")
    if (request.method == "POST"):
        email = request.POST.get("email")
        password = request.POST.get("password")
        authenticatedUser = authenticate(
            request, email=email,
            password=password)  #authenticate using custom user model
        if authenticatedUser is not None:
            UserLogin(request, authenticatedUser)
            user = User.objects.get(email=request.user.email)
            if (user.verified):
                return redirect("/")
            else:
                return redirect("/accounts/verifyEmail")
        else:
            messages.error(request, "Username And Password doesn't match")
            return redirect("/accounts/login")
Exemple #9
0
def signUp(request):
    if (request.method == "GET"):
        if request.user.is_authenticated:
            return redirect("/")
        else:
            return render(request, "accounts/signUp.html")
    if (request.method == "POST"):
        if not User.objects.filter(email=request.POST.get("email")).exists():
            if (request.POST.get("firstName")):
                firstName = request.POST.get("firstName")
            if (request.POST.get("lastName")):
                lastName = request.POST.get("lastName")
            if (request.POST.get("email")):
                email = request.POST.get("email")
            if (request.POST.get("password")):
                password = request.POST.get("password")
                createdUser = User.objects.createConsumerUser(
                    email, firstName, lastName, password)
                user = UserLogin(request, createdUser)
                return redirect("/accounts/verifyEmail")
        else:
            messages.error(request, "Email is already registered")
            return redirect("/accounts/signUp")