Ejemplo n.º 1
0
def update(id):
    """Update a post if the current user is the author."""
    post = get_post(id)

    if request.method == "POST":
        title = request.form["title"]
        body = request.form["body"]
        error = None

        if not title:
            error = "Title is required."

        if error is not None:
            flash(error)
        else:
            # stmt = update(Post).where(Post.id == id).values(title=title, body=body)
            # db_session.execute(stmt)

            post.title = title
            post.body = body
            db.flush()
            db.commit()

            # stmt = Post.update().where(Post.id == id).values(title=title, body=body)
            # db.execute(stmt)
            #
            # stmt = update(Post).where(Post.id == id).values(title=title, body=body)
            # db.execute(stmt)
            return redirect(url_for("blog.index"))

    return render_template("blog/update.html", post=post)
Ejemplo n.º 2
0
def register():
    """Register a new user.

    Validates that the username is not already taken. Hashes the
    password for security.
    """
    if request.method == "POST":
        username = request.form["username"]
        password = request.form["password"]
        error = None

        if not username:
            error = "Username is required."
        elif not password:
            error = "Password is required."
        elif (
                db.query(User).filter(User.name == username).first()
                is not None
        ):
            error = "User {0} is already registered.".format(username)

        if error is None:
            # the name is available, store it in the database and go to
            # the login page
            db.add(User(username, password=generate_password_hash(password)))
            db.commit()
            return redirect(url_for("auth.login"))

        flash(error)

    return render_template("auth/register.html")
Ejemplo n.º 3
0
def delete(id):
    """Delete a post.

    Ensures that the post exists and that the logged in user is the
    author of the post.
    """
    post = get_post(id)
    db.delete(post)
    db.commit()
    return redirect(url_for("blog.index"))
Ejemplo n.º 4
0
def create():
    """Create a new post for the current user."""
    if request.method == "POST":
        title = request.form["title"]
        body = request.form["body"]
        error = None

        if not title:
            error = "Title is required."

        if error is not None:
            flash(error)
        else:

            post = Post(title, body, g.user.id)
            db.add(post)
            db.commit()
            return redirect(url_for("blog.index"))

    return render_template("blog/create.html")
Ejemplo n.º 5
0
import click
from flask.cli import with_appcontext
from flask_test.orms import init_db, db


@click.command("init-db")
@with_appcontext
def init_db_command():
    """Clear existing data and create new tables."""
    init_db()
    click.echo("Initialized the database.")


def init_app(app):
    """Register database functions with the Flask app. This is called by
    the application factory.
    """
    # app.teardown_appcontext(close_db)
    app.cli.add_command(init_db_command)


if __name__ == "__main__":
    init_db()
    from flask_test.orms import User

    user = User("test01", "test")
    db.add(user)
    db.commit()