bgneal@285: """
bgneal@285: This module contains views for working with post attachments.
bgneal@285: """
bgneal@285: from django.http import HttpResponse
bgneal@285: from django.http import HttpResponseForbidden
bgneal@285: from django.http import HttpResponseBadRequest
bgneal@285: from django.http import HttpResponseNotFound
bgneal@285: import django.utils.simplejson as json
bgneal@285: 
bgneal@285: from forums.models import Post
bgneal@285: 
bgneal@285: 
bgneal@285: def fetch_attachments(request):
bgneal@285:     """
bgneal@285:     This view is the target of an AJAX GET request to retrieve the
bgneal@285:     attachment embed data for a given forum post.
bgneal@285: 
bgneal@285:     """
bgneal@285:     if not request.user.is_authenticated():
bgneal@285:         return HttpResponseForbidden('Please login or register.')
bgneal@285: 
bgneal@285:     post_id = request.GET.get('pid')
bgneal@285:     if post_id is None:
bgneal@285:         return HttpResponseBadRequest('Missing post ID.')
bgneal@285: 
bgneal@285:     try:
bgneal@285:         post = Post.objects.get(pk=post_id)
bgneal@285:     except Post.DoesNotExist:
bgneal@285:         return HttpResponseNotFound("That post doesn't exist.")
bgneal@286: 
bgneal@285:     embeds = post.attachments.all().select_related('embed')
bgneal@285:     data = [{'id': embed.id, 'html': embed.html} for embed in embeds]
bgneal@285: 
bgneal@285:     return HttpResponse(json.dumps(data), content_type='application/json')
bgneal@285: