예제 #1
0
def category_page_edit(category_slug):
    category = Category.query.filter_by(slug=category_slug).first()
    categories = db_tools.get_categories()
    features = db_tools.get_featured_items()
    new_category = Category()
    if request.method == "POST":
        if request.form["name"]:
            new_category.name = request.form["name"]
        if request.form["slug"]:
            new_category.slug = request.form["slug"]
        if not app_tools.valid_slug(new_category.slug):
            flash("Slug must be only lowercase characters or hyphens (-).")
            return render_template(
                "category/category_edit.html", category=new_category, categories=categories, featured_items=features
            )
        category.name = new_category.name
        category.slug = new_category.slug
        try:
            db.session.add(category)
            db.session.commit()
            flash("Category %s updated." % category.name)
            return redirect(url_for("category.category_page", category_slug=category.slug))
        except sqlalchemy_exceptions.IntegrityError as error:
            db.session.rollback()
            print(error)
            flash("The category and slug must be unique.")
            return render_template(
                "category/category_edit.html", category=new_category, categories=categories, featured_items=features
            )
    else:
        return render_template(
            "category/category_edit.html", category=category, categories=categories, featured_items=features
        )
예제 #2
0
def show_catalog():
    categories = db_tools.get_categories()
    latest_items = Item.query.order_by(db.desc(Item.created_on)).\
        limit(10).all()
    featured_items = db_tools.get_featured_items()
    return render_template('catalog/home.html',
                           categories=categories,
                           latest_items=latest_items,
                           featured_items=featured_items)
예제 #3
0
def category_page_delete(category_slug):
    category = Category.query.filter_by(slug=category_slug).first()
    categories = db_tools.get_categories()
    features = db_tools.get_featured_items()
    if request.method == "POST":
        db.session.delete(category)
        db.session.commit()
        flash("Category %s deleted." % category.name)
        return redirect(url_for("catalog.show_catalog"))
    else:
        return render_template(
            "category/category_delete.html", category=category, categories=categories, featured_items=features
        )
예제 #4
0
def category_page(category_slug):
    categories = db_tools.get_categories()
    category = Category.query.filter_by(slug=category_slug).first()
    items = Item.query.join(Category).filter(Category.slug == category_slug).order_by(db.asc(Item.name)).all()
    item_count = len(items)
    featured_items = db_tools.get_featured_items()
    return render_template(
        "category/category.html",
        items=items,
        item_count=item_count,
        categories=categories,
        category=category,
        featured_items=featured_items,
        page_title=category.name,
    )
예제 #5
0
def category_page_new():
    categories = db_tools.get_categories()
    features = db_tools.get_featured_items()
    if request.method == "POST":
        new_category = Category(name=request.form["name"], slug=request.form["slug"])
        if not app_tools.valid_slug(new_category.slug):
            flash("Slug must be only lowercase characters or hyphens (-).")
            return render_template(
                "category/category_new.html", category=new_category, categories=categories, featured_items=features
            )
        try:
            db.session.add(new_category)
            db.session.commit()
            flash("Category %s created successfully." % new_category.name)
            return redirect(url_for(".category_page", category_slug=new_category.slug))
        except sqlalchemy_exceptions.IntegrityError as error:
            print(error)
            db.session.rollback()
            flash("The category and slug must be unique.")
            return render_template("category/category_new.html", categories=categories, featured_items=features)
    else:
        return render_template("category/category_new.html", categories=categories, featured_items=features)
예제 #6
0
def show_features():
    categories = db_tools.get_categories()
    featured_items = db_tools.get_featured_items()
    return render_template('catalog/features.html',
                           categories=categories,
                           featured_items=featured_items)