diff options
| -rw-r--r-- | apioforum/forum.py | 33 | ||||
| -rw-r--r-- | apioforum/templates/create_thread.html | 14 | ||||
| -rw-r--r-- | apioforum/templates/view_forum.html | 1 | ||||
| -rw-r--r-- | apioforum/templates/view_thread.html | 6 | ||||
| -rw-r--r-- | apioforum/thread.py | 33 | 
5 files changed, 83 insertions, 4 deletions
| diff --git a/apioforum/forum.py b/apioforum/forum.py index c09b0e8..261da15 100644 --- a/apioforum/forum.py +++ b/apioforum/forum.py @@ -2,7 +2,8 @@  # currently there is only ever one forum however  from flask import ( -    Blueprint, render_template +    Blueprint, render_template, request, +    g, redirect, url_for  )  from .db import get_db @@ -13,3 +14,33 @@ def view_forum():      db = get_db()      threads = db.execute("SELECT * FROM threads ORDER BY updated DESC LIMIT 10;").fetchall()      return render_template("view_forum.html",threads=threads) + +@bp.route("/create_thread",methods=("GET","POST")) +def create_thread(): +    db = get_db() +    if request.method == "POST": +        title = request.form['title'] +        content = request.form['content'] +        err = None +        if g.user is None: +            err = "you need to be logged in to create a thread" +        elif len(title.strip()) == 0 or len(content.strip()) == 0: +            err = "title and content can't be empty" + +        if err is None: +            cur = db.cursor() +            cur.execute( +                "INSERT INTO threads (title,creator,created,updated) VALUES (?,?,current_timestamp,current_timestamp);", +                (title,g.user) +            ) +            thread_id = cur.lastrowid +            cur.execute( +                "INSERT INTO posts (thread,created,author,content) VALUES (?,current_timestamp,?,?);", +                (thread_id,g.user,content) +            ) +            db.commit() +            return redirect(url_for('thread.view_thread',thread_id=thread_id)) +         +         +    return render_template("create_thread.html") + diff --git a/apioforum/templates/create_thread.html b/apioforum/templates/create_thread.html new file mode 100644 index 0000000..b8d204c --- /dev/null +++ b/apioforum/templates/create_thread.html @@ -0,0 +1,14 @@ +{% extends 'base.html' %} +{% block header %} +<h1>{% block title %}create thread{% endblock %}</h1> +{% endblock %} + +{% block content %} +<form action="{{url_for('forum.create_thread')}}", method="POST"> +    <label for="title">thread title</label> +    <input name="title" id="title"> +    <label for="content">thread content</label> +    <textarea name="content" id="content"></textarea> +    <input type="submit" value="yes"> +</form> +{% endblock %} diff --git a/apioforum/templates/view_forum.html b/apioforum/templates/view_forum.html index 2140c7a..13e35a8 100644 --- a/apioforum/templates/view_forum.html +++ b/apioforum/templates/view_forum.html @@ -2,6 +2,7 @@  {% block header %}<h1>{% block title %}apio forum george etc{%endblock%}</h1>{%endblock%}  {%block content%}  <p>the</p> +<a href="{{url_for('forum.create_thread')}}">create thread</a>  <table>      <tr>          <th>id</th> diff --git a/apioforum/templates/view_thread.html b/apioforum/templates/view_thread.html index ebbcd3b..718df4e 100644 --- a/apioforum/templates/view_thread.html +++ b/apioforum/templates/view_thread.html @@ -9,7 +9,7 @@  <dl>  {% for post in posts %}  <dt>{{post.author}} {{post.created}}</dt> -<dd>{{post.content}}</dd> +<dd>{{rendered_posts[loop.index0] | safe}}<dd>  {% else %}  <dt>there weren't</dt>  <dd>any posts</dd> @@ -17,6 +17,10 @@  </dl>  {% if g.user %}  <p>loggedi n as {{ g.user }}</p> +<form action="{{url_for('thread.create_post',thread_id=thread_id)}}" method="POST"> +    <textarea name="content"></textarea> +    <input type="submit" value="yes"> +</form>  {% else %}  <p>not logged in</p>  {% endif %} diff --git a/apioforum/thread.py b/apioforum/thread.py index 2276e84..638ff7f 100644 --- a/apioforum/thread.py +++ b/apioforum/thread.py @@ -1,8 +1,11 @@  # view posts in thread  from flask import ( -    Blueprint, render_template, abort +    Blueprint, render_template, abort, request, g, redirect, +    url_for, flash  ) +from markdown import markdown +from markupsafe import escape  from .db import get_db  bp = Blueprint("thread", __name__, url_prefix="/thread") @@ -18,5 +21,31 @@ def view_thread(thread_id):              "SELECT * FROM posts WHERE thread = ? ORDER BY created ASC;",              (thread_id,)          ).fetchall() -        return render_template("view_thread.html",posts=posts,thread=thread) +        rendered_posts = [markdown(escape(q['content'])) for q in posts] +        return render_template("view_thread.html",posts=posts,thread=thread,thread_id=thread_id,rendered_posts=rendered_posts) +@bp.route("/<int:thread_id>/create_post", methods=("POST",)) +def create_post(thread_id): +    if g.user is None: +        flash("you need to log in before you can post") +        return redirect(url_for('thread.view_thread',thread_id=thread_id)) +    else: +        db = get_db() +        content = request.form['content'] +        thread = db.execute("SELECT * FROM threads WHERE id = ?;",(thread_id,)).fetchone() +        if len(content.strip()) == 0: +            flash("you cannot post an empty message") +        elif not thread: +            flash("that thread does not exist") +        else: +            db.execute( +                "INSERT INTO posts (thread,author,content,created) VALUES (?,?,?,current_timestamp);", +                (thread_id,g.user,content) +            ) +            db.execute( +                "UPDATE threads SET updated = current_timestamp WHERE id = ?;", +                (thread_id,) +            ) +            db.commit() +            flash("post posted postfully") +    return redirect(url_for('thread.view_thread',thread_id=thread_id)) | 
