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
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))
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
def render_sentbox(request): user = AuthManager.get_current_user(request) context = Context({"user_messages": lib.USER.list_sent_messages(user.email), "user": AuthManager.get_current_user(request)}) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'sentbox.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
def render_sentbox(request): user = AuthManager.get_current_user(request) context = Context({ "user_messages": lib.USER.list_sent_messages(user.email), "user": AuthManager.get_current_user(request) }) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'sentbox.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
def render_message(request, to_user, error = None): if request.method == 'POST': return send_message(request, to_user, request.POST['message'], request.POST['subject']) else: context = Context({ "send_user": lib.USER.get(to_user), "user": AuthManager.get_current_user(request) }) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'message.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
def render_to_string(request, template, context = None): if context == None: context = Context() if not "items" in request.session.keys(): request.session["items"] = list() context["user"] = AuthManager.get_current_user(request) context["basket"] = list(); for item in request.session['items']: userbook = lib.USERBOOK.get(item) context["basket"].append(userbook) return loader.render_to_string(template, context)
def render_user(request, user_key): user = lib.USER.get(user_key) user_listings = lib.USER.list_books(user.email) context = Context({ "user_listings": user_listings, "viewing_user": user, "user": AuthManager.get_current_user(request) }) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'user.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
def render_to_string(request, template, context=None): if context == None: context = Context() if not "items" in request.session.keys(): request.session["items"] = list() context["user"] = AuthManager.get_current_user(request) context["basket"] = list() for item in request.session['items']: userbook = lib.USERBOOK.get(item) context["basket"].append(userbook) return loader.render_to_string(template, context)
def render_book(request, book_isbn): "Page to show the details of a single book" copies = lib.BOOK.list_book_copies(book_isbn) context = Context({ "user": AuthManager.get_current_user(request), "book": lib.BOOK.get_by_key_name(book_isbn), "book_listings": copies }) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'book.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
def render_user(request, user_key): user = lib.USER.get(user_key) user_listings = lib.USER.list_books(user.email) context = Context({ "user_listings": user_listings, "viewing_user": user, "user": AuthManager.get_current_user(request) }) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'user.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
def render_book(request, book_isbn): "Page to show the details of a single book" copies = lib.BOOK.list_book_copies(book_isbn) context = Context({ "user": AuthManager.get_current_user(request), "book": lib.BOOK.get_by_key_name(book_isbn), "book_listings": copies }) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'book.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
def send_message(request, to_user, message, subject): send_to_user = lib.USER.get(to_user) send_from_user = AuthManager.get_current_user(request) try: lib.MESSAGE.create_message(send_from_user.email,send_to_user.email, subject, message) context = Context({}) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'send_message_success.html') except Exception as e: context = Context({"error": str(e)}) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'send_message_failure.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
def render_message(request, to_user, error=None): if request.method == 'POST': return send_message(request, to_user, request.POST['message'], request.POST['subject']) else: context = Context({ "send_user": lib.USER.get(to_user), "user": AuthManager.get_current_user(request) }) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'message.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
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))
def render_create_book(request): "Show the create book form" # Check permissions if not AuthManager.has_permission(request, 'create_book'): raise PermissionDenied # Handle the request if we're allowed to if request.method == 'POST': return create_book_action(request) else: user = AuthManager.get_current_user(request) context = Context({"user":user}) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'create_book.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
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
def list_book_action(request): # Are we using a template or a new book? if not "template_isbn" in request.POST.keys(): # Create new book create_book_action(request) isbn = cgi.escape(request.POST["isbn"]) elif request.POST["template_isbn"]: isbn = cgi.escape(request.POST["template_isbn"]) else: return render_create_listing( request, "Please select a book from the drop-down or enter details for a new book." ) book = lib.BOOK.get_by_key_name(isbn) user = AuthManager.get_current_user(request) condition = int(request.POST['condition']) #Convert condition from ints into the appropriate strings if condition == 1: condition = "New" if condition == 2: condition = "As New" if condition == 3: condition = "Used" if condition == 4: condition = "Damaged" price = float(cgi.escape(request.POST['price'])) price = int(price * 100) #convert P.pp to interger pence try: lib.USERBOOK(key_name=None, user=user, book=book, price=price, condition=condition, listed_stamp=int(time.time()), sold_stamp=0, sold_to_user=None).put() tmpl = os.path.join(os.path.dirname(__file__), 'template', 'list_book_success.html') context = Context() except Exception as e: context = Context({"error": e}) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'list_book_failure.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
def render_listing(request, listing_id): "Page to show a listing, as well as other listings of the same book" listing = lib.USERBOOK.get(listing_id) book = listing.book seller = listing.user copies = lib.BOOK.list_book_copies(book.isbn) context = Context({ "book":book, "seller":seller, "current_book":listing, "same_books":copies, "user": AuthManager.get_current_user(request) }) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'listing.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
def render_create_book(request): "Show the create book form" # Check permissions if not AuthManager.has_permission(request, 'create_book'): raise PermissionDenied # Handle the request if we're allowed to if request.method == 'POST': return create_book_action(request) else: user = AuthManager.get_current_user(request) context = Context({"user": user}) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'create_book.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
def render_listing(request, listing_id): "Page to show a listing, as well as other listings of the same book" listing = lib.USERBOOK.get(listing_id) book = listing.book seller = listing.user copies = lib.BOOK.list_book_copies(book.isbn) context = Context({ "book": book, "seller": seller, "current_book": listing, "same_books": copies, "user": AuthManager.get_current_user(request) }) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'listing.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
def send_message(request, to_user, message, subject): send_to_user = lib.USER.get(to_user) send_from_user = AuthManager.get_current_user(request) try: lib.MESSAGE.create_message(send_from_user.email, send_to_user.email, subject, message) context = Context({}) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'send_message_success.html') except Exception as e: context = Context({"error": str(e)}) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'send_message_failure.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
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
def list_book_action(request): # Are we using a template or a new book? if not "template_isbn" in request.POST.keys(): # Create new book create_book_action(request) isbn = cgi.escape(request.POST["isbn"]) elif request.POST["template_isbn"]: isbn = cgi.escape(request.POST["template_isbn"]) else: return render_create_listing(request, "Please select a book from the drop-down or enter details for a new book.") book = lib.BOOK.get_by_key_name(isbn) user = AuthManager.get_current_user(request) condition = int(request.POST['condition']) #Convert condition from ints into the appropriate strings if condition == 1: condition = "New" if condition == 2: condition = "As New" if condition == 3: condition = "Used" if condition == 4: condition = "Damaged" price = float(cgi.escape(request.POST['price'])) price = int(price * 100) #convert P.pp to interger pence try: lib.USERBOOK(key_name = None, user = user, book = book, price = price, condition = condition, listed_stamp = int(time.time()), sold_stamp = 0, sold_to_user = None ).put() tmpl = os.path.join(os.path.dirname(__file__), 'template', 'list_book_success.html') context = Context() except Exception as e: context = Context({"error": e}) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'list_book_failure.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
def render_create_listing(request, error = None): "Show the create book form" # Check permissions if not AuthManager.has_permission(request, 'list_book'): raise PermissionDenied # Handle the request if we're allowed to if request.method == 'POST' and error is None: return list_book_action(request) else: user = AuthManager.get_current_user(request) context = Context({ "error": error, "user": user, "books": lib.BOOK.list_all_books() }) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'list_book.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
def render_create_listing(request, error=None): "Show the create book form" # Check permissions if not AuthManager.has_permission(request, 'list_book'): raise PermissionDenied # Handle the request if we're allowed to if request.method == 'POST' and error is None: return list_book_action(request) else: user = AuthManager.get_current_user(request) context = Context({ "error": error, "user": user, "books": lib.BOOK.list_all_books() }) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'list_book.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response