Mercurial > public > sg101
comparison 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 |
comparison
equal
deleted
inserted
replaced
284:df2c81f705a8 | 285:8fd4984d5c3b |
---|---|
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 |