Project

General

Profile

Task #2952 » 01-askapi_comments.patch

AskBot API: Expose comments - Guilhem Moulin, 2021-03-30 13:09

View differences:

askbot/doc/source/api.rst
48 48
`/api/v1/answers/<answer_id>/`
49 49
----------------------------------
50 50
Returns data about individual answer
51

  
52
`/api/v1/comments/<comment_id>/`
53
----------------------------------
54
Returns data about individual comment
51
-- a/askbot/urls.py
55
++ b/askbot/urls.py
......
715 715
    url('^api/v1/questions/$', views.api_v1.questions, name='api_v1_questions'),
716 716
    url('^api/v1/questions/(?P<question_id>\d+)/$', views.api_v1.question, name='api_v1_question'),
717 717
    url('^api/v1/answers/(?P<answer_id>\d+)/$', views.api_v1.answer, name='api_v1_answer'),
718
    url('^api/v1/comments/(?P<comment_id>\d+)/$', views.api_v1.comment, name='api_v1_comment'),
718 719
)
719 720

  
720 721
if 'askbot.deps.django_authopenid' in settings.INSTALLED_APPS:
721
-- a/askbot/models/question.py
722
++ b/askbot/models/question.py
......
784 784
        answers = self.get_answers(user=user)
785 785
        return [answer.id for answer in answers]
786 786

  
787
    def get_comment_ids(self, parent=None):
788
        """give the ids to all the comments"""
789
        comments = self.get_comments(parent=parent).order_by('added_at')
790
        return [comment.id for comment in comments]
791

  
787 792
    def get_latest_revision(self, user=None):
788 793
        #todo: add denormalized field to Thread model
789 794
        from askbot.models import Post, PostRevision
......
1093 1098
            #                | models.Q(deleted_by=user)
1094 1099
            #            )
1095 1100

  
1101
    def get_comments(self, parent=None):
1102
        """returns query set for comments to this question"""
1103
        if parent is None:
1104
            return self.posts.get_comments().filter(deleted=False)
1105
        else:
1106
            return self.posts.get_comments().filter(parent_id=parent, deleted=False)
1107

  
1096 1108
    def invalidate_cached_summary_html(self):
1097 1109
        """Invalidates cached summary html in all activated languages"""
1098 1110
        langs = translation_utils.get_language_codes()
1099
-- a/askbot/views/api_v1.py
1111
++ b/askbot/views/api_v1.py
......
38 38
        'answer_count': thread.answer_count,
39 39
        'answer_ids': thread.get_answer_ids(),
40 40
        'accepted_answer_id': thread.accepted_answer_id,
41
        'comment_count': thread.get_comments(parent=question_post.id).count(),
42
        'comment_ids': thread.get_comment_ids(parent=question_post.id),
41 43
        'view_count': thread.view_count,
42 44
        'score': thread.score,
43 45
        'last_activity_at': get_epoch_str(thread.last_activity_at),
......
58 60

  
59 61
def get_answer_data(post):
60 62
    """returns data dictionary for a given answer post"""
63
    post_id = post.id
64
    post_thread = post.thread
61 65
    datum = {
62 66
        'added_at': post.added_at.strftime('%s'),
63
        'id': post.id,
67
        'id': post_id,
64 68
        'score': post.score,
65 69
        'summary': post.summary,
66 70
        'url': site_url(post.get_absolute_url()),
71
        'comment_count': post_thread.get_comments(parent=post_id).count(),
72
        'comment_ids': post_thread.get_comment_ids(parent=post_id),
67 73
    }
68 74
    datum['author'] = {
69 75
        'id': post.author.id,
......
236 242
    }
237 243
    response_data = simplejson.dumps(ajax_data)
238 244
    return HttpResponse(response_data, content_type='application/json')
245

  
246
def comment(request, comment_id):
247
    '''
248
    Gets a single comment
249
    '''
250
    #we retrieve comment by comment id, b/c that's what is in the url,
251
    post = get_object_or_404(
252
        models.Post, id=comment_id,
253
        post_type='comment', deleted=False
254
    )
255
    datum = get_answer_data(post)
256
    json_string = simplejson.dumps(datum)
257
    return HttpResponse(json_string, content_type='application/json')
(1-1/6)