Beispiel #1
0
def write():
    """Allows logged in user to author a new blog entry."""

    entry_date = datetime.now().strftime("%Y-%m-%d %H:%M")

    if request.method == "POST":
        user_id = g.user.id
        new_entry = Entry(
            user_id,
            title=request.form["title"],
            text=request.form["text"],
            category_id=request.form["category_id"],
            entry_type=request.form["entry_type"],
            type_meta=request.form["type_meta"],
        )
        try:
            if request.form["published"]:
                new_entry.publish()
        except KeyError:
            new_entry.published = False

        if new_entry.create():
            flash("New entry successfully posted")
        else:
            flash("Failed to create new entry")
        return redirect(url_for(".show"))

    entry = {"date": entry_date}
    categories = Category.query.all()
    users = User.query.order_by(User.name.desc()).all()
    entry_types = EntryType.all()
    return render_template("entry/write.html", entry=entry, categories=categories, users=users, entry_types=entry_types)
Beispiel #2
0
def edit(entry_id):
    if request.method == "POST":
        entry = Entry.query.filter(Entry.id == request.form["entry_id"]).first()
        if not entry:
            flash("Entry not found, so cannot edit.")
            redirect(url_for(".manage"))
        entry.user_id = g.user.id
        entry.title = request.form["title"]
        entry.text = request.form["text"]
        entry.category_id = request.form["category_id"]
        entry.user_id = request.form["user_id"]
        entry.entry_type = request.form["entry_type"]
        entry.type_meta = request.form["type_meta"]

        try:
            if request.form["published"]:
                entry.publish()
        except KeyError:
            entry.published = False

        if entry.save():
            flash("Entry successfully updated")
        else:
            flash("There was an error trying to update that entry.")

        return redirect(url_for(".manage"))

    categories = Category.query.all()
    entry = Entry.query.filter(Entry.id == entry_id).first()
    users = User.query.order_by(User.name.desc()).all()
    entry_types = EntryType.all()
    if not Entry:
        flash("Entry not found, so cannot edit.")
        redirect(url_for(".manage"))

    return render_template("entry/edit.html", entry=entry, categories=categories, users=users, entry_types=entry_types)