diff gpp/forums/views/attachments.py @ 285:8fd4984d5c3b

This is a first rough commit for #95, adding the ability to embed YouTube videos in forum posts. Some more polish and testing needs to happen at this point. I wanted to get all these changes off my hard drive and into the repository.
author Brian Neal <bgneal@gmail.com>
date Thu, 14 Oct 2010 02:39:35 +0000
parents
children 72fd300685d5
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/forums/views/attachments.py	Thu Oct 14 02:39:35 2010 +0000
@@ -0,0 +1,35 @@
+"""
+This module contains views for working with post attachments.
+"""
+from django.http import HttpResponse
+from django.http import HttpResponseForbidden
+from django.http import HttpResponseBadRequest
+from django.http import HttpResponseNotFound
+import django.utils.simplejson as json
+
+from forums.models import Post
+
+
+def fetch_attachments(request):
+    """
+    This view is the target of an AJAX GET request to retrieve the
+    attachment embed data for a given forum post.
+
+    """
+    if not request.user.is_authenticated():
+        return HttpResponseForbidden('Please login or register.')
+
+    post_id = request.GET.get('pid')
+    if post_id is None:
+        return HttpResponseBadRequest('Missing post ID.')
+
+    try:
+        post = Post.objects.get(pk=post_id)
+    except Post.DoesNotExist:
+        return HttpResponseNotFound("That post doesn't exist.")
+    
+    embeds = post.attachments.all().select_related('embed')
+    data = [{'id': embed.id, 'html': embed.html} for embed in embeds]
+
+    return HttpResponse(json.dumps(data), content_type='application/json')
+