示例#1
0
def new_topic(forum_id):
    forum = Forum.query.get(forum_id)

    if not forum:
        abort(404)

    user = g.user

    if user.forum_ban:
        abort(403)

    if forum.locked and not user.admin:
        abort(403)

    form = NewTopicForm()

    if form.validate_on_submit():
        title = form.title.data
        body = form.body.data
        image = form.image.data

        last_post = forum.last_post
        if last_post.body == body and last_post.user_id == user.id:
            return redirect(last_post.url)

        topic = ForumTopic(forum=forum, user=user, name=title)
        topic.save(commit=True)

        post = ForumPost(topic_id=topic.id, user=user, body=body, user_ip=request.remote_addr)
        post.save(commit=False)

        user.forum_profile.post_count += 1
        user.forum_profile.save(commit=False)

        forum.last_post = post
        forum.post_count += 1
        forum.topic_count += 1
        forum.save(commit=False)

        topic.last_post = post
        topic.post_count += 1
        topic.save(commit=True)

        if image:
            if not ForumAttachment.create_attachment(post.id, image, commit=True):
                flash('There was a problem with the upload', 'error')

        api.forum_post(user, topic.forum.name, topic.name, post.url, is_new_topic=True)

        return redirect(topic.url)

    retval = {
        'forum': forum,
        'form': form
    }

    return render_template('forums/new_topic.html', **retval)
示例#2
0
def forum_topic(topic_id):
    topic = ForumTopic.query.options(
        joinedload(ForumTopic.forum)
        .joinedload(Forum.category)
    ).get(topic_id)

    if not topic or topic.deleted:
        abort(404)

    user = g.user

    form = PostForm()

    if form.validate_on_submit():
        if not user:
            flash('You must log in first', 'warning')
            return redirect(url_for('login', next=request.path))

        if topic.deleted:
            flash('That topic no longer exists', 'warning')
            return redirect(url_for('forum', forum_id=topic.forum_id))

        if topic.closed:
            flash('That topic has been locked', 'warning')
            return redirect(url_for('forum', forum_id=topic.forum_id))

        if user.forum_ban:
            abort(403)

        body = form.body.data
        image = form.image.data
        subscribe = form.subscribe.data

        last_post = topic.last_post
        if last_post.body == body and last_post.user_id == user.id:
            return redirect(last_post.url)

        post = ForumPost(topic_id=topic.id, user=user, body=body, user_ip=request.remote_addr)

        user.forum_profile.post_count += 1
        user.forum_profile.save(commit=False)

        topic.forum.last_post = post
        topic.forum.post_count += 1
        topic.forum.save(commit=False)

        topic.updated = datetime.utcnow()
        topic.last_post = post
        topic.post_count += 1

        post.save(commit=True)

        if image:
            if not ForumAttachment.create_attachment(post.id, image, commit=True):
                flash('There was a problem with the upload', 'error')

        if subscribe:
            libforums.subscribe_to_topic(user, topic, commit=True)

        if not topic.forum.category.collapsed:
            api.forum_post(user, topic.forum.name, topic.name, post.url, is_new_topic=False)

        create_subscribed_post_notifications(post)

        return redirect(post.url)

    page = request.args.get('p')

    page_size = POSTS_PER_PAGE

    try:
        page = max(int(page), 1) if page else 1
    except:
        page = 1

    if page < 1 or page > topic.post_count / page_size + 1:
        return redirect(url_for('forum_topic', topic_id=topic_id))

    posts = ForumPost.query.options(
        joinedload(ForumPost.user)
        .joinedload(User.player)
    ).options(
        joinedload(ForumPost.user)
        .joinedload(User.forum_profile)
    ).options(
        joinedload(ForumPost.user)
        .joinedload(User.forum_ban)
    ).options(
        joinedload(ForumPost.attachments)
    ).options(
        joinedload(ForumPost.topic)
        .joinedload(ForumTopic.forum)
    ).filter_by(topic_id=topic_id, deleted=False) \
        .order_by(ForumPost.created) \
        .limit(page_size) \
        .offset((page - 1) * page_size)

    player_ids = set([post.user.player_id for post in posts])

    player_stats = PlayerStats.query.filter(
        PlayerStats.server_id == app.config['MAIN_SERVER_ID'],
        PlayerStats.player_id.in_(player_ids)
    )

    player_stats = {
        stats.player: stats
        for stats in player_stats
    }

    subscription = None

    if user:
        notifications.mark_notifications_read(
            user,
            [{'post_id': post.id} for post in posts],
            allow_commit=False
        )
        topic.update_read(user)

        subscription = ForumTopicSubscription.query.filter_by(
            user=user,
            topic=topic
        ).first()

    retval = {
        'topic': topic,
        'posts': posts,
        'player_stats': player_stats,
        'page_size': page_size,
        'page': page,
        'form': form,
        'subscription': subscription
    }

    return render_template('forums/topic.html', **retval)
示例#3
0
def new_topic(forum_id):
    forum = Forum.query.options(joinedload(Forum.category)).get(forum_id)

    if not forum:
        abort(404)

    user = g.user

    if user.forum_ban:
        abort(403)

    if forum.locked and not user.admin:
        abort(403)

    can_post = libforums.can_user_post(user)

    if not can_post:
        flash(
            'You need to spend more time on the server before being able to post!',
            'warning')
        return redirect(url_for('forum', forum_id=forum_id))

    form = NewTopicForm()

    if form.validate_on_submit():
        title = form.title.data
        body = form.body.data
        image = form.image.data
        email_all = form.email_all.data
        subscribe = form.subscribe.data

        last_post = forum.last_post
        if last_post and last_post.body == body and last_post.user_id == user.id:
            return redirect(last_post.url)

        topic = ForumTopic(forum=forum, user=user, name=title)
        topic.save(commit=True)

        post = ForumPost(topic_id=topic.id,
                         user=user,
                         body=body,
                         user_ip=request.remote_addr)
        post.save(commit=False)

        user.forum_profile.post_count += 1
        user.forum_profile.save(commit=False)

        forum.last_post = post
        forum.post_count += 1
        forum.topic_count += 1
        forum.save(commit=False)

        topic.last_post = post
        topic.post_count += 1
        topic.save(commit=True)

        if image:
            if not ForumAttachment.create_attachment(
                    post.id, image, commit=True):
                flash('There was a problem with the upload', 'error')

        if user.admin and forum.locked:
            create_news_post_notifications(post, email_all=email_all)

        if subscribe:
            libforums.subscribe_to_topic(user, topic, commit=True)

        if libforums.should_notify_post(user, topic, post):
            api.forum_post(user,
                           topic.forum.name,
                           topic.name,
                           post.url,
                           is_new_topic=True)

        return redirect(topic.url)

    retval = {'forum': forum, 'form': form}

    return render_template('forums/new_topic.html', **retval)
示例#4
0
def new_topic(forum_id):
    forum = Forum.query.options(
        joinedload(Forum.category)
    ).get(forum_id)

    if not forum:
        abort(404)

    user = g.user

    if user.forum_ban:
        abort(403)

    if forum.locked and not user.admin:
        abort(403)

    can_post = libforums.can_user_post(user)

    if not can_post:
        flash('You need to spend more time on the server before being able to post!', 'warning')
        return redirect(url_for('forum', forum_id=forum_id))

    form = NewTopicForm()

    if form.validate_on_submit():
        title = form.title.data
        body = form.body.data
        image = form.image.data
        email_all = form.email_all.data
        subscribe = form.subscribe.data

        last_post = forum.last_post
        if last_post and last_post.body == body and last_post.user_id == user.id:
            return redirect(last_post.url)

        topic = ForumTopic(forum=forum, user=user, name=title)
        topic.save(commit=True)

        post = ForumPost(topic_id=topic.id, user=user, body=body, user_ip=request.remote_addr)
        post.save(commit=False)

        user.forum_profile.post_count += 1
        user.forum_profile.save(commit=False)

        forum.last_post = post
        forum.post_count += 1
        forum.topic_count += 1
        forum.save(commit=False)

        topic.last_post = post
        topic.post_count += 1
        topic.save(commit=True)

        if image:
            if not ForumAttachment.create_attachment(post.id, image, commit=True):
                flash('There was a problem with the upload', 'error')

        if user.admin and forum.locked:
            create_news_post_notifications(post, email_all=email_all)

        if subscribe:
            libforums.subscribe_to_topic(user, topic, commit=True)

        if libforums.should_notify_post(user, topic, post):
            api.forum_post(user, topic.forum.name, topic.name, post.url, is_new_topic=True)

        return redirect(topic.url)

    retval = {
        'forum': forum,
        'form': form
    }

    return render_template('forums/new_topic.html', **retval)
示例#5
0
def forum_topic(topic_id):
    topic = ForumTopic.query.options(
        joinedload(ForumTopic.forum).joinedload(Forum.category)).get(topic_id)

    if not topic or topic.deleted:
        abort(404)

    user = g.user

    form = PostForm()

    can_user_post = libforums.can_user_post(user)

    if form.validate_on_submit():
        if not user:
            flash('You must log in first', 'warning')
            return redirect(url_for('login', next=request.path))

        if topic.deleted:
            flash('That topic no longer exists', 'warning')
            return redirect(url_for('forum', forum_id=topic.forum_id))

        if topic.closed:
            flash('That topic has been locked', 'warning')
            return redirect(url_for('forum', forum_id=topic.forum_id))

        if user.forum_ban:
            abort(403)

        if not can_user_post:
            abort(403)

        body = form.body.data
        image = form.image.data
        subscribe = form.subscribe.data

        last_post = topic.last_post
        if last_post.body == body and last_post.user_id == user.id:
            return redirect(last_post.url)

        post = ForumPost(topic_id=topic.id,
                         user=user,
                         body=body,
                         user_ip=request.remote_addr)

        user.forum_profile.post_count += 1
        user.forum_profile.save(commit=False)

        topic.forum.last_post = post
        topic.forum.post_count += 1
        topic.forum.save(commit=False)

        topic.updated = datetime.utcnow()
        topic.last_post = post
        topic.post_count += 1

        post.save(commit=True)

        if image:
            if not ForumAttachment.create_attachment(
                    post.id, image, commit=True):
                flash('There was a problem with the upload', 'error')

        if subscribe:
            libforums.subscribe_to_topic(user, topic, commit=True)

        if libforums.should_notify_post(user, topic, post):
            api.forum_post(user,
                           topic.forum.name,
                           topic.name,
                           post.url,
                           is_new_topic=False)

        create_subscribed_post_notifications(post)

        return redirect(post.url)

    page = request.args.get('p')

    page_size = POSTS_PER_PAGE

    try:
        page = max(int(page), 1) if page else 1
    except:
        page = 1

    if page < 1 or page > topic.post_count / page_size + 1:
        return redirect(url_for('forum_topic', topic_id=topic_id))

    posts = ForumPost.query.options(
        joinedload(ForumPost.user).joinedload(User.player)).options(
            joinedload(ForumPost.user).joinedload(User.forum_profile)).options(
                joinedload(ForumPost.user).joinedload(User.forum_ban)).options(
                    joinedload(ForumPost.attachments)).options(
                        joinedload(ForumPost.topic).joinedload(
                            ForumTopic.forum)).options(
                                joinedload(ForumPost.votes).joinedload(
                                    ForumPostVote.user).joinedload(User.player)
                            ).filter(
                                ForumPost.topic_id == topic_id,
                                ForumPost.deleted == False).order_by(
                                    ForumPost.created).limit(page_size).offset(
                                        (page - 1) * page_size)

    player_ids = set([post.user.player_id for post in posts])

    player_stats = PlayerStats.query.options(joinedload(
        PlayerStats.player)).options(joinedload(PlayerStats.group)).filter(
            PlayerStats.server_id == app.config['MAIN_SERVER_ID'],
            PlayerStats.player_id.in_(player_ids))

    player_stats = {stats.player: stats for stats in player_stats}

    subscription = None
    votes = None

    if user:
        notifications.mark_notifications_read(user, [{
            'post_id': post.id
        } for post in posts],
                                              allow_commit=False)
        topic.update_read(user)

        subscription = ForumTopicSubscription.query.filter_by(
            user=user, topic=topic).first()

        all_votes = ForumPostVote.query.filter(
            ForumPostVote.user_id == user.id,
            ForumPostVote.post_id.in_(post.id for post in posts))

        votes = {
            forum_post_vote.post_id: forum_post_vote.vote
            for forum_post_vote in all_votes
        }

    retval = {
        'topic': topic,
        'posts': posts,
        'player_stats': player_stats,
        'page_size': page_size,
        'page': page,
        'form': form,
        'subscription': subscription,
        'votes': votes,
        'can_user_post': can_user_post
    }

    return render_template('forums/topic.html', **retval)