annotate gpp/forums/views/attachments.py @ 286:72fd300685d5

For #95. You can now make posts with no text in the body if you have attachments. And now if you create a new topic with an attachment, and the POST fails (say you forgot the topic title), we will now re-attach attachments. Also fixed a bug in the smiley code that would arise if it was asked to markup an empty string.
author Brian Neal <bgneal@gmail.com>
date Sat, 23 Oct 2010 20:19:46 +0000
parents 8fd4984d5c3b
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