comparison forums/views/attachments.py @ 581:ee87ea74d46b

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