Mercurial > public > sg101
comparison oembed/views.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/oembed/views.py@6d6fdc58487c |
children | 89b240fe9297 |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 Views for the oembed application. | |
3 """ | |
4 import re | |
5 import urllib2 | |
6 | |
7 from django.http import HttpResponse | |
8 from django.http import HttpResponseBadRequest | |
9 from django.http import HttpResponseForbidden | |
10 import django.utils.simplejson as json | |
11 from django.conf import settings | |
12 | |
13 from oembed.models import Provider | |
14 from oembed.models import Oembed | |
15 from oembed.core import get_oembed | |
16 | |
17 | |
18 def fetch_media(request): | |
19 """ | |
20 This view returns the HTML media of an embeddable resource as | |
21 JSON. This view is the target of an AJAX request. | |
22 """ | |
23 if not request.user.is_authenticated(): | |
24 return HttpResponseForbidden('Please login or register.') | |
25 | |
26 url = request.POST.get('q') | |
27 | |
28 if not url: | |
29 return HttpResponseBadRequest('Please provide a valid URL.') | |
30 | |
31 # Is this already in our database? | |
32 try: | |
33 oembed = Oembed.objects.get(url=url) | |
34 except Oembed.DoesNotExist: | |
35 pass | |
36 else: | |
37 data = dict(id=oembed.id, embed=oembed.html) | |
38 return HttpResponse(json.dumps(data), content_type='application/json') | |
39 | |
40 # It isn't in the database, try to find it from our providers | |
41 providers = Provider.objects.all() | |
42 for provider in providers: | |
43 if re.match(provider.url_regex, url): | |
44 try: | |
45 data = get_oembed(provider.api_endpoint, | |
46 url, | |
47 maxwidth=settings.OEMBED_MAXWIDTH, | |
48 maxheight=settings.OEMBED_MAXHEIGHT) | |
49 except IOError, e: | |
50 return HttpResponseBadRequest( | |
51 "Sorry, we could not retrieve your video (%s)" % e) | |
52 | |
53 if 'type' not in data or data['type'] != 'video': | |
54 return HttpResponseBadRequest( | |
55 "Hey, this doesn't look like a video..??") | |
56 | |
57 oembed = Oembed(url=url, | |
58 type=Oembed.VIDEO, | |
59 title=data.get('title', ''), | |
60 width=int(data.get('width', 0)), | |
61 height=int(data.get('height', 0)), | |
62 html=data.get('html', '')) | |
63 oembed.save() | |
64 | |
65 data = dict(id=oembed.id, embed=oembed.html) | |
66 return HttpResponse(json.dumps(data), | |
67 content_type='application/json') | |
68 | |
69 return HttpResponseBadRequest("Sorry, we couldn't find that video.") | |
70 | |
71 | |
72 def fetch_saved_media(request): | |
73 """ | |
74 This view returns the HTML embed information for previously saved Oembed | |
75 objects as JSON. This view is the target of an AJAX request. | |
76 """ | |
77 if not request.user.is_authenticated(): | |
78 return HttpResponseForbidden('Please login or register.') | |
79 | |
80 embed_ids = request.GET.getlist('embeds') | |
81 if not embed_ids: | |
82 return HttpResponseBadRequest('Missing embed list.') | |
83 | |
84 embeds = Oembed.objects.in_bulk(embed_ids) | |
85 | |
86 # build results in order | |
87 results = [] | |
88 for pk in embeds: | |
89 results.append(dict(id=pk, html=embeds[pk].html)) | |
90 | |
91 return HttpResponse(json.dumps(results), content_type='application/json') |