Esempio n. 1
0
    def get(self, forum_id, slug=None):

        forum_instance, forumsread = Forum.get_forum(
            forum_id=forum_id, user=real(current_user)
        )

        if forum_instance.external:
            return redirect(forum_instance.external)

        # remove the current forum from the select field (move).
        available_forums = Forum.query.order_by(Forum.position).all()
        available_forums.remove(forum_instance)
        page = request.args.get("page", 1, type=int)
        topics = Forum.get_topics(
            forum_id=forum_instance.id,
            user=real(current_user),
            page=page,
            per_page=flaskbb_config["TOPICS_PER_PAGE"]
        )

        return render_template(
            "forum/edit_forum.html",
            forum=forum_instance,
            topics=topics,
            available_forums=available_forums,
            forumsread=forumsread,
        )
Esempio n. 2
0
    def get(self, topic_id, slug=None):
        page = request.args.get("page", 1, type=int)

        # Fetch some information about the topic
        topic = Topic.get_topic(topic_id=topic_id, user=real(current_user))

        # Count the topic views
        topic.views += 1
        topic.save()

        # Update the topicsread status if the user hasn't read it
        forumsread = None
        if current_user.is_authenticated:
            forumsread = ForumsRead.query.filter_by(
                user_id=current_user.id, forum_id=topic.forum_id).first()

        topic.update_read(real(current_user), topic.forum, forumsread)

        # fetch the posts in the topic
        posts = Post.query.outerjoin(User, Post.user_id == User.id).filter(
            Post.topic_id == topic.id).add_entity(User).order_by(
                Post.id.asc()).paginate(page, flaskbb_config["POSTS_PER_PAGE"],
                                        False)

        # Abort if there are no posts on this page
        if len(posts.items) == 0:
            abort(404)

        return render_template("forum/topic.html",
                               topic=topic,
                               posts=posts,
                               last_seen=time_diff(),
                               form=self.form())
Esempio n. 3
0
    def get(self, forum_id, slug=None):

        forum_instance, forumsread = Forum.get_forum(
            forum_id=forum_id, user=real(current_user)
        )

        if forum_instance.external:
            return redirect(forum_instance.external)

        # remove the current forum from the select field (move).
        available_forums = Forum.query.order_by(Forum.position).all()
        available_forums.remove(forum_instance)
        page = request.args.get("page", 1, type=int)
        topics = Forum.get_topics(
            forum_id=forum_instance.id,
            user=real(current_user),
            page=page,
            per_page=flaskbb_config["TOPICS_PER_PAGE"]
        )

        return render_template(
            "forum/edit_forum.html",
            forum=forum_instance,
            topics=topics,
            available_forums=available_forums,
            forumsread=forumsread,
        )
Esempio n. 4
0
    def get(self):
        page = request.args.get('page', 1, type=int)
        topics = real(current_user).tracked_topics.outerjoin(
            TopicsRead,
            db.and_(TopicsRead.topic_id == Topic.id, TopicsRead.user_id ==
                    real(current_user).id)).add_entity(TopicsRead).order_by(
                        Topic.last_updated.desc()).paginate(
                            page, flaskbb_config['TOPICS_PER_PAGE'], True)

        return render_template('forum/topictracker.html', topics=topics)
Esempio n. 5
0
    def post(self):
        topic_ids = request.form.getlist('rowid')
        tmp_topics = Topic.query.filter(Topic.id.in_(topic_ids)).all()

        for topic in tmp_topics:
            real(current_user).untrack_topic(topic)

        real(current_user).save()

        flash(_('%(topic_count)s topics untracked.', topic_count=len(tmp_topics)), 'success')
        return redirect(url_for('forum.topictracker'))
Esempio n. 6
0
    def post(self):
        topic_ids = request.form.getlist("rowid")
        tmp_topics = Topic.query.filter(Topic.id.in_(topic_ids)).all()

        for topic in tmp_topics:
            real(current_user).untrack_topic(topic)

        real(current_user).save()

        flash(
            _("%(topic_count)s topics untracked.", topic_count=len(tmp_topics)),
            "success"
        )
        return redirect(url_for("forum.topictracker"))
Esempio n. 7
0
    def get(self, category_id, slug=None):
        category, forums = Category.get_forums(category_id=category_id,
                                               user=real(current_user))

        return render_template("forum/category.html",
                               forums=forums,
                               category=category)
Esempio n. 8
0
    def post(self, topic_id, slug=None):
        topic = Topic.get_topic(topic_id=topic_id, user=real(current_user))
        form = self.form()

        if not form:
            flash(_("Cannot post reply"), "warning")
            return redirect(topic.url)

        elif form.validate_on_submit():
            post = form.save(real(current_user), topic)
            return redirect(post.url)

        else:
            for e in form.errors.get("content", []):
                flash(e, "danger")
            return redirect(topic.url)
Esempio n. 9
0
    def get(self):
        categories = Category.get_all(user=real(current_user))

        # Fetch a few stats about the forum
        user_count = User.query.count()
        topic_count = Topic.query.count()
        post_count = Post.query.count()
        newest_user = User.query.order_by(User.id.desc()).first()
        print('newest', newest_user)

        # Check if we use redis or not
        if not current_app.config['REDIS_ENABLED']:
            online_users = len(User.get_active()) + random.randint(-3, 3)
            # Because we do not have server side sessions, we cannot check if there
            # are online guests
            online_guests = None
        else:

            online_users = len(User.get_active()) + random.randint(-3, 3)
            online_guests = len(get_online_users(guest=True))

        return render_template('forum/index.html',
                               categories=categories,
                               user_count=user_count,
                               topic_count=topic_count,
                               post_count=post_count,
                               newest_user=newest_user,
                               online_users=online_users,
                               online_guests=online_guests)
Esempio n. 10
0
 def post(self, forum_id, slug=None):
     forum_instance = Forum.query.filter_by(id=forum_id).first_or_404()
     form = self.form()
     if 'preview' in request.form and form.validate():
         return render_template(
             'forum/new_topic.html',
             forum=forum_instance,
             form=form,
             preview=form.content.data
         )
     elif 'submit' in request.form and form.validate():
         from urllib import quote
         import re
         def linked(match):
             group = match.group(1)
             if not "title" in group:
                 url = quote(group)
             else:
                 url = quote(group[:-25])
             return "(http://flaskweb.com/link?url="+url+")" 
         form.content.data = re.sub("\(((https?|ftp|file)://[-A-Za-z0-9+&@\"#/%?=~_| !:,.;]+[-A-Za-z0-9+&@#/\"% =~_|])\)",linked,form.content.data,flags=re.I)
         topic = form.save(real(current_user), forum_instance)
         # redirect to the new topic
         return redirect(url_for('forum.view_topic', topic_id=topic.id))
     else:
         return render_template(
             'forum/new_topic.html', forum=forum_instance, form=form
         )
Esempio n. 11
0
    def _get_choices(self):
        'Get list of forums to choose from'

        return [
            (category.title, [(forum.id, forum.title) for forum, x in forums])
            for category, forums in Category.get_all(user=real(current_user))
        ]
Esempio n. 12
0
    def get(self):
        categories = Category.get_all(user=real(current_user))

        # Fetch a few stats about the forum
        user_count = User.query.count()
        topic_count = Topic.query.count()
        post_count = Post.query.count()
        newest_user = User.query.order_by(User.id.desc()).first()

        # Check if we use redis or not
        if not current_app.config["REDIS_ENABLED"]:
            online_users = User.query.filter(
                User.lastseen >= time_diff()).count()

            # Because we do not have server side sessions,
            # we cannot check if there are online guests
            online_guests = None
        else:
            online_users = len(get_online_users())
            online_guests = len(get_online_users(guest=True))

        return render_template("forum/index.html",
                               categories=categories,
                               user_count=user_count,
                               topic_count=topic_count,
                               post_count=post_count,
                               newest_user=newest_user,
                               online_users=online_users,
                               online_guests=online_guests)
Esempio n. 13
0
def index():
    categories = Category.get_all(user=real(current_user))

    # Fetch a few stats about the forum
    user_count = User.query.count()
    topic_count = Topic.query.count()
    post_count = Post.query.count()
    newest_user = User.query.order_by(User.id.desc()).first()

    # Check if we use redis or not
    if not current_app.config["REDIS_ENABLED"]:
        online_users = User.query.filter(User.lastseen >= time_diff()).count()

        # Because we do not have server side sessions, we cannot check if there
        # are online guests
        online_guests = None
    else:
        online_users = len(get_online_users())
        online_guests = len(get_online_users(guest=True))

    return render_template("forum/index.html",
                           categories=categories,
                           user_count=user_count,
                           topic_count=topic_count,
                           post_count=post_count,
                           newest_user=newest_user,
                           online_users=online_users,
                           online_guests=online_guests)
Esempio n. 14
0
    def post(self, topic_id, slug=None):
        topic = Topic.get_topic(topic_id=topic_id, user=real(current_user))
        form = self.form()

        if not form:
            flash(_('Cannot post reply'), 'warning')
            return redirect('forum.view_topic', topic_id=topic_id, slug=slug)

        elif form.validate_on_submit():
            post = form.save(real(current_user), topic)
            return redirect(url_for('forum.view_post', post_id=post.id))

        else:
            for e in form.errors.get('content', []):
                flash(e, 'danger')
            return redirect(url_for('forum.view_topic', topic_id=topic_id, slug=slug))
Esempio n. 15
0
    def get(self, category_id, slug=None):
        category, forums = Category.get_forums(
            category_id=category_id, user=real(current_user)
        )

        return render_template(
            "forum/category.html", forums=forums, category=category
        )
Esempio n. 16
0
    def post(self, post_id):
        form = self.form()
        if form.validate_on_submit():
            post = Post.query.filter_by(id=post_id).first_or_404()
            form.save(real(current_user), post)
            flash(_("Thanks for reporting."), "success")

        return render_template("forum/report_post.html", form=form)
Esempio n. 17
0
    def post(self, post_id):
        form = self.form()
        if form.validate_on_submit():
            post = Post.query.filter_by(id=post_id).first_or_404()
            form.save(real(current_user), post)
            flash(_("Thanks for reporting."), "success")

        return render_template("forum/report_post.html", form=form)
Esempio n. 18
0
    def post(self, topic_id, slug=None):
        topic = Topic.get_topic(topic_id=topic_id, user=real(current_user))
        form = self.form()

        if not form:
            flash(_("Cannot post reply"), "warning")
            return redirect("forum.view_topic", topic_id=topic_id, slug=slug)

        elif form.validate_on_submit():
            post = form.save(real(current_user), topic)
            return redirect(url_for("forum.view_post", post_id=post.id))

        else:
            for e in form.errors.get("content", []):
                flash(e, "danger")
            return redirect(
                url_for("forum.view_topic", topic_id=topic_id, slug=slug)
            )
Esempio n. 19
0
    def post(self, forum_id, slug=None):
        forum_instance = Forum.query.filter_by(id=forum_id).first_or_404()
        form = self.form()
        if form.validate_on_submit():
            topic = form.save(real(current_user), forum_instance)
            return redirect(url_for("forum.view_topic", topic_id=topic.id))

        return render_template(
            "forum/new_topic.html", forum=forum_instance, form=form
        )
Esempio n. 20
0
    def post(self, forum_id, slug=None):
        forum_instance = Forum.query.filter_by(id=forum_id).first_or_404()
        form = self.form()
        if form.validate_on_submit():
            topic = form.save(real(current_user), forum_instance)
            return redirect(url_for("forum.view_topic", topic_id=topic.id))

        return render_template(
            "forum/new_topic.html", forum=forum_instance, form=form
        )
Esempio n. 21
0
def view_topic(topic_id, slug=None):
    page = request.args.get('page', 1, type=int)

    # Fetch some information about the topic
    topic = Topic.get_topic(topic_id=topic_id, user=real(current_user))

    # Count the topic views
    topic.views += 1
    topic.save()

    # fetch the posts in the topic
    posts = Post.query.\
        outerjoin(User, Post.user_id == User.id).\
        filter(Post.topic_id == topic.id).\
        add_entity(User).\
        order_by(Post.id.asc()).\
        paginate(page, flaskbb_config['POSTS_PER_PAGE'], False)

    # Abort if there are no posts on this page
    if len(posts.items) == 0:
        abort(404)

    # Update the topicsread status if the user hasn't read it
    forumsread = None
    if current_user.is_authenticated:
        forumsread = ForumsRead.query.\
            filter_by(user_id=real(current_user).id,
                      forum_id=topic.forum.id).first()

    topic.update_read(real(current_user), topic.forum, forumsread)

    form = None
    if Permission(CanPostReply):
        form = QuickreplyForm()
        if form.validate_on_submit():
            post = form.save(real(current_user), topic)
            return view_post(post.id)

    return render_template("forum/topic.html",
                           topic=topic,
                           posts=posts,
                           last_seen=time_diff(),
                           form=form)
Esempio n. 22
0
def view_forum(forum_id, slug=None):
    page = request.args.get('page', 1, type=int)

    forum_instance, forumsread = Forum.get_forum(
        forum_id=forum_id, user=real(current_user)
    )

    if forum_instance.external:
        return redirect(forum_instance.external)

    topics = Forum.get_topics(
        forum_id=forum_instance.id, user=real(current_user), page=page,
        per_page=flaskbb_config["TOPICS_PER_PAGE"]
    )

    return render_template(
        "forum/forum.html", forum=forum_instance,
        topics=topics, forumsread=forumsread,
    )
Esempio n. 23
0
    def get(self, forum_id, slug=None):
        page = request.args.get("page", 1, type=int)

        forum_instance, forumsread = Forum.get_forum(forum_id=forum_id,
                                                     user=real(current_user))

        if forum_instance.external:
            return redirect(forum_instance.external)

        topics = Forum.get_topics(forum_id=forum_instance.id,
                                  user=real(current_user),
                                  page=page,
                                  per_page=flaskbb_config["TOPICS_PER_PAGE"])

        return render_template(
            "forum/forum.html",
            forum=forum_instance,
            topics=topics,
            forumsread=forumsread,
        )
Esempio n. 24
0
    def get(self, topic_id, slug=None):
        page = request.args.get("page", 1, type=int)

        # Fetch some information about the topic
        topic = Topic.get_topic(topic_id=topic_id, user=real(current_user))

        # Count the topic views
        topic.views += 1
        topic.save()

        # Update the topicsread status if the user hasn't read it
        forumsread = None
        if current_user.is_authenticated:
            forumsread = ForumsRead.query.filter_by(
                user_id=current_user.id,
                forum_id=topic.forum_id).first()

        topic.update_read(real(current_user), topic.forum, forumsread)

        # fetch the posts in the topic
        posts = Post.query.outerjoin(
            User, Post.user_id == User.id
        ).filter(
            Post.topic_id == topic.id
        ).add_entity(
            User
        ).order_by(
            Post.id.asc()
        ).paginate(page, flaskbb_config["POSTS_PER_PAGE"], False)

        # Abort if there are no posts on this page
        if len(posts.items) == 0:
            abort(404)

        return render_template(
            "forum/topic.html",
            topic=topic,
            posts=posts,
            last_seen=time_diff(),
            form=self.form()
        )
Esempio n. 25
0
    def post(self, post_id):
        post = Post.query.filter_by(id=post_id).first_or_404()
        form = self.form(obj=post)

        if form.validate_on_submit():
            form.populate_obj(post)
            post = form.save(real(current_user), post.topic)
            return redirect(url_for("forum.view_post", post_id=post.id))

        return render_template(
            "forum/new_post.html", topic=post.topic, form=form, edit_mode=True
        )
Esempio n. 26
0
def view_topic(topic_id, slug=None):
    page = request.args.get('page', 1, type=int)

    # Fetch some information about the topic
    topic = Topic.get_topic(topic_id=topic_id, user=real(current_user))

    # Count the topic views
    topic.views += 1
    topic.save()

    # fetch the posts in the topic
    posts = Post.query.\
        outerjoin(User, Post.user_id == User.id).\
        filter(Post.topic_id == topic.id).\
        add_entity(User).\
        order_by(Post.id.asc()).\
        paginate(page, flaskbb_config['POSTS_PER_PAGE'], False)

    # Abort if there are no posts on this page
    if len(posts.items) == 0:
        abort(404)

    # Update the topicsread status if the user hasn't read it
    forumsread = None
    if current_user.is_authenticated:
        forumsread = ForumsRead.query.\
            filter_by(user_id=real(current_user).id,
                      forum_id=topic.forum.id).first()

    topic.update_read(real(current_user), topic.forum, forumsread)

    form = None
    if Permission(CanPostReply):
        form = QuickreplyForm()
        if form.validate_on_submit():
            post = form.save(real(current_user), topic)
            return view_post(post.id)

    return render_template("forum/topic.html", topic=topic, posts=posts,
                           last_seen=time_diff(), form=form)
Esempio n. 27
0
def topictracker():
    page = request.args.get("page", 1, type=int)
    topics = real(current_user).tracked_topics.\
        outerjoin(TopicsRead,
                  db.and_(TopicsRead.topic_id == Topic.id,
                          TopicsRead.user_id == real(current_user).id)).\
        add_entity(TopicsRead).\
        order_by(Topic.last_updated.desc()).\
        paginate(page, flaskbb_config['TOPICS_PER_PAGE'], True)

    # bulk untracking
    if request.method == "POST":
        topic_ids = request.form.getlist("rowid")
        tmp_topics = Topic.query.filter(Topic.id.in_(topic_ids)).all()

        for topic in tmp_topics:
            real(current_user).untrack_topic(topic)
        real(current_user).save()

        flash(
            _("%(topic_count)s topics untracked.",
              topic_count=len(tmp_topics)), "success")
        return redirect(url_for("forum.topictracker"))

    return render_template("forum/topictracker.html", topics=topics)
Esempio n. 28
0
    def post(self, topic_id, slug=None):
        topic = Topic.query.filter_by(id=topic_id).first_or_404()
        form = self.form()
        if form.validate_on_submit():
            if 'preview' in request.form:
                return render_template(
                    'forum/new_post.html', topic=topic, form=form, preview=form.content.data
                )
            else:
                post = form.save(real(current_user), topic)
                return redirect(url_for('forum.view_post', post_id=post.id))

        return render_template('forum/new_post.html', topic=topic, form=form)
Esempio n. 29
0
 def post(self, forum_id, slug=None):
     forum_instance = Forum.query.filter_by(id=forum_id).first_or_404()
     form = self.form()
     if 'preview' in request.form and form.validate():
         return render_template(
             'forum/new_topic.html', forum=forum_instance, form=form, preview=form.content.data
         )
     elif 'submit' in request.form and form.validate():
         topic = form.save(real(current_user), forum_instance)
         # redirect to the new topic
         return redirect(url_for('forum.view_topic', topic_id=topic.id))
     else:
         return render_template('forum/new_topic.html', forum=forum_instance, form=form)
Esempio n. 30
0
    def post(self, topic_id, slug=None, post_id=None):
        topic = Topic.query.filter_by(id=topic_id).first_or_404()
        form = self.form()

        # check if topic exists
        if post_id is not None:
            post = Post.query.filter_by(id=post_id).first_or_404()

        if form.validate_on_submit():
            post = form.save(real(current_user), topic)
            return redirect(url_for("forum.view_post", post_id=post.id))

        return render_template("forum/new_post.html", topic=topic, form=form)
Esempio n. 31
0
    def post(self, topic_id, slug=None, post_id=None):
        topic = Topic.query.filter_by(id=topic_id).first_or_404()
        form = self.form()

        # check if topic exists
        if post_id is not None:
            post = Post.query.filter_by(id=post_id).first_or_404()

        if form.validate_on_submit():
            post = form.save(real(current_user), topic)
            return redirect(url_for("forum.view_post", post_id=post.id))

        return render_template("forum/new_post.html", topic=topic, form=form)
Esempio n. 32
0
    def post(self, conversation_id):
        conversation = Conversation.query.filter_by(
            id=conversation_id, user_id=current_user.id).first_or_404()

        form = self.form()
        if form.validate_on_submit():
            to_user_id = None
            # If the current_user is the user who recieved the message
            # then we have to change the id's a bit.
            if current_user.id == conversation.to_user_id:
                to_user_id = conversation.from_user_id
                to_user = conversation.from_user
            else:
                to_user_id = conversation.to_user_id
                to_user = conversation.to_user

            form.save(conversation=conversation, user_id=current_user.id)

            # save the message in the recievers conversation
            old_conv = conversation
            conversation = Conversation.query.filter(
                Conversation.user_id == to_user_id,
                Conversation.shared_id == conversation.shared_id,
            ).first()

            # user deleted the conversation, start a new conversation with just
            # the recieving message
            if conversation is None:
                conversation = Conversation(
                    subject=old_conv.subject,
                    from_user=real(current_user),
                    to_user=to_user,
                    user_id=to_user_id,
                    shared_id=old_conv.shared_id,
                )
                conversation.save()

            form.save(conversation=conversation,
                      user_id=current_user.id,
                      unread=True)
            invalidate_cache(conversation.to_user)

            return redirect(
                url_for(
                    "conversations_bp.view_conversation",
                    conversation_id=old_conv.id,
                ))

        return render_template("conversation.html",
                               conversation=conversation,
                               form=form)
Esempio n. 33
0
    def post(self, topic_id, slug=None):
        topic = Topic.query.filter_by(id=topic_id).first_or_404()
        post = topic.first_post
        form = self.focrm(obj=post, title=topic.title)

        if form.validate_on_submit():
            form.populate_obj(topic, post)
            topic = form.save(real(current_user), topic.forum)

            return redirect(url_for("forum.view_topic", topic_id=topic.id))

        return render_template(
            "forum/new_topic.html", forum=topic.forum, form=form, edit_mode=True
        )
Esempio n. 34
0
    def post(self, topic_id, slug=None):
        topic = Topic.get_topic(topic_id=topic_id, user=real(current_user))
        form = self.form()

        if not form:
            flash(_("Cannot post reply"), "warning")
            return redirect("forum.view_topic", topic_id=topic_id, slug=slug)

        elif form.validate_on_submit():
            try:
                post = form.save(real(current_user), topic)
                return redirect(url_for("forum.view_post", post_id=post.id))
            except StopNewPost as e:
                flash(e.reason, "danger")
            except BaseFlaskBBError as e:
                flask(e.reason, "warning")
            except Exception:
                flash(_("Unrecoverable error while posting reply"))
        else:
            for e in form.errors.get("content", []):
                flash(e, "danger")

        return self.get(topic_id=topic_id, slug=slug)
Esempio n. 35
0
    def post(self, post_id):
        post = Post.query.filter_by(id=post_id).first_or_404()
        form = self.form(obj=post)

        if form.validate_on_submit():
            form.populate_obj(post)
            post.date_modified = time_utcnow()
            post.modified_by = real(current_user).username
            post.save()
            return redirect(url_for("forum.view_post", post_id=post.id))

        return render_template(
            "forum/new_post.html", topic=post.topic, form=form, edit_mode=True
        )
Esempio n. 36
0
    def get(self):
        page = request.args.get("page", 1, type=int)
        topics = real(current_user).tracked_topics.\
            outerjoin(
                TopicsRead,
                db.and_(
                    TopicsRead.topic_id == Topic.id,
                    TopicsRead.user_id == real(current_user).id
                )).\
            outerjoin(Post, Topic.last_post_id == Post.id).\
            outerjoin(Forum, Topic.forum_id == Forum.id).\
            outerjoin(
                ForumsRead,
                db.and_(
                    ForumsRead.forum_id == Forum.id,
                    ForumsRead.user_id == real(current_user).id
                )).\
            add_entity(Post).\
            add_entity(TopicsRead).\
            add_entity(ForumsRead).\
            order_by(Topic.last_updated.desc()).\
            paginate(page, flaskbb_config["TOPICS_PER_PAGE"], True)

        return render_template("forum/topictracker.html", topics=topics)
Esempio n. 37
0
    def get(self):
        page = request.args.get("page", 1, type=int)
        topics = real(current_user).tracked_topics.\
            outerjoin(
                TopicsRead,
                db.and_(
                    TopicsRead.topic_id == Topic.id,
                    TopicsRead.user_id == real(current_user).id
                )).\
            outerjoin(Post, Topic.last_post_id == Post.id).\
            outerjoin(Forum, Topic.forum_id == Forum.id).\
            outerjoin(
                ForumsRead,
                db.and_(
                    ForumsRead.forum_id == Forum.id,
                    ForumsRead.user_id == real(current_user).id
                )).\
            add_entity(Post).\
            add_entity(TopicsRead).\
            add_entity(ForumsRead).\
            order_by(Topic.last_updated.desc()).\
            paginate(page, flaskbb_config["TOPICS_PER_PAGE"], True)

        return render_template("forum/topictracker.html", topics=topics)
Esempio n. 38
0
    def post(self, forum_id, slug=None):
        forum_instance = Forum.query.filter_by(id=forum_id).first_or_404()
        form = self.form()
        if form.validate_on_submit():
            try:
                topic = form.save(real(current_user), forum_instance)
                return redirect(url_for("forum.view_topic", topic_id=topic.id))
            except StopNewTopic as e:
                flash(e.reason, "danger")
            except BaseFlaskBBError as e:
                flask(e.reason, "warning")
            except Exception:
                flash(_("Unrecoverable error while posting new topic"))

        return render_template("forum/new_topic.html",
                               forum=forum_instance,
                               form=form)
Esempio n. 39
0
    def post(self, forum_id=None, slug=None):
        # Mark a single forum as read
        if forum_id is not None:
            forum_instance = Forum.query.filter_by(id=forum_id).first_or_404()
            forumsread = ForumsRead.query.filter_by(
                user_id=real(current_user).id, forum_id=forum_instance.id
            ).first()
            TopicsRead.query.filter_by(
                user_id=real(current_user).id, forum_id=forum_instance.id
            ).delete()

            if not forumsread:
                forumsread = ForumsRead()
                forumsread.user = real(current_user)
                forumsread.forum = forum_instance

            forumsread.last_read = time_utcnow()
            forumsread.cleared = time_utcnow()

            db.session.add(forumsread)
            db.session.commit()

            flash(
                _(
                    "Forum %(forum)s marked as read.",
                    forum=forum_instance.title
                ), "success"
            )

            return redirect(forum_instance.url)

        # Mark all forums as read
        ForumsRead.query.filter_by(user_id=real(current_user).id).delete()
        TopicsRead.query.filter_by(user_id=real(current_user).id).delete()

        forums = Forum.query.all()
        forumsread_list = []
        for forum_instance in forums:
            forumsread = ForumsRead()
            forumsread.user = real(current_user)
            forumsread.forum = forum_instance
            forumsread.last_read = time_utcnow()
            forumsread.cleared = time_utcnow()
            forumsread_list.append(forumsread)

        db.session.add_all(forumsread_list)
        db.session.commit()

        flash(_("All forums marked as read."), "success")

        return redirect(url_for("forum.index"))
Esempio n. 40
0
    def post(self, forum_id=None, slug=None):
        # Mark a single forum as read
        if forum_id is not None:
            forum_instance = Forum.query.filter_by(id=forum_id).first_or_404()
            forumsread = ForumsRead.query.filter_by(
                user_id=real(current_user).id, forum_id=forum_instance.id
            ).first()
            TopicsRead.query.filter_by(
                user_id=real(current_user).id, forum_id=forum_instance.id
            ).delete()

            if not forumsread:
                forumsread = ForumsRead()
                forumsread.user = real(current_user)
                forumsread.forum = forum_instance

            forumsread.last_read = time_utcnow()
            forumsread.cleared = time_utcnow()

            db.session.add(forumsread)
            db.session.commit()

            flash(
                _(
                    "Forum %(forum)s marked as read.",
                    forum=forum_instance.title
                ), "success"
            )

            return redirect(forum_instance.url)

        # Mark all forums as read
        ForumsRead.query.filter_by(user_id=real(current_user).id).delete()
        TopicsRead.query.filter_by(user_id=real(current_user).id).delete()

        forums = Forum.query.all()
        forumsread_list = []
        for forum_instance in forums:
            forumsread = ForumsRead()
            forumsread.user = real(current_user)
            forumsread.forum = forum_instance
            forumsread.last_read = time_utcnow()
            forumsread.cleared = time_utcnow()
            forumsread_list.append(forumsread)

        db.session.add_all(forumsread_list)
        db.session.commit()

        flash(_("All forums marked as read."), "success")

        return redirect(url_for("forum.index"))
Esempio n. 41
0
def rss(key):
    'Personalized RSS feed'

    settings = SubscriptionSettings.query.filter(
        SubscriptionSettings.rss_key == key).first_or_404()
    user_id = settings.user_id
    user = User.query.get(user_id)
    categories = Category.get_all(user=real(user))
    allowed_forums = []

    for category, forums in categories:
        for forum, forumsread in forums:
            allowed_forums.append(forum.id)

    forums = [r.forum_id for r in Subscription.query.filter(
              (Subscription.user_id == user_id) &
              Subscription.forum_id.in_(allowed_forums)).all()]
    tracked = []

    if settings.tracked_topics:
        tracked = [r.topic_id for r in db.session.query(topictracker).filter(
                   text('topictracker.user_id == ' + str(user_id))).all()]

    url_root = (request.url_root[:-1] if request.url_root[-1] == '/'
                else request.url_root)
    feed = AtomFeed(_('Recent posts'), feed_url=request.url, url=url_root)
    posts = (Post.query.filter(Post.user_id != user_id)
             .join(Topic, Post.topic)
             .filter(Topic.id.in_(tracked) | Topic.forum_id.in_(forums))
             .order_by(Post.date_created.desc())
             .limit(20)
             .all())

    for post in posts:
        feed.add(_('{title} by {user}').format(title=post.topic.title,
                                               user=post.username),
                 markdown.render(post.content), content_type='html',
                 author=post.username, url=url_root + post.url,
                 updated=post.date_modified or post.date_created,
                 published=post.date_created)

    return feed.get_response()
Esempio n. 42
0
def new_post(topic_id, slug=None):
    topic = Topic.query.filter_by(id=topic_id).first_or_404()

    if not Permission(CanPostReply):
        flash(_("You do not have the permissions to post in this topic."),
              "danger")
        return redirect(topic.forum.url)

    form = ReplyForm()
    if form.validate_on_submit():
        if "preview" in request.form:
            return render_template("forum/new_post.html",
                                   topic=topic,
                                   form=form,
                                   preview=form.content.data)
        else:
            post = form.save(real(current_user), topic)
            return view_post(post.id)

    return render_template("forum/new_post.html", topic=topic, form=form)
Esempio n. 43
0
def new_post(topic_id, slug=None):
    topic = Topic.query.filter_by(id=topic_id).first_or_404()

    if not Permission(CanPostReply):
        flash(_("You do not have the permissions to post in this topic."),
              "danger")
        return redirect(topic.forum.url)

    form = ReplyForm()
    if form.validate_on_submit():
        if "preview" in request.form:
            return render_template(
                "forum/new_post.html", topic=topic,
                form=form, preview=form.content.data
            )
        else:
            post = form.save(real(current_user), topic)
            return view_post(post.id)

    return render_template("forum/new_post.html", topic=topic, form=form)
Esempio n. 44
0
    def post(self, topic_id, slug=None, post_id=None):
        topic = Topic.query.filter_by(id=topic_id).first_or_404()
        form = self.form()

        # check if topic exists
        if post_id is not None:
            post = Post.query.filter_by(id=post_id).first_or_404()

        if form.validate_on_submit():
            try:
                post = form.save(real(current_user), topic)
                return redirect(url_for("forum.view_post", post_id=post.id))
            except StopNewPost as e:
                flash(e.reason, "danger")
            except BaseFlaskBBError as e:
                flash(e.reason, "warning")
            except Exception:
                flash(_("Unrecoverable error while submitting new post"))

        return render_template("forum/new_post.html", topic=topic, form=form)
Esempio n. 45
0
    def post(self, post_id):
        post = Post.query.filter_by(id=post_id).first_or_404()
        form = self.form(obj=post)

        if form.validate_on_submit():
            if 'preview' in request.form:
                return render_template(
                    'forum/new_post.html',
                    topic=post.topic,
                    form=form,
                    preview=form.content.data,
                    edit_mode=True
                )
            else:
                form.populate_obj(post)
                post.date_modified = time_utcnow()
                post.modified_by = real(current_user).username
                post.save()
                return redirect(url_for('forum.view_post', post_id=post.id))

        return render_template('forum/new_post.html', topic=post.topic, form=form, edit_mode=True)
Esempio n. 46
0
def new_topic(forum_id, slug=None):
    forum_instance = Forum.query.filter_by(id=forum_id).first_or_404()

    if not Permission(CanPostTopic):
        flash(_("You do not have the permissions to create a new topic."),
              "danger")
        return redirect(forum_instance.url)

    form = NewTopicForm()
    if request.method == "POST":
        if "preview" in request.form and form.validate():
            return render_template(
                "forum/new_topic.html", forum=forum_instance,
                form=form, preview=form.content.data
            )
        if "submit" in request.form and form.validate():
            topic = form.save(real(current_user), forum_instance)
            # redirect to the new topic
            return redirect(url_for('forum.view_topic', topic_id=topic.id))

    return render_template(
        "forum/new_topic.html", forum=forum_instance, form=form
    )
Esempio n. 47
0
def edit_post(post_id):
    post = Post.query.filter_by(id=post_id).first_or_404()

    if not Permission(CanEditPost):
        flash(_("You do not have the permissions to edit this post."),
              "danger")
        return view_post(post.id)

    if post.first_post:
        form = NewTopicForm()
    else:
        form = ReplyForm()

    if form.validate_on_submit():
        if "preview" in request.form:
            return render_template(
                "forum/new_post.html", topic=post.topic,
                form=form, preview=form.content.data, edit_mode=True
            )
        else:
            form.populate_obj(post)
            post.date_modified = time_utcnow()
            post.modified_by = real(current_user).username
            post.save()

            if post.first_post:
                post.topic.title = form.title.data
                post.topic.save()
            return view_post(post.id)
    else:
        if post.first_post:
            form.title.data = post.topic.title

        form.content.data = post.content

    return render_template("forum/new_post.html", topic=post.topic, form=form,
                           edit_mode=True)
Esempio n. 48
0
def topictracker():
    page = request.args.get("page", 1, type=int)
    topics = real(current_user).tracked_topics.\
        outerjoin(TopicsRead,
                  db.and_(TopicsRead.topic_id == Topic.id,
                          TopicsRead.user_id == real(current_user).id)).\
        add_entity(TopicsRead).\
        order_by(Topic.last_updated.desc()).\
        paginate(page, flaskbb_config['TOPICS_PER_PAGE'], True)

    # bulk untracking
    if request.method == "POST":
        topic_ids = request.form.getlist("rowid")
        tmp_topics = Topic.query.filter(Topic.id.in_(topic_ids)).all()

        for topic in tmp_topics:
            real(current_user).untrack_topic(topic)
        real(current_user).save()

        flash(_("%(topic_count)s topics untracked.",
                topic_count=len(tmp_topics)), "success")
        return redirect(url_for("forum.topictracker"))

    return render_template("forum/topictracker.html", topics=topics)
Esempio n. 49
0
def manage_forum(forum_id, slug=None):
    page = request.args.get('page', 1, type=int)

    forum_instance, forumsread = Forum.get_forum(forum_id=forum_id,
                                                 user=real(current_user))

    # remove the current forum from the select field (move).
    available_forums = Forum.query.order_by(Forum.position).all()
    available_forums.remove(forum_instance)

    if not Permission(IsAtleastModeratorInForum(forum=forum_instance)):
        flash(_("You do not have the permissions to moderate this forum."),
              "danger")
        return redirect(forum_instance.url)

    if forum_instance.external:
        return redirect(forum_instance.external)

    topics = Forum.get_topics(
        forum_id=forum_instance.id, user=real(current_user), page=page,
        per_page=flaskbb_config["TOPICS_PER_PAGE"]
    )

    mod_forum_url = url_for("forum.manage_forum", forum_id=forum_instance.id,
                            slug=forum_instance.slug)

    # the code is kind of the same here but it somehow still looks cleaner than
    # doin some magic
    if request.method == "POST":
        ids = request.form.getlist("rowid")
        tmp_topics = Topic.query.filter(Topic.id.in_(ids)).all()

        if not len(tmp_topics) > 0:
            flash(_("In order to perform this action you have to select at "
                    "least one topic."), "danger")
            return redirect(mod_forum_url)

        # locking/unlocking
        if "lock" in request.form:
            changed = do_topic_action(topics=tmp_topics, user=real(current_user),
                                      action="locked", reverse=False)

            flash(_("%(count)s topics locked.", count=changed), "success")
            return redirect(mod_forum_url)

        elif "unlock" in request.form:
            changed = do_topic_action(topics=tmp_topics, user=real(current_user),
                                      action="locked", reverse=True)
            flash(_("%(count)s topics unlocked.", count=changed), "success")
            return redirect(mod_forum_url)

        # highlighting/trivializing
        elif "highlight" in request.form:
            changed = do_topic_action(topics=tmp_topics, user=real(current_user),
                                      action="important", reverse=False)
            flash(_("%(count)s topics highlighted.", count=changed), "success")
            return redirect(mod_forum_url)

        elif "trivialize" in request.form:
            changed = do_topic_action(topics=tmp_topics, user=real(current_user),
                                      action="important", reverse=True)
            flash(_("%(count)s topics trivialized.", count=changed), "success")
            return redirect(mod_forum_url)

        # deleting
        elif "delete" in request.form:
            changed = do_topic_action(topics=tmp_topics, user=real(current_user),
                                      action="delete", reverse=False)
            flash(_("%(count)s topics deleted.", count=changed), "success")
            return redirect(mod_forum_url)

        # moving
        elif "move" in request.form:
            new_forum_id = request.form.get("forum")

            if not new_forum_id:
                flash(_("Please choose a new forum for the topics."), "info")
                return redirect(mod_forum_url)

            new_forum = Forum.query.filter_by(id=new_forum_id).first_or_404()
            # check the permission in the current forum and in the new forum

            if not Permission(
                And(
                    IsAtleastModeratorInForum(forum_id=new_forum_id),
                    IsAtleastModeratorInForum(forum=forum_instance)
                )
            ):
                flash(_("You do not have the permissions to move this topic."),
                      "danger")
                return redirect(mod_forum_url)

            new_forum.move_topics_to(tmp_topics)
            return redirect(mod_forum_url)

    return render_template(
        "forum/edit_forum.html", forum=forum_instance, topics=topics,
        available_forums=available_forums, forumsread=forumsread,
    )
Esempio n. 50
0
    def post(self, forum_id, slug=None):  # noqa: C901
        forum_instance, __ = Forum.get_forum(
            forum_id=forum_id, user=real(current_user)
        )
        mod_forum_url = url_for(
            "forum.manage_forum",
            forum_id=forum_instance.id,
            slug=forum_instance.slug
        )

        ids = request.form.getlist("rowid")
        tmp_topics = Topic.query.filter(Topic.id.in_(ids)).all()

        if not len(tmp_topics) > 0:
            flash(
                _(
                    "In order to perform this action you have to select at "
                    "least one topic."
                ), "danger"
            )
            return redirect(mod_forum_url)

        # locking/unlocking
        if "lock" in request.form:
            changed = do_topic_action(
                topics=tmp_topics,
                user=real(current_user),
                action="locked",
                reverse=False
            )

            flash(_("%(count)s topics locked.", count=changed), "success")
            return redirect(mod_forum_url)

        elif "unlock" in request.form:
            changed = do_topic_action(
                topics=tmp_topics,
                user=real(current_user),
                action="locked",
                reverse=True
            )
            flash(_("%(count)s topics unlocked.", count=changed), "success")
            return redirect(mod_forum_url)

        # highlighting/trivializing
        elif "highlight" in request.form:
            changed = do_topic_action(
                topics=tmp_topics,
                user=real(current_user),
                action="important",
                reverse=False
            )
            flash(_("%(count)s topics highlighted.", count=changed), "success")
            return redirect(mod_forum_url)

        elif "trivialize" in request.form:
            changed = do_topic_action(
                topics=tmp_topics,
                user=real(current_user),
                action="important",
                reverse=True
            )
            flash(_("%(count)s topics trivialized.", count=changed), "success")
            return redirect(mod_forum_url)

        # deleting
        elif "delete" in request.form:
            changed = do_topic_action(
                topics=tmp_topics,
                user=real(current_user),
                action="delete",
                reverse=False
            )
            flash(_("%(count)s topics deleted.", count=changed), "success")
            return redirect(mod_forum_url)

        # moving
        elif "move" in request.form:
            new_forum_id = request.form.get("forum")

            if not new_forum_id:
                flash(_("Please choose a new forum for the topics."), "info")
                return redirect(mod_forum_url)

            new_forum = Forum.query.filter_by(id=new_forum_id).first_or_404()
            # check the permission in the current forum and in the new forum

            if not Permission(
                    And(IsAtleastModeratorInForum(forum_id=new_forum_id),
                        IsAtleastModeratorInForum(forum=forum_instance))):
                flash(
                    _("You do not have the permissions to move this topic."),
                    "danger"
                )
                return redirect(mod_forum_url)

            if new_forum.move_topics_to(tmp_topics):
                flash(_("Topics moved."), "success")
            else:
                flash(_("Failed to move topics."), "danger")

            return redirect(mod_forum_url)

        # hiding/unhiding
        elif "hide" in request.form:
            changed = do_topic_action(
                topics=tmp_topics,
                user=real(current_user),
                action="hide",
                reverse=False
            )
            flash(_("%(count)s topics hidden.", count=changed), "success")
            return redirect(mod_forum_url)

        elif "unhide" in request.form:
            changed = do_topic_action(
                topics=tmp_topics,
                user=real(current_user),
                action="unhide",
                reverse=False
            )
            flash(_("%(count)s topics unhidden.", count=changed), "success")
            return redirect(mod_forum_url)

        else:
            flash(_("Unknown action requested"), "danger")
            return redirect(mod_forum_url)
Esempio n. 51
0
def untrack_topic(topic_id, slug=None):
    topic = Topic.query.filter_by(id=topic_id).first_or_404()
    real(current_user).untrack_topic(topic)
    real(current_user).save()
    return redirect(topic.url)