def delete_friend(response, username): current_username = get_current_user(response) current = User.find(current_username) other = User.find(username) current.delete_friend(other) response.redirect(response.get_field('redirect'))
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 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 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 edit_user(response, username): try: current_user = User.find(username) except UserNotFound: handle_error(response, message="Unable to find the specified user.") return # supports image only for now, extend later for profile data changes filename, content_type, photo = response.get_file('profile-photo') accepted_image_formats = ['jpg', 'jpeg', 'JPEG', 'png', 'PNG', 'gif', 'GIF'] if not filename or not content_type or not photo: if 'default' not in current_user.image.split('.'): old_image = 'static/images/profiles/' + current_user.image try: os.remove(old_image) except: pass current_user.image = None current_user.save() response.redirect('/users/' + username) return extension = filename.split('.')[-1] if extension in accepted_image_formats: img = "{}.{}".format(current_user.user_id, extension) profile_img = 'static/images/profiles/' + img if 'default' not in current_user.image.split('.'): old_image = 'static/images/profiles/' + current_user.image os.remove(old_image) current_user.image = img current_user.save() with open(profile_img, mode='wb') as f: f.write(photo) response.redirect('/users/' + username)
def add_item(response, username): user_lists = User.find(username).get_wishlists() current_wishlist = user_lists[0] details = { "name": response.get_field('wish'), "image": response.get_field('image') or '/static/images/gift_box.png', "link": response.get_field('website'), "description": response.get_field('description'), "price": response.get_field('price') } encoded_string = "" if details['name'] != "": product = Product.create(**details) current_wishlist.add_item(product) else: encoded_string = urlencode({'error': "0"}) response.redirect('/users/' + username + "?" + encoded_string)
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 delete_item(response, username, item_id): user_lists = User.find(username).get_wishlists() wishlist = user_lists[0] wishlist.delete_item(item_id) response.redirect('/users/' + username)