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
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_register_action(request): try: email = cgi.escape(request.POST['email']) email2 = cgi.escape(request.POST['email2']) firstname = cgi.escape(request.POST['firstname']) lastname = cgi.escape(request.POST['lastname']) year = int(cgi.escape(request.POST['year'])) # Check their email addresses match if email != email2: raise EmailDoesntMatchError() # Check that the email isn't already registered if lib.USER.get_by_key_name(request.POST['email']): raise AlreadyRegisteredError() # Do the creation lib.USER.create_user(email, firstname, lastname, year) new_password = lib.USER.reset_password(email) # Send the success email context = Context({ "firstname": firstname, "lastname": lastname, "email": email, "password": new_password }) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'registersuccess.eml') message = mail.EmailMessage( sender="TexTrader Support <*****@*****.**>", subject="TexTrader: Your account details") message.to = "%s %s <%s>" % (firstname, lastname, email) message.body = render_to_string(request, tmpl, context) message.send() tmpl = os.path.join(os.path.dirname(__file__), 'template', 'registersuccess.html') response = HttpResponse() response.write(render_to_string(request, tmpl)) return response except InvalidUserError as e: return render_register_form(request, str(e)) except EmailDoesntMatchError as e: return render_register_form(request, str(e)) except AlreadyRegisteredError as e: return render_register_form(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_home(request): context = Context() context["book_list"] = lib.USERBOOK.get_recent_listings() tmpl = os.path.join(os.path.dirname(__file__), 'template', 'home.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
def do_advanced_search(request): matched_books = [] #Stores all the books that match the query #Get all the entered details title = cgi.escape(request.POST['title'].strip()) isbn = cgi.escape(request.POST['isbn'].strip()) author = cgi.escape(request.POST['author'].strip()) all_books = lib.BOOK.list_all_books() #Grab all the books from the database if title: for book in all_books: if title.lower() in book.title.lower(): matched_books.append(book) if isbn: for book in all_books: if isbn.lower() in book.isbn.lower(): matched_books.append(book) if author: for book in all_books: if author.lower() in book.author.lower(): matched_books.append(book) context = Context({ "book_list": matched_books }) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'search.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
def render_register_form(request, error=None): tmpl = os.path.join(os.path.dirname(__file__), 'template', 'registerform.html') context = Context({"error": error}) response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
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 create_book_action(request): isbn = cgi.escape(request.POST['isbn']) title = cgi.escape(request.POST['title']) author = cgi.escape(request.POST['author']) year = int(cgi.escape(request.POST['year'])) edition = cgi.escape(request.POST['edition']) publisher = cgi.escape(request.POST['publisher']) rrp = float(cgi.escape(request.POST['rrp'])) picture = cgi.escape(request.POST['picture']) rrp = int(rrp * 100) #convert P.pp to interger pence context = Context() try: lib.BOOK.create_book(isbn, title, author, year, edition, publisher, rrp, picture) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'create_book_success.html') except Exception as e: context = Context({"error": e}) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'create_book_failure.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
def render_advanced_search(request): if request.method == 'POST': return do_advanced_search(request) else: tmpl = os.path.join(os.path.dirname(__file__), 'template', 'advanced.html') response = HttpResponse() response.write(render_to_string(request, tmpl)) 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_register_action(request): try: email = cgi.escape(request.POST['email']) email2 = cgi.escape(request.POST['email2']) firstname = cgi.escape(request.POST['firstname']) lastname = cgi.escape(request.POST['lastname']) year = int(cgi.escape(request.POST['year'])) # Check their email addresses match if email != email2: raise EmailDoesntMatchError() # Check that the email isn't already registered if lib.USER.get_by_key_name(request.POST['email']): raise AlreadyRegisteredError() # Do the creation lib.USER.create_user(email, firstname, lastname, year) new_password = lib.USER.reset_password(email) # Send the success email context = Context({ "firstname": firstname, "lastname" : lastname, "email": email, "password": new_password }) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'registersuccess.eml') message = mail.EmailMessage(sender = "TexTrader Support <*****@*****.**>", subject = "TexTrader: Your account details") message.to = "%s %s <%s>" % (firstname, lastname, email) message.body = render_to_string(request, tmpl, context) message.send() tmpl = os.path.join(os.path.dirname(__file__), 'template', 'registersuccess.html') response = HttpResponse() response.write(render_to_string(request, tmpl)) return response except InvalidUserError as e: return render_register_form(request, str(e)) except EmailDoesntMatchError as e: return render_register_form(request, str(e)) except AlreadyRegisteredError as e: return render_register_form(request, str(e))
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_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_forgotpw_action(request): try: email = cgi.escape(request.POST['email']) lastname = cgi.escape(request.POST['lastname']) # Check their email addresses match user = lib.USER.get_by_key_name(email) if user == None: raise InvalidUserError() if user.lastName != lastname: raise InvalidUserError() # Do the creation new_password = lib.USER.reset_password(email) # Send the success email context = Context({ "firstname": user.firstName, "lastname": user.lastName, "email": email, "password": new_password }) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'forgotpw.eml') message = mail.EmailMessage( sender="TexTrader Support <*****@*****.**>", subject="TexTrader: Password reset") message.to = "%s %s <%s>" % (user.firstName, user.lastName, user.email) message.body = render_to_string(request, tmpl, context) message.send() tmpl = os.path.join(os.path.dirname(__file__), 'template', 'pwresetsuccess.html') response = HttpResponse() response.write(render_to_string(request, tmpl)) return response except InvalidUserError as e: return render_register_form(request, str(e)) except EmailDoesntMatchError as e: return render_register_form(request, str(e))
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_search(request): search_string = request.GET['s'] matched_books = [] all_books = lib.BOOK.list_all_books() #Grab all the books from the database for book in all_books: if search_string.lower() in book.title.lower(): matched_books.append(book) context = Context({ "book_list": matched_books }) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'search.html') response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response
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")
def render_forgotpw_action(request): try: email = cgi.escape(request.POST['email']) lastname = cgi.escape(request.POST['lastname']) # Check their email addresses match user = lib.USER.get_by_key_name(email) if user == None: raise InvalidUserError() if user.lastName != lastname: raise InvalidUserError() # Do the creation new_password = lib.USER.reset_password(email) # Send the success email context = Context({ "firstname": user.firstName, "lastname" : user.lastName, "email": email, "password": new_password }) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'forgotpw.eml') message = mail.EmailMessage(sender = "TexTrader Support <*****@*****.**>", subject = "TexTrader: Password reset") message.to = "%s %s <%s>" % (user.firstName, user.lastName, user.email) message.body = render_to_string(request, tmpl, context) message.send() tmpl = os.path.join(os.path.dirname(__file__), 'template', 'pwresetsuccess.html') response = HttpResponse() response.write(render_to_string(request, tmpl)) return response except InvalidUserError as e: return render_register_form(request, str(e)) except EmailDoesntMatchError as e: return render_register_form(request, str(e))
def render_login_form(request, error=None): """ Handle display of the login page """ origin = None if "from" in request.GET.keys(): origin = request.GET["from"] if "from" in request.POST.keys(): origin = request.POST["from"] context = Context({"origin": origin, "error": error}) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'login.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_login_form(request, error = None): """ Handle display of the login page """ origin = None if "from" in request.GET.keys(): origin = request.GET["from"] if "from" in request.POST.keys(): origin = request.POST["from"] context = Context({"origin": origin, "error": error}) tmpl = os.path.join(os.path.dirname(__file__), 'template', 'login.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 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_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 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 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
def page_not_found(request): tmpl = os.path.join(os.path.dirname(__file__), 'template', '404.html') response = HttpResponse() response.write(render_to_string(request, tmpl)) return response
def server_error(request): tmpl = os.path.join(os.path.dirname(__file__), 'template', '500.html') response = HttpResponse() response.write(render_to_string(request, tmpl)) return response
def render_register_form(request, error = None): tmpl = os.path.join(os.path.dirname(__file__), 'template', 'registerform.html') context = Context({"error": error}) response = HttpResponse() response.write(render_to_string(request, tmpl, context)) return response