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
`/api/v1/answers/<answer_id>/`
----------------------------------
Returns data about individual answer
`/api/v1/comments/<comment_id>/`
----------------------------------
Returns data about individual comment
-- a/askbot/urls.py
++ b/askbot/urls.py
......
url('^api/v1/questions/$', views.api_v1.questions, name='api_v1_questions'),
url('^api/v1/questions/(?P<question_id>\d+)/$', views.api_v1.question, name='api_v1_question'),
url('^api/v1/answers/(?P<answer_id>\d+)/$', views.api_v1.answer, name='api_v1_answer'),
url('^api/v1/comments/(?P<comment_id>\d+)/$', views.api_v1.comment, name='api_v1_comment'),
)
if 'askbot.deps.django_authopenid' in settings.INSTALLED_APPS:
-- a/askbot/models/question.py
++ b/askbot/models/question.py
......
answers = self.get_answers(user=user)
return [answer.id for answer in answers]
def get_comment_ids(self, parent=None):
"""give the ids to all the comments"""
comments = self.get_comments(parent=parent).order_by('added_at')
return [comment.id for comment in comments]
def get_latest_revision(self, user=None):
#todo: add denormalized field to Thread model
from askbot.models import Post, PostRevision
......
# | models.Q(deleted_by=user)
# )
def get_comments(self, parent=None):
"""returns query set for comments to this question"""
if parent is None:
return self.posts.get_comments().filter(deleted=False)
else:
return self.posts.get_comments().filter(parent_id=parent, deleted=False)
def invalidate_cached_summary_html(self):
"""Invalidates cached summary html in all activated languages"""
langs = translation_utils.get_language_codes()
-- a/askbot/views/api_v1.py
++ b/askbot/views/api_v1.py
......
'answer_count': thread.answer_count,
'answer_ids': thread.get_answer_ids(),
'accepted_answer_id': thread.accepted_answer_id,
'comment_count': thread.get_comments(parent=question_post.id).count(),
'comment_ids': thread.get_comment_ids(parent=question_post.id),
'view_count': thread.view_count,
'score': thread.score,
'last_activity_at': get_epoch_str(thread.last_activity_at),
......
def get_answer_data(post):
"""returns data dictionary for a given answer post"""
post_id = post.id
post_thread = post.thread
datum = {
'added_at': post.added_at.strftime('%s'),
'id': post.id,
'id': post_id,
'score': post.score,
'summary': post.summary,
'url': site_url(post.get_absolute_url()),
'comment_count': post_thread.get_comments(parent=post_id).count(),
'comment_ids': post_thread.get_comment_ids(parent=post_id),
}
datum['author'] = {
'id': post.author.id,
......
}
response_data = simplejson.dumps(ajax_data)
return HttpResponse(response_data, content_type='application/json')
def comment(request, comment_id):
'''
Gets a single comment
'''
#we retrieve comment by comment id, b/c that's what is in the url,
post = get_object_or_404(
models.Post, id=comment_id,
post_type='comment', deleted=False
)
datum = get_answer_data(post)
json_string = simplejson.dumps(datum)
return HttpResponse(json_string, content_type='application/json')
(1-1/6)