Source

reddish / reddish / controllers / community.py

import logging

from pylons import tmpl_context as c

from ..lib.base import BaseController, get, post
from ..lib.errors import ClientError
from ..lib import helpers as h, validator as v
from .. import model

log = logging.getLogger(__name__)

class CommunityController(BaseController):

    @post
    @v.validate(v.LoggedIn, cmty = v.Moderates('community'),
        user = v.User('user'), reason = v.Field('reason'))
    def ban(self, cmty, user, reason):
        cmty.add_ban(c.user, user, reason)
        return self._render({})

    @get
    @v.validate(v.LoggedIn, cmty = v.Moderates('community'))
    def bans(self, cmty):
        return self._render('community/bans', { 'bans': cmty.get_bans() })

    @get
    @v.validate(cmty = v.Community('community'), post = v.Post('post'),
        limit = v.IntRange('limit', min = 1, max = 500,
            required = False, default = 500))
    def comments(self, cmty, post, limit):
        post.belongs_to(cmty)
        c.active_tab = 'comments'
        return self._render('community/comments',
            { 'post': post, 'comments': post.get_comments(c.user, limit) })

    @get
    @v.validate(v.LoggedIn, cmty = v.Moderates('community'))
    def contributors(self, cmty):
        return self._render('community/contributors',
            { 'contributors': cmty.get_contributors() })

    @get
    @v.validate(v.LoggedIn)
    def create(self):
        return self._render('community/create', {})

    @create.post
    @v.validate(v.LoggedIn, cmty = v.CommunitySpec())
    def create(self, cmty):
        cmty.create(c.user)
        return self._render({ 'call': ['community_new', h.url('/g/' + cmty.name)] })

    @get
    @v.validate(cmty = v.Community('community'), post = v.Post('post'))
    def duplicates(self, cmty, post):
        post.belongs_to(cmty)
        c.active_tab = 'other discussions'
        return self._render('community/duplicates',
            { 'post': post, 'posts': h.page(post.get_other_discussions(c.user)) })

    @get
    @v.validate(v.LoggedIn, cmty = v.Moderates('community'))
    def edit(self, cmty):
        return self._render('community/edit', { 'community': cmty })

    @edit.post
    @v.validate(v.LoggedIn, cmty = v.Moderates('community'),
        updater = v.CommunityEdit())
    def edit(self, cmty, updater):
        updater.update(cmty)
        cmty.log(c.user, 'edit')
        return self._render({ 'call': ['set_status', 'saved'] })

    @get
    @v.validate(cmty = v.Community('community'))
    def info(self, cmty):
        return self._render({ 'community': cmty })

    @get
    @v.validate(cmty = v.Community('community'))
    def latest(self, cmty):
        c.active_tab = 'latest'
        return self._render('community/index',
            { 'posts': h.page(cmty.get_posts(c.user, 'latest')) })

    @get
    @v.validate(v.LoggedIn, cmty = v.Moderates('community'))
    def log(self, cmty):
        return self._render('community/log', { 'logs': h.page(cmty.get_logs()) })

    @get
    @v.validate(cmty = v.Community('community'))
    def moderators(self, cmty):
        return self._render('community/moderators',
            { 'moderators': cmty.get_moderators() })

    @get
    @v.validate(cmty = v.Community('community'))
    def new(self, cmty):
        c.active_tab = 'new'
        return self._render('community/index',
            { 'posts': h.page(cmty.get_posts(c.user, 'new')) })

    @get
    def reports(self):
        return self.post_reports()

    @get
    @v.validate(v.LoggedIn, cmty = v.Moderates('community'))
    def comment_reports(self, cmty):
        c.active_tab = 'comments'
        return self._render('community/comment_reports',
            { 'comments': h.page(cmty.get_reported_comments()) })

    @get
    @v.validate(v.LoggedIn, cmty = v.Moderates('community'))
    def post_reports(self, cmty):
        c.active_tab = 'posts'
        return self._render('community/post_reports',
            { 'posts': h.page(cmty.get_reported_posts()) })

    @get
    def removed(self):
        return self.removed_posts()

    @get
    @v.validate(v.LoggedIn, cmty = v.Moderates('community'))
    def removed_comments(self, cmty):
        c.active_tab = 'removed comments'
        return self._render('community/removed_comments',
            { 'comments': h.page(cmty.get_removed_comments()) })

    @get
    @v.validate(v.LoggedIn, cmty = v.Moderates('community'))
    def removed_posts(self, cmty):
        c.active_tab = 'removed posts'
        return self._render('community/removed_posts',
            { 'posts': h.page(cmty.get_removed_posts()) })

    @get
    @v.validate(cmty = v.Community('community', required = False),
        query = v.Field('q', required = False),
        match = v.Field('match', required = False),
        here = v.Bool('here_only'),
        sort = v.Field('sort', required = False))
    def search(self, cmty, query, match, here, sort):
        if query:
            result = None

            if '://' in query:
                try:
                    link = h.URL(query)
                except ClientError:
                    pass
                else:
                    result = model.Post.by_link(link)

            if result is None:
                result = model.Post.search(h.keywords(query),
                    c.user, sort, match == 'any')

            if here and cmty:
                result.filter({ 'community': cmty._id })

            return self._render('community/search', { 'posts': result })
        return self._render('community/search', {})

    @get
    @v.validate(v.LoggedIn, cmty = v.Community('community', required = False))
    def submit(self, cmty):
        return self._render('community/submit', {})

    @get
    @v.validate(cmty = v.Community('community'))
    def top(self, cmty):
        c.active_tab = 'top'
        return self._render('community/index',
            { 'posts': h.page(cmty.get_posts(c.user, 'top')) })

    @post
    @v.validate(v.LoggedIn, cmty = v.Moderates('community'), user = v.User('user'))
    def unban(self, cmty, user):
        cmty.remove_ban(c.user, user)
        return self._render({ 'call': ['ban_removed', user.name] })

    @get
    @v.validate(cmty = v.Community('community'),
        post = v.Post('post'), comment = v.Comment('comment'),
        context = v.IntRange('context', min = 0, max = 9,
            required = False, default = 0),
        limit = v.IntRange('limit', min = 1, max = 500,
            required = False, default = 100))
    def view_comment(self, cmty, post, comment, context, limit):
        post.belongs_to(cmty)
        comment.belongs_to(cmty)

        c.hilight_comment = comment

        comment = comment.get_context(c.user, context, limit = limit)
        c.comment_context = context

        return self._render('community/comments',
            { 'post': post, 'comments': [comment] })

    @get
    @v.validate(v.LoggedIn, cmty = v.Moderates('community'),
        comment = v.Comment('comment'))
    def view_comment_reports(self, cmty, comment):
        comment.belongs_to(cmty)
        return self._render('community/view_comment_reports',
            { 'comment': comment, 'reports': h.page(comment.get_reports()) })

    @get
    @v.validate(v.LoggedIn, cmty = v.Moderates('community'),
        post = v.Post('post'))
    def view_post_reports(self, cmty, post):
        post.belongs_to(cmty)
        return self._render('community/view_post_reports',
            { 'post': post, 'reports': h.page(post.get_reports()) })