annotate gpp/forums/views/attachments.py @ 580:c525f3e0b5d0

Use the static() function to serve MEDIA files in development.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 May 2012 15:08:07 -0500
parents 72fd300685d5
children
rev   line source
bgneal@285 1 """
bgneal@285 2 This module contains views for working with post attachments.
bgneal@285 3 """
bgneal@285 4 from django.http import HttpResponse
bgneal@285 5 from django.http import HttpResponseForbidden
bgneal@285 6 from django.http import HttpResponseBadRequest
bgneal@285 7 from django.http import HttpResponseNotFound
bgneal@285 8 import django.utils.simplejson as json
bgneal@285 9
bgneal@285 10 from forums.models import Post
bgneal@285 11
bgneal@285 12
bgneal@285 13 def fetch_attachments(request):
bgneal@285 14 """
bgneal@285 15 This view is the target of an AJAX GET request to retrieve the
bgneal@285 16 attachment embed data for a given forum post.
bgneal@285 17
bgneal@285 18 """
bgneal@285 19 if not request.user.is_authenticated():
bgneal@285 20 return HttpResponseForbidden('Please login or register.')
bgneal@285 21
bgneal@285 22 post_id = request.GET.get('pid')
bgneal@285 23 if post_id is None:
bgneal@285 24 return HttpResponseBadRequest('Missing post ID.')
bgneal@285 25
bgneal@285 26 try:
bgneal@285 27 post = Post.objects.get(pk=post_id)
bgneal@285 28 except Post.DoesNotExist:
bgneal@285 29 return HttpResponseNotFound("That post doesn't exist.")
bgneal@286 30
bgneal@285 31 embeds = post.attachments.all().select_related('embed')
bgneal@285 32 data = [{'id': embed.id, 'html': embed.html} for embed in embeds]
bgneal@285 33
bgneal@285 34 return HttpResponse(json.dumps(data), content_type='application/json')
bgneal@285 35