Ejemplo n.º 1
0
def create_topic():
    form = TopicForm(request.form)

    form.tags.choices = [form.tags.data]

    if not form.validate():
        return render_template("topics/new.html", form=form)

    subject = form.subject.data
    body = form.body.data
    author_id = current_user.id

    initial_post = Post(body)
    initial_post.author_id = author_id
    topic = Topic(subject, author_id)
    topic.posts.append(initial_post)

    tag_names = form.tags.data[0].split(",")
    for tag_name in tag_names:
        tag = Tag.query.filter_by(name=tag_name).first()
        if not tag:
            tag = Tag(tag_name)
        topic.tags.append(tag)

    db.session().add(topic)
    db.session().commit()

    return redirect(url_for("topics_index"))
Ejemplo n.º 2
0
def posts_create(thread_id):
  thread = Thread.query.get_or_404(thread_id)

  form = PostForm(request.form)

  if not form.validate():
    return render_template("posts/new.html",
      form = form,
      thread_id = thread_id,
      title = thread.title
    )

  try:
    thread.modification_time = db.func.current_timestamp()

    posted = Post(form.content.data)
    posted.account_id = current_user.id
    posted.thread_id = thread_id

    db.session().add(posted)
    db.session().commit()
    flash("Your comment was posted", "alert alert-info")
  except:
    db.session.rollback()
    flash("Error occurred, comment was not posted", "alert alert-danger")

  return redirect(url_for("posts_thread", thread_id=thread_id))
Ejemplo n.º 3
0
def threads_create():
    form = ThreadForm(request.form)

    if not form.validate():
        return render_template("threads/new_thread.html", form=form)

    try:
        # luodaan uusi lanka ja sille ID
        thread = Thread(form.title.data)
        thread.owner_id = current_user.id
        db.session.add(thread)
        db.session.flush()
        db.session.refresh(thread)

        # luodaan uusi postaus
        posted = Post(form.content.data)
        posted.account_id = current_user.id
        posted.thread_id = thread.id
        db.session().add(posted)
        db.session().commit()

        flash("Your new post was saved", "alert alert-info")
        return redirect(url_for("posts_index"))
    except:
        db.session.rollback()
        flash("Error occurred, could not save post", "alert alert-danger")
        return render_template("threads/new_thread.html", form=form)
Ejemplo n.º 4
0
def posts_create():
    form = PostForm(request.form)

    if (form.add_tag.data):
        form.tags.append(form.tag.data)
        form.tag.data = ''
        return render_template("posts/new.html", form=form)

    if not form.validate():
        return render_template("posts/new.html", form=form)

    p = Post(form.name.data, form.content.data)
    p.user_id = current_user.id

    db.session().add(p)
    db.session().commit()

    for tag in form.tags:
        tagd = Tag.query.filter_by(name=tag).first()
        if not tagd:
            t = Tag(tag)
            db.session().add(t)
            db.session().commit()
            pt = PostTag(t.id, p.id)
        else:
            pt = PostTag(tagd.id, p.id)

        db.session().add(pt)
        db.session().commit()

    PostForm.tags = []

    return redirect(url_for("posts_index"))
Ejemplo n.º 5
0
def posts_index():
  page = request.args.get("page", 1, type=int)
  # allow only positive values
  page = page if page >= 1 else 1
  posts = Post.list_posts_ordered_by("upvote", page)
  next_page_url = url_for("posts_index", page=page+1) if Post.has_next(page) else None
  start_index = config.POSTS_PER_PAGE * (page - 1)
  return render_template("posts/list.html",
    posts=posts, next_page_url=next_page_url, start_index=start_index)
Ejemplo n.º 6
0
def post_id(topic_id):

    topic = Topic.query.filter_by(id=topic_id).first()
    return render_template(
        "posts/single.html",
        posts=Post.find_matching_topic_for_post(topic_id),
        subject=topic,
        form=ReplyForm(),
        user_id=current_user.id,
        volume=Post.count_number_of_posts_by_topic(topic_id))
Ejemplo n.º 7
0
def user_wall(id):
    user = User.query.get(id)
    subscriber_count = Subscription.query.filter_by(wall_id=user.wall.id).count()
    subscription_count = Subscription.query.filter_by(owner_id=user.id).count()
    post_count = Post.query.filter_by(owner_id=user.id).count()
    comment_count = Comment.query.filter_by(owner_id=user.id).count()

    if not user:
        return redirect(url_for("oops",
                                error="Invalid user ID"))

    if request.method == "GET":
        limit = 5
        older_than = request.args.get("older_than")
        if older_than == None:
            older_than = datetime.datetime.utcnow() + datetime.timedelta(seconds=30)

        return render_template("wall/user_wall.html",
                               posts=Post.get_posts_for_user_wall(id,
                                                                  older_than=older_than,
                                                                  limit=limit),
                               user=user,
                               form=PostForm(),
                               limit=limit,
                               subscriber_count=subscriber_count,
                               subscription_count=subscription_count,
                               post_count=post_count,
                               comment_count=comment_count)

    form = PostForm(request.form)

    if not form.validate():
        return render_template("wall/user_wall.html",
                               posts=Post.get_posts_for_user_wall(id),
                               user=user,
                               form=form,
                               subscriber_count=subscriber_count,
                               subscription_count=subscription_count,
                               post_count=post_count,
                               comment_count=comment_count)

    content = re.sub(r"^\s+",
                     "",
                     form.content.data,
                     flags=re.MULTILINE).strip()
    owner_id = current_user.id
    wall_id = user.wall.id

    post = Post(content, owner_id, wall_id)
    db.session().add(post)
    db.session().commit()

    return redirect(url_for("user_wall",
                            id=id))
Ejemplo n.º 8
0
def posts_create():
    form = PostForm(request.form)

    if not form.validate():
        return render_template("posts/new.html", form=form)

    post = Post(form.name.data)
    post.accountId = current_user.id

    db.session().add(post)
    db.session().commit()

    return redirect(url_for("posts_index"))
Ejemplo n.º 9
0
def threads_create():
    if request.method == "GET":
        return render_template("threads/new.html", form=ThreadForm())

    form = ThreadForm(request.form)
    if not form.validate():
        return render_template("threads/new.html", form=form)

    # Create Thread
    dbThread = Thread(form.topic.data)
    db.session().add(dbThread)
    db.session().flush()

    # Add Thread category dependicies
    if (form.yleinen.data == True):
        dbCategory = Category.query.filter_by(name="Yleinen").first()
        dbThread.categories.append(dbCategory)

    if (form.retro.data == True):
        dbCategory = Category.query.filter_by(name="Retro").first()
        dbThread.categories.append(dbCategory)

    if (form.wii.data == True):
        dbCategory = Category.query.filter_by(name="Wii").first()
        dbThread.categories.append(dbCategory)

    if (form.wiiu.data == True):
        dbCategory = Category.query.filter_by(name="WiiU").first()
        dbThread.categories.append(dbCategory)

    if (form.switch.data == True):
        dbCategory = Category.query.filter_by(name="Switch").first()
        dbThread.categories.append(dbCategory)

    if (form.ds.data == True):
        dbCategory = Category.query.filter_by(name="DS").first()
        dbThread.categories.append(dbCategory)

    if (form.threeDs.data == True):
        dbCategory = Category.query.filter_by(name="3DS").first()
        dbThread.categories.append(dbCategory)

    # Create Post
    dbPost = Post(form.message.data, 1)
    dbPost.account_id = current_user.id
    dbPost.thread_id = dbThread.id
    db.session().add(dbPost)

    db.session().commit()
    return redirect(url_for("threads_index"))
Ejemplo n.º 10
0
def posts_submit():
    form = PostForm(request.form)

    if not form.validate():
        return render_template('posts/submit.html', form=form)

    with session_scope() as session:
        post = Post(form.title.data, form.content.data)
        post.account_id = current_user.id

        session.add(post)
        session.commit()

    return redirect(url_for('posts_index'))
Ejemplo n.º 11
0
def post_create(thread_id):
    form = PostForm(request.form)
    form.content.data = escape(form.content.data)

    if not form.validate():
        return render_template("post/new.html", form=form, thread_id=thread_id)
    if not current_user.is_authenticated:
        flash("Authentication error")
        return redirect(url_for("category_index"))
    p = Post(form.content.data)
    p.account_id = current_user.id
    p.thread_id = thread_id
    db.session().add(p)
    db.session().commit()
    return redirect(url_for("thread_view", thread_id=thread_id))
Ejemplo n.º 12
0
def hallOfFame():
    response = Post.get_the_post_with_most_comments()

    return render_template("hallOfFame.html",
                           bestPost=response[0],
                           bestPostPoster=User.query.get(response[1]).username,
                           numberOfComments=response[2])
Ejemplo n.º 13
0
def threads_create():
    form = ThreadForm(request.form)

    if not form.validate():
        return render_template("threads/newthread.html", form=ThreadForm())

    t = Thread(form.header.data)

    db.session().add(t)
    db.session().commit()

    p = Post(form.content.data)
    p.account_id = current_user.id
    p.thread_id = Thread.find_by_name(t.header)
    db.session().add(p)
    db.session().commit()
    return redirect(url_for("threads_index"))
Ejemplo n.º 14
0
def thread_reply(thread_id):
    thread = Thread.query.get(thread_id)

    form = PostForm(request.form)

    if not form.validate():
        return redirect(url_for("view_thread", thread_id=thread_id, form=form))

    post = Post(form.message.data)
    post.user_id = current_user.id
    post.thread_id = thread.id

    db.session().add(post)
    db.session().commit()

    return redirect(
        url_for("view_thread", thread_id=thread_id, form=ThreadForm()))
Ejemplo n.º 15
0
def posts_new(threadId):
    if request.method == "GET":
        return render_template("posts/new.html",
                               form=PostForm(),
                               threadId=threadId)

    form = PostForm(request.form)
    if not form.validate():
        return render_template("posts/new.html", form=form, threadId=threadId)

    dbPost = Post(form.message.data, 0)
    dbPost.account_id = current_user.id
    dbPost.thread_id = threadId

    db.session().add(dbPost)
    db.session().commit()
    return redirect(url_for("threads_open", threadId=threadId))
Ejemplo n.º 16
0
def index():
    countUsers = User.total_users()
    countThreads = Thread.total_threads()
    countPosts = Post.total_posts()

    return render_template("index.html",
                           countUsers=countUsers,
                           countThreads=countThreads,
                           countPosts=countPosts)
Ejemplo n.º 17
0
def posts_create():
    form = PostForm(request.form)
    thread_id = int(request.args.get('thread_id'))
    p = Post(form.content.data,
             account_id=current_user.id,
             thread_id=thread_id)

    db.session().add(p)
    db.session().commit()

    return redirect(url_for("get_thread", thread_id=thread_id))
Ejemplo n.º 18
0
def create_post(topic_id):
    topic = Topic.query.get(topic_id)

    if topic is None:
        return redirect(url_for("topics_index"))

    form = PostForm(request.form)

    if not form.validate():
        return render_template("topics/single.html", topic=topic, form=form)

    body = request.form.get("body")
    post = Post(body)
    post.topic = topic
    post.author_id = current_user.id

    db.session().add(post)
    db.session().commit()

    return redirect(url_for("view_topic", topic_id=topic_id))
Ejemplo n.º 19
0
def threads_open(threadId):

    # List(4 elements): [0] Thread.id, [1] Thread.topic, [2] Thread.created, [3] Thread.modified
    threadData = Thread.get_thread(threadId)

    # List of list(6 elements): [0] Account.id, [1] Account.username, [2] Post.id, [3] Post.message, [4] Post.created, [5] Post.modified
    postsData = Post.get_posts_in_thread(threadId)

    return render_template("threads/open.html",
                           threadData=threadData,
                           postsData=postsData)
Ejemplo n.º 20
0
def user_feed():
    limit = 5
    older_than = request.args.get("older_than")
    if older_than == None:
        older_than = datetime.datetime.utcnow()

    return render_template("feed/user_feed.html",
                           posts=Post.get_posts_for_user_feed(
                               current_user.id,
                               older_than=older_than,
                               limit=limit),
                           limit=limit)
Ejemplo n.º 21
0
def posts_create():
    form = PostForm(request.form)

    if not form.validate():
        return render_template("posts/list.html", posts = Post.query.filter_by(parent_id=None).order_by(Post.create_time.desc()).all(), hashtags = Hashtag.get_trending_hashtags(1, 5), form = form, show = True)

    post = Post(current_user.id, form.content.data, None)
  
    db.session().add(post)
    db.session().commit()
  
    return redirect(url_for("posts_index"))
Ejemplo n.º 22
0
def threads_remove(threadId):
    postsData = Post.get_posts_in_thread(threadId)

    for row in postsData:
        dbPost = Post.query.get(row[2])
        db.session().delete(dbPost)

    dbThread = Thread.query.get(threadId)
    db.session().delete(dbThread)

    db.session().commit()
    return redirect(url_for("threads_index"))
Ejemplo n.º 23
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(body=form.body.data, user_id=current_user.id)
        db.session.add(post)
        db.session.commit()
        flash('¡Has dejado el mensaje', 'success')
        return redirect(url_for('main.debate'))
    return render_template('posts/create_post.html',
                           title='Nuevo Mensaje',
                           form=form,
                           legend='Nuevo Mensaje')
Ejemplo n.º 24
0
def posts_create(theme_num):

    form = PostForm(request.form)

    if not form.validate():
        return render_template("posts/write.html",
                               form=form,
                               theme_id=theme_num)

    b = Topic(form.topic.data)
    old_topic = Topic.query.filter_by(name=form.topic.data).first()

    if not old_topic:
        b.theme_id = theme_num
        db.session().add(b)
        db.session().commit()
        Subject = Topic.query.filter_by(name=form.topic.data).first()

        a = Post(request.form.get("content"))
        a.topic = form.topic.data
        a.author = current_user.name
        a.account_id = current_user.id
        a.subject_id = Subject.id
        db.session().add(a)

        db.session().commit()

        return redirect(url_for("topic_id", theme_id=theme_num))

    else:
        flash("Topic already taken!")
        return render_template("posts/write.html",
                               form=form,
                               error="Topic already taken!",
                               theme_id=theme_num)
Ejemplo n.º 25
0
def posts_reply_to(post_id):
    form = PostForm(request.form)

    if not form.validate():
        post = Post.query.get(post_id)
        return render_template("posts/reply_to.html", post = post, form = form)

    post = Post(current_user.id, form.content.data, post_id)

    db.session().add(post)
    db.session().commit()

    return redirect(url_for("posts_thread", post_id = post_id))
Ejemplo n.º 26
0
def user_panel(user_id):
    try:
        user_id = int(user_id)
    except ValueError:
        flash("No such user")
        return redirect(url_for("category_index"))
    if not current_user.id == user_id and not current_user.has_role("admin"):
        flash("Authentication error")
        return redirect(url_for("category_index"))
    return render_template("control/user.html",
                           user=user_datastore.get_user(user_id),
                           recent_posts=Post.find_latest_posts(user_id),
                           recent_threads=Thread.find_latest_threads(user_id))
Ejemplo n.º 27
0
def posts_create():
  form = PostForm(request.form)

  if not form.validate():
    return render_template("posts/new.html", form=form)

  post = Post(form.title.data, False, form.url.data)
  post.author = current_user

  if not form.url.data:
    post.content = form.text.data
    post.is_text = True

  db.session().add(post)
  # insert post to database to generate id
  db.session().flush()

  upvote = Upvote(current_user.id, post.id)
  db.session().add(upvote)
  db.session().commit()

  return redirect(url_for("posts_index"))
Ejemplo n.º 28
0
def posts_index():
    if request.method == "GET":
        return render_template("posts/list.html",
                               posts=Post.query.order_by(
                                   Post.date_created.desc()).all(),
                               form=PostForm())

    if not current_user.is_authenticated:
        return redirect("/auth/login")

    form = PostForm(request.form)
    if not form.validate():
        return render_template("posts/list.html",
                               form=form,
                               posts=Post.query.order_by(
                                   Post.date_created.desc()).all())

    p = Post(form.content.data)
    p.account_id = current_user.id

    db.session().add(p)
    db.session().commit()

    return redirect(url_for("posts_index"))
Ejemplo n.º 29
0
def posts_reply(topic_id):
    form = ReplyForm(request.form)
    subject = Topic.query.filter_by(id=topic_id).first()

    a = Post(request.form.get("content"))
    a.topic = subject.name
    a.author = current_user.name
    a.account_id = current_user.id
    a.subject_id = subject.id
    db.session().add(a)

    db.session().commit()

    return redirect(url_for("post_id", topic_id=topic_id))
Ejemplo n.º 30
0
def topics_index():
    page = request.args.get("page", 1, type=int)
    topics = Topic.query.options(joinedload(Topic.posts),
                                 joinedload(Topic.tags),
                                 joinedload(Topic.author)).order_by(
                                     desc(Topic.date_created)).paginate(
                                         page, 5, False)

    next_url = None
    prev_url = None
    if topics.has_next:
        next_url = url_for("topics_index", page=topics.next_num)
    if topics.has_prev:
        prev_url = url_for("topics_index", page=topics.prev_num)

    most_liked_posts = Post.find_most_liked_posts_today()
    return render_template("topics/all_topics.html",
                           topics=topics.items,
                           most_liked_posts=most_liked_posts,
                           next_url=next_url,
                           prev_url=prev_url)