annotate forums/views/attachments.py @ 1145:a469445d8be3

Convert fixed pages to V3 design.
author Brian Neal <bgneal@gmail.com>
date Mon, 14 Nov 2016 19:40:36 -0600
parents 7e0c3cbd3cda
children
rev   line source
bgneal@285 1 """
bgneal@285 2 This module contains views for working with post attachments.
bgneal@679 3
bgneal@285 4 """
bgneal@679 5 import json
bgneal@679 6
bgneal@285 7 from django.http import HttpResponse
bgneal@285 8 from django.http import HttpResponseForbidden
bgneal@285 9 from django.http import HttpResponseBadRequest
bgneal@285 10 from django.http import HttpResponseNotFound
bgneal@285 11
bgneal@1037 12 from forums.models import Attachment
bgneal@285 13 from forums.models import Post
bgneal@285 14
bgneal@285 15
bgneal@285 16 def fetch_attachments(request):
bgneal@285 17 """
bgneal@285 18 This view is the target of an AJAX GET request to retrieve the
bgneal@285 19 attachment embed data for a given forum post.
bgneal@285 20
bgneal@285 21 """
bgneal@285 22 if not request.user.is_authenticated():
bgneal@285 23 return HttpResponseForbidden('Please login or register.')
bgneal@285 24
bgneal@285 25 post_id = request.GET.get('pid')
bgneal@285 26 if post_id is None:
bgneal@285 27 return HttpResponseBadRequest('Missing post ID.')
bgneal@285 28
bgneal@285 29 try:
bgneal@285 30 post = Post.objects.get(pk=post_id)
bgneal@285 31 except Post.DoesNotExist:
bgneal@285 32 return HttpResponseNotFound("That post doesn't exist.")
bgneal@286 33
bgneal@1037 34 attachments = Attachment.objects.filter(post=post).select_related('embed')
bgneal@1037 35 data = [{'id': a.embed.id, 'html': a.embed.html} for a in attachments]
bgneal@285 36
bgneal@285 37 return HttpResponse(json.dumps(data), content_type='application/json')
bgneal@285 38