Ejemplo n.º 1
0
def render_account(request, message = None):
    if request.method == 'GET':
        if not AuthManager.is_logged_in(request):
            return redirect("/login")
        user = AuthManager.get_current_user(request)
        context = Context({ "user_listings": lib.USER.list_books(user.email),
                            "message": message})
        response = HttpResponse()
        tmpl = os.path.join(os.path.dirname(__file__), 'template', 'account.html')
        response.write(render_to_string(request, tmpl, context))
        return response
    if request.method == 'POST': 
        if not AuthManager.is_logged_in(request):
            return redirect("/login")
        user = AuthManager.get_current_user(request)
        try :
            password = cgi.escape(request.POST['password'])
            password2 = cgi.escape(request.POST['password_confirm'])
            if password != password2:
                raise PasswordDoesntMatchError()
            lib.USER.change_password(user.email, password)
            message = "Successfully updated password."
            context = Context({ "user_listings": lib.USER.list_books(user.email),
                                "message": message})
            response = HttpResponse()
            tmpl = os.path.join(os.path.dirname(__file__), 'template', 'account.html')
            response.write(render_to_string(request, tmpl, context))
            return response
        except PasswordDoesntMatchError as e:
            request.method = 'GET'
            return render_account(request, str(e))
Ejemplo n.º 2
0
def render_pp_return(request):
    if not AuthManager.is_logged_in(request):
        return redirect("/login")
    pp = lib.PAYPAL.Paypal()
    pp.sandbox = True
    pp.merchant_id = "*****@*****.**"
    pp.pdt_auth_token = "lwcdLbpiHFwN8PJr08Rv6JVvYcmp90ivctfoJSWgBvANCrG-7iXJ59e8Qy4"
    pdt_data = pp.get_pdt_object(request.GET["tx"])
    import logging
    logging.info("PDT data is: %s" % dir(pdt_data))

    total_cost = 0
    for item in request.session['items']:
        userbook = lib.USERBOOK.get(item)
        total_cost += userbook.price

    request.session["items"] = list()
    if total_cost == float(pdt_data.items[0].cost) * 100:
        # Good - the totals match
        response = HttpResponse()
        tmpl = os.path.join(os.path.dirname(__file__), 'template',
                            'checkout_complete.html')
        response.write(render_to_string(request, tmpl))
        return response
    else:
        response = HttpResponse()
        tmpl = os.path.join(os.path.dirname(__file__), 'template',
                            'checkout_error.html')
        response.write(render_to_string(request, tmpl))
        return response
Ejemplo n.º 3
0
def render_commit(request):
    if not AuthManager.is_logged_in(request):
        return redirect("/login")
    pp = lib.PAYPAL.Paypal()
    pp.sandbox = True
    pp.merchant_id = "*****@*****.**"
    pp.pdt_auth_token = "lwcdLbpiHFwN8PJr08Rv6JVvYcmp90ivctfoJSWgBvANCrG-7iXJ59e8Qy4"
    pp.return_url = "http://%s/paypal_pdt" % request.get_host()
    pp.set_pdt_mode("enabled")
    user = AuthManager.get_current_user(request)
    context = Context({"user": user})
    if 'items' in request.session.keys():
        total_cost = 0
        for item in request.session['items']:
            userbook = lib.USERBOOK.get(item)
            userbook.mark_as_sold(AuthManager.get_current_user(request))
            total_cost += userbook.price
        item = lib.PAYPAL.Item("TT-BASKET", "TexTrader Basket",
                               total_cost / 100)
        context["ppcheckout"] = pp.buy_now_button(item)
        tmpl = os.path.join(os.path.dirname(__file__), 'template',
                            'checkout.html')
    else:
        tmpl = os.path.join(os.path.dirname(__file__), 'template',
                            'emptybasket.html')
    response = HttpResponse()
    response.write(render_to_string(request, tmpl, context))
    return response
Ejemplo n.º 4
0
def render_basket_remove(request, listing_id):
    if not AuthManager.is_logged_in(request):
        return redirect("/login")
    if "items" in request.session.keys(
    ) and listing_id in request.session["items"]:
        request.session["items"].remove(listing_id)
    return redirect(render_basket)
Ejemplo n.º 5
0
def render_pp_return(request):
    if not AuthManager.is_logged_in(request):
        return redirect("/login")
    pp = lib.PAYPAL.Paypal()
    pp.sandbox = True
    pp.merchant_id = "*****@*****.**"
    pp.pdt_auth_token = "lwcdLbpiHFwN8PJr08Rv6JVvYcmp90ivctfoJSWgBvANCrG-7iXJ59e8Qy4"
    pdt_data = pp.get_pdt_object(request.GET["tx"])
    import logging
    logging.info("PDT data is: %s" % dir(pdt_data))

    total_cost = 0
    for item in request.session['items']:
        userbook = lib.USERBOOK.get(item)
        total_cost += userbook.price

    request.session["items"] = list()
    if total_cost == float(pdt_data.items[0].cost) * 100:
        # Good - the totals match
        response = HttpResponse()
        tmpl =  os.path.join(os.path.dirname(__file__), 'template', 'checkout_complete.html')
        response.write(render_to_string(request, tmpl))
        return response
    else:
        response = HttpResponse()
        tmpl =  os.path.join(os.path.dirname(__file__), 'template', 'checkout_error.html')
        response.write(render_to_string(request, tmpl))
        return response
Ejemplo n.º 6
0
def render_commit(request):
    if not AuthManager.is_logged_in(request):
        return redirect("/login")
    pp = lib.PAYPAL.Paypal();
    pp.sandbox = True
    pp.merchant_id = "*****@*****.**"
    pp.pdt_auth_token = "lwcdLbpiHFwN8PJr08Rv6JVvYcmp90ivctfoJSWgBvANCrG-7iXJ59e8Qy4"
    pp.return_url = "http://%s/paypal_pdt" % request.get_host()
    pp.set_pdt_mode("enabled")
    user = AuthManager.get_current_user(request)
    context = Context({"user": user})
    if 'items' in request.session.keys():
        total_cost = 0
        for item in request.session['items']:
            userbook = lib.USERBOOK.get(item)
            userbook.mark_as_sold(AuthManager.get_current_user(request))
            total_cost += userbook.price
        item = lib.PAYPAL.Item("TT-BASKET", "TexTrader Basket", total_cost / 100)
        context["ppcheckout"] = pp.buy_now_button(item)
        tmpl =  os.path.join(os.path.dirname(__file__), 'template', 'checkout.html')
    else:
        tmpl =  os.path.join(os.path.dirname(__file__), 'template', 'emptybasket.html')
    response = HttpResponse()
    response.write(render_to_string(request, tmpl, context))
    return response
Ejemplo n.º 7
0
def render_basket_add(request):
    if not AuthManager.is_logged_in(request):
        return redirect("/login")
    if not "items" in request.session.keys():
        request.session["items"] = set()
    if not request.POST["item"] in request.session["items"]:
        request.session["items"].append(request.POST["item"])
    return redirect(render_basket)
Ejemplo n.º 8
0
def render_basket_add(request):
    if not AuthManager.is_logged_in(request):
        return redirect("/login")
    if not "items" in request.session.keys():
        request.session["items"] = set()
    if not request.POST["item"] in request.session["items"]:
        request.session["items"].append(request.POST["item"])
    return redirect(render_basket)
Ejemplo n.º 9
0
def render_login(request):
    """
    Handler for requests to /login
    """
    if AuthManager.is_logged_in(request):
        return redirect("web.account.render_account")
    if request.method == "POST":
        return render_login_action(request)
    else:
        return render_login_form(request)
Ejemplo n.º 10
0
def render_login(request):
    """
    Handler for requests to /login
    """
    if AuthManager.is_logged_in(request):
        return redirect("web.account.render_account")
    if request.method == "POST":
        return render_login_action(request)
    else:
        return render_login_form(request)
Ejemplo n.º 11
0
def render_logout(request):
    """
    Handler for requests to /logout
    """
    if AuthManager.is_logged_in(request):
        AuthManager.set_logged_out(request)
        tmpl =  os.path.join(os.path.dirname(__file__), 'template', 'logout.html')
        response = HttpResponse()
        response.write(render_to_string(request, tmpl))
        return response
    else:
        return redirect("/login")
Ejemplo n.º 12
0
def render_account(request, message=None):
    if request.method == 'GET':
        if not AuthManager.is_logged_in(request):
            return redirect("/login")
        user = AuthManager.get_current_user(request)
        context = Context({
            "user_listings": lib.USER.list_books(user.email),
            "message": message
        })
        response = HttpResponse()
        tmpl = os.path.join(os.path.dirname(__file__), 'template',
                            'account.html')
        response.write(render_to_string(request, tmpl, context))
        return response
    if request.method == 'POST':
        if not AuthManager.is_logged_in(request):
            return redirect("/login")
        user = AuthManager.get_current_user(request)
        try:
            password = cgi.escape(request.POST['password'])
            password2 = cgi.escape(request.POST['password_confirm'])
            if password != password2:
                raise PasswordDoesntMatchError()
            lib.USER.change_password(user.email, password)
            message = "Successfully updated password."
            context = Context({
                "user_listings": lib.USER.list_books(user.email),
                "message": message
            })
            response = HttpResponse()
            tmpl = os.path.join(os.path.dirname(__file__), 'template',
                                'account.html')
            response.write(render_to_string(request, tmpl, context))
            return response
        except PasswordDoesntMatchError as e:
            request.method = 'GET'
            return render_account(request, str(e))
Ejemplo n.º 13
0
def render_basket(request):
    if not AuthManager.is_logged_in(request):
        return redirect("/login")
    user = AuthManager.get_current_user(request)
    context = Context({"user": user})
    if 'items' in request.session.keys():
        total_cost = 0
        for item in request.session['items']:
            userbook = lib.USERBOOK.get(item)
            total_cost += userbook.price
        tmpl =  os.path.join(os.path.dirname(__file__), 'template', 'basket.html')
    else:
        tmpl =  os.path.join(os.path.dirname(__file__), 'template', 'emptybasket.html')
    response = HttpResponse()
    response.write(render_to_string(request, tmpl, context))
    return response
Ejemplo n.º 14
0
def render_basket(request):
    if not AuthManager.is_logged_in(request):
        return redirect("/login")
    user = AuthManager.get_current_user(request)
    context = Context({"user": user})
    if 'items' in request.session.keys():
        total_cost = 0
        for item in request.session['items']:
            userbook = lib.USERBOOK.get(item)
            total_cost += userbook.price
        tmpl = os.path.join(os.path.dirname(__file__), 'template',
                            'basket.html')
    else:
        tmpl = os.path.join(os.path.dirname(__file__), 'template',
                            'emptybasket.html')
    response = HttpResponse()
    response.write(render_to_string(request, tmpl, context))
    return response
Ejemplo n.º 15
0
def render_basket_remove(request, listing_id):
    if not AuthManager.is_logged_in(request):
        return redirect("/login")
    if "items" in request.session.keys() and listing_id in request.session["items"]:
        request.session["items"].remove(listing_id)
    return redirect(render_basket)