comment.py 1.45 KB
Newer Older
1
from .utils import *
Rocky Duan committed
2

3
from .thread import Thread
Rocky Duan committed
4 5 6
import models
import settings

Calen Pennington committed
7

Rocky Duan committed
8 9 10
class Comment(models.Model):

    accessible_fields = [
11 12 13 14
        'id', 'body', 'anonymous', 'anonymous_to_peers', 'course_id',
        'endorsed', 'parent_id', 'thread_id', 'username', 'votes', 'user_id',
        'closed', 'created_at', 'updated_at', 'depth', 'at_position_list',
        'type', 'commentable_id',
Rocky Duan committed
15 16 17
    ]

    updatable_fields = [
18
        'body', 'anonymous', 'anonymous_to_peers', 'course_id', 'closed',
Rocky Duan committed
19 20 21 22 23 24 25 26
        'user_id', 'endorsed',
    ]

    initializable_fields = updatable_fields

    base_url = "{prefix}/comments".format(prefix=settings.PREFIX)
    type = 'comment'

Rocky Duan committed
27 28 29 30
    @property
    def thread(self):
        return Thread(id=self.thread_id, type='thread')

Rocky Duan committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44
    @classmethod
    def url_for_comments(cls, params={}):
        if params.get('thread_id'):
            return _url_for_thread_comments(params['thread_id'])
        else:
            return _url_for_comment(params['parent_id'])

    @classmethod
    def url(cls, action, params={}):
        if action in ['post']:
            return cls.url_for_comments(params)
        else:
            return super(Comment, cls).url(action, params)

Calen Pennington committed
45

Rocky Duan committed
46 47 48
def _url_for_thread_comments(thread_id):
    return "{prefix}/threads/{thread_id}/comments".format(prefix=settings.PREFIX, thread_id=thread_id)

Calen Pennington committed
49

Rocky Duan committed
50 51
def _url_for_comment(comment_id):
    return "{prefix}/comments/{comment_id}".format(prefix=settings.PREFIX, comment_id=comment_id)