def login(response): uid = response.get_field("uid") pwd = response.get_field("pwd", strip=False) userid = response.get_secure_cookie('userid') # already logged in! if userid: response.redirect('/users/' + userid.decode('utf-8')) return elif uid is None or pwd is None: scope = {"logged_in": get_current_user(response)} response.write(epyc.render("templates/login.html", scope)) return errors = [] if not uid or not pwd: errors.append("Empty username or password") elif not User.check_password(uid, pwd): errors.append("Incorrect username or password") if errors: scope = { "errors": errors, "logged_in": get_current_user(response) } response.write(epyc.render("templates/login.html", scope)) else: response.set_secure_cookie('userid', uid) response.redirect('/users/%s' % uid)
def handle_error(response, message=""): scope = { "logged_in": get_current_user(response), "message": message } response.write(epyc.render("templates/404.html", scope))
def feed(response): response.write(epyc.render("templates/feed.html", { "productimage": "http://placekitten/150/150", "productname": "Green Socks", "firstname": "Marie", "lastname": "Atzarakis", "dob": "31st August" }))
def friends_list(response): current_username = get_current_user(response) current_user = User.find(current_username) friends_list = current_user.find_friends() scope = {'friends': friends_list, 'logged_in': current_username} response.write(epyc.render("templates/friends.html", scope))
def signup(response): fname = response.get_field("fname") lname = response.get_field("lname") email = response.get_field("email") username = response.get_field("username") password = response.get_field("password") # check for invalid input errors = [] if not fname: errors.append("First name required") if not lname: errors.append("Last name required") if not re.match(r"^[-a-zA-Z0-9+\._]+@([-a-zA-Z0-9]+\.)+[a-zA-Z]+$", email): errors.append("Valid email address required") if not re.match(r"^[a-zA-Z0-9_]+$", username): errors.append("Username can only contain letters, numbers or underscores") if len(password) < 6: errors.append("Password must be longer than 5 characters") if not errors: try: User.find(username) except UserNotFound: pass else: errors.append("Username is taken") if errors: scope = { "errors": errors, "logged_in": get_current_user(response), "fname": fname, "lname": lname, "email": email, "username": username } response.write(epyc.render("templates/login.html", scope)) else: user = User.create(fname, lname, username, email, password) response.set_secure_cookie('userid', user.username) listname = "{}'s wishlist".format(user.username) Wishlist.create(listname, user) response.redirect('/users/' + user.username)
def search(response): search = response.get_field("q") logged_in = get_current_user(response) types = { "people": 0, "items": 1 } tp = types.get(response.get_field("t"), 0) if search: if tp == types['people']: items = User.search(search) else: items = Product.search(search) scope = { "query": search, "results": items, "tp": tp, "types": types, "logged_in": get_current_user(response) } app_log.info("[%s found for '%s'] %s" % (response.get_field('t'), search, items)) response.write(epyc.render("templates/search.html", scope)) else: scope = { "query": "", "results": [], "tp": tp, "types": types, "logged_in": get_current_user(response) } response.write(epyc.render("templates/search.html", scope))
def profile(response, username): logged_in = get_current_user(response) try: current_user = User.find(username) except UserNotFound: handle_error(response, message="Unable to find the specified user.") return user_lists = current_user.get_wishlists() if not user_lists: wishlist_name = "{}'s wishlist".format(username) Wishlist.create(wishlist_name, current_user) user_lists = current_user.get_wishlists() current_wishlist = user_lists[0] products = current_wishlist.get_items() for product in products: product.price = format_price(product.price) error_code = response.get_field('error') errors = [] if error_code == '0': errors.append("Wish name cannot be empty") scope = { "username": username, "products": products, "listname": current_wishlist.list_name, "logged_in": logged_in, "current_user_fullname": display_name(current_user), "is_current_users_wishlist_page": is_current_users_wishlist_page(response, username), "response": response, "errors": errors, "profile_image_filename": '/static/images/profiles/%s' % current_user.image } if logged_in: logged_in_user = User.find(logged_in) scope["mutual_friend"] = logged_in_user.check_friend(current_user) scope["pending_friend_request"] = logged_in_user.check_pending_friend(current_user) scope["pending_friend_invite"] = current_user.check_pending_friend(logged_in_user) response.write(epyc.render("templates/wishlist.html", scope))
def edit_item(response, username, item_id): try: current_user = User.find(username) except UserNotFound: handle_error(response, message="Unable to find the specified user.") return if response.request.method == "POST": product = Product.find(item_id) product.name = response.get_field('wish') product.image = response.get_field('image') or '/static/images/gift_box.png' product.link = response.get_field('website') or None product.description = response.get_field('description') or None product.price = response.get_field('price') or None product.save() response.redirect("/users/" + username) return user_lists = current_user.get_wishlists() if not user_lists: handle_error(response, message="Unable to find the given user's wishlist.") return current_wishlist = user_lists[0] scope = { "username": username, "listname": current_wishlist.list_name, "logged_in": current_user, "current_user_fullname": display_name(current_user), "is_current_users_wishlist_page": is_current_users_wishlist_page(response, username), "product": Product.find(item_id) } response.write(epyc.render("templates/edit_item.html", scope))
def render(self, context=None, path="."): "Return rendered content from file at path" return epyc.render(path + "/" + self.path, context)
def home(response): scope = {"logged_in": get_current_user(response)} response.write(epyc.render("templates/home.html", scope))
#!/usr/bin/env python3 import sys import epyc if __name__ == "__main__": if len(sys.argv) > 1: print(epyc.render(sys.argv[1])) else: string = "\n".join(iter(input, "")) print(epyc._render(string, {"a": [1, 2, 3]})) # Node Testing #import render #print(render.ForNode("a", "[1, 2, 3]", render.GroupNode([render.TextNode("hello, a:"), render.ExprNode("a"), render.TextNode("!\n")])).render({})) #print(render.IfNode("x > 10", render.TextNode("Statement is TRUE"), render.TextNode("Statement is FALSE")).render({'x':5})) #print(render.ForNode("a", "range(100)", render.GroupNode([render.IfNode("a % 3 == 0", render.TextNode("Fizz"), render.TextNode("Buzz"))])).render())
def render(self, scope={}, path="."): "Return rendered content from file at path" return epyc.render(path + "/" + self.path, scope)