def update_item_post(category_name, item_name): """Handle update item request.""" # Get the item to be updated. item = data.get_item(category_name, item_name) if not item: return "Not Found", 404 # Check if the item name has changed. old_name = item.name new_name = request.form["name"] if old_name != new_name: # Make sure the new name does not already exist in this category. items = data.get_items(category_name) if new_name in items: flash("Error: The item name '{0}' already exists " "in this category.".format(new_name)) return redirect(url_for('update_item', category_name = category_name, item_name = item_name)) # Update the in-memory cache about the item name change. data.change_item_name_in_cache(category_name, old_name, new_name) item.name = new_name # Update description and wiki url and update into database. item.description = request.form["description"] item.wiki_url = request.form["wiki"] item.last_modified = datetime.now() data.update_item(item) flash("The item has been updated.") return redirect(url_for('read_item', category_name = category_name, item_name = new_name))
def create_item_post(category_name): """Handle create item request.""" # Check the `category_name`. category = data.get_category(category_name) if not category: return "Category '%s' not found" % category_name, 404 # Make sure the item name does not exist in the same category. items = data.get_items(category_name) name = request.form["name"] if name in items: flash("Error: The item name '{0}' already exists" "in this category.".format(name)) return redirect(url_for('create_item', category_name=category_name)) # Create an `Item` object and add into database. item = Item(category = category, iid = None, name = name, description = request.form["description"], wiki_url = request.form["wiki"], last_modified = datetime.now()) data.add_item(item) flash("The item has been added.") return redirect(url_for('read_item', category_name = category_name, item_name = name))
def api_json_category(category_name): """Return a json object containing information of the requested category together with all items inside this category.""" category = data.get_category(category_name) if not category: return "Not Found", 404 items = data.get_items(category_name).values() return jsonify( category = category.to_json(), items = [i.to_json() for i in items] )
def read_category(category_name): """Render read category page.""" # Get the category. categories = data.get_categories() if category_name not in categories: return "Not Found", 404 category = categories[category_name] # Render the page. jumbotron = render_template("category-jumbo.html", category = category) abstracts = [render_template("item-abstract.html", item=i) for i in data.get_items(category_name).values()] category_links = [render_template("category-link.html", category=c, active=(c==category)) for c in categories.values()] return render_template("view.html", jumbotron = jumbotron, abstracts = abstracts, category_links = category_links)
def read_category(category_name): """Render read category page.""" # Get the category. categories = data.get_categories() if category_name not in categories: return "Not Found", 404 category = categories[category_name] # Render the page. jumbotron = render_template("category-jumbo.html", category=category) abstracts = [ render_template("item-abstract.html", item=i) for i in data.get_items(category_name).values() ] category_links = [ render_template("category-link.html", category=c, active=(c == category)) for c in categories.values() ] return render_template("view.html", jumbotron=jumbotron, abstracts=abstracts, category_links=category_links)
def read_item(category_name, item_name): """Render read item page.""" # Get the category. categories = data.get_categories() if category_name not in categories: return "Not Found", 404 category = categories[category_name] # Get the item. items = data.get_items(category_name) if item_name not in items: return "Not Found", 404 item = items[item_name] # Render the page. jumbotron = render_template("item-jumbo.html", item = item) category_links = [render_template("category-link.html", category=c, active=(c==category)) for c in categories.values()] return render_template("view.html", jumbotron = jumbotron, abstracts = [], category_links = category_links)
def read_item(category_name, item_name): """Render read item page.""" # Get the category. categories = data.get_categories() if category_name not in categories: return "Not Found", 404 category = categories[category_name] # Get the item. items = data.get_items(category_name) if item_name not in items: return "Not Found", 404 item = items[item_name] # Render the page. jumbotron = render_template("item-jumbo.html", item=item) category_links = [ render_template("category-link.html", category=c, active=(c == category)) for c in categories.values() ] return render_template("view.html", jumbotron=jumbotron, abstracts=[], category_links=category_links)