def __init__(self, *args, **kwargs): RedirectForm.__init__(self, *args, **kwargs) self.user = None
def thread_mod_action(forum_slug, thread_id, thread_slug, action): # This view is becoming spaghetti. url_forum = Forum.query.filter_by(slug=forum_slug).first() thread = Thread.query.get(thread_id) if not thread: abort(404) if thread.is_private: abort(404) forum = thread.forum requires_moderator = True if action == 'sticky': url = thread.sticky_url elif action == 'lock': url = thread.lock_url elif action == 'follow': if not thread.can_be_followed: abort(404) url = thread.follow_url requires_moderator = False if requires_moderator: if not forum.can_be_moderated_by(g.user): abort(403) else: if not forum.can_be_viewed_by(g.user): abort(403) if request.method == 'GET' and (url_forum != thread.forum or thread_slug != thread.slug): return redirect(url, code=301) ajax = ('ajax' in request.values) form = RedirectForm() if action == 'sticky': old_value = thread.is_stickied attr = 'is_stickied' if old_value: cap_verb = 'Unstick' done_word = 'unstickied' link_title = 'Stick' message = 'Are you sure you want to make this thread a normal thread?' else: cap_verb = 'Stick' done_word = 'stickied' link_title = 'Unstick' message = 'Are you sure you want to make this thread a sticky?' elif action == 'lock': old_value = thread.is_locked attr = 'is_locked' if old_value: cap_verb = 'Unlock' done_word = 'unlocked' link_title = 'Lock' message = 'Are you sure you want to unlock this thread?' else: cap_verb = 'Lock' done_word = 'locked' link_title = 'Unlock' message = 'Are you sure you want to lock this thread?' elif action == 'follow': old_value = thread.is_followed_by(g.user) attr = None if old_value: cap_verb = 'Follow' link_title = 'Follow' message = 'Are you sure you want to follow this thread?' else: cap_verb = 'Unfollow' link_title = 'Unfollow' message = 'Are you sure you want to stop following this thread?' if request.method == 'POST': if action == 'follow': if old_value: thread.followers.remove(g.user) thread.follower_count -= 1 msg = 'You are no longer following this thread.' else: thread.followers.append(g.user) thread.follower_count += 1 msg = 'You are now following this thread. You will receive notifications about new posts.' else: setattr(thread, attr, not old_value) msg = 'The thread has been %s.' % done_word db.session.commit() if ajax: return jsonify(toast=msg, link_title=link_title) else: flash(msg) return form.redirect(url=thread.url) else: return render_template('confirm.html', form=form, crumbs_type='thread', forum=forum, thread=thread, final_crumb=('%s Thread' % cap_verb), message=message, url=url)