diff --git a/boards/urls.py b/boards/urls.py --- a/boards/urls.py +++ b/boards/urls.py @@ -63,5 +63,7 @@ urlpatterns = patterns('', url(r'^api/threads/(?P\w+)/$', api.api_get_threads, name='get_threads'), url(r'api/tags/$', api.api_get_tags, name='get_tags'), + url(r'api/thread/(?P\w+)/$', api.api_get_thread_posts, + name='get_thread'), ) diff --git a/boards/views/api.py b/boards/views/api.py --- a/boards/views/api.py +++ b/boards/views/api.py @@ -98,6 +98,7 @@ def get_post(request, post_id): return render(request, 'boards/post.html', context) +# TODO Test this def api_get_threads(request, count): """ Get the JSON thread opening posts list. @@ -142,6 +143,7 @@ def api_get_threads(request, count): return HttpResponse(content=json.dumps(opening_posts)) +# TODO Test this def api_get_tags(request): """ Get all tags or user tags. @@ -155,3 +157,33 @@ def api_get_tags(request): tag_names.append(tag.name) return HttpResponse(content=json.dumps(tag_names)) + + +# TODO The result can be cached by the thread last update time +# TODO Test this +def api_get_thread_posts(request, opening_post_id): + """ + Get the JSON array of thread posts + """ + + opening_post = get_object_or_404(Post, id=opening_post_id) + thread = opening_post.thread_new + posts = thread.get_replies() + + json_post_list = [] + + for post in posts: + # TODO Add pub time and replies + post_json = { + 'id' : post.id, + 'title' : post.title, + 'text' : post.text.rendered, + } + if post.image: + post_json += { + 'image' : post.image.url, + 'image_preview' : post.image.url_200x150, + } + json_post_list.append(post_json) + + return HttpResponse(content=json.dumps(json_post_list))