annotate oembed/views.py @ 1163:44e55e4317f8

Merge with mainline.
author Brian Neal <bgneal@gmail.com>
date Tue, 07 Mar 2017 19:47:18 -0600
parents 4da4e32b314c
children
rev   line source
bgneal@285 1 """
bgneal@285 2 Views for the oembed application.
bgneal@679 3
bgneal@285 4 """
bgneal@679 5 import json
bgneal@285 6 import re
bgneal@285 7
bgneal@285 8 from django.http import HttpResponse
bgneal@285 9 from django.http import HttpResponseBadRequest
bgneal@285 10 from django.http import HttpResponseForbidden
bgneal@361 11 from django.conf import settings
bgneal@285 12
bgneal@285 13 from oembed.models import Provider
bgneal@285 14 from oembed.models import Oembed
bgneal@285 15 from oembed.core import get_oembed
bgneal@285 16
bgneal@285 17
bgneal@285 18 def fetch_media(request):
bgneal@285 19 """
bgneal@286 20 This view returns the HTML media of an embeddable resource as
bgneal@286 21 JSON. This view is the target of an AJAX request.
bgneal@285 22 """
bgneal@285 23 if not request.user.is_authenticated():
bgneal@285 24 return HttpResponseForbidden('Please login or register.')
bgneal@285 25
bgneal@285 26 url = request.POST.get('q')
bgneal@285 27
bgneal@285 28 if not url:
bgneal@285 29 return HttpResponseBadRequest('Please provide a valid URL.')
bgneal@285 30
bgneal@285 31 # Is this already in our database?
bgneal@285 32 try:
bgneal@285 33 oembed = Oembed.objects.get(url=url)
bgneal@285 34 except Oembed.DoesNotExist:
bgneal@285 35 pass
bgneal@285 36 else:
bgneal@285 37 data = dict(id=oembed.id, embed=oembed.html)
bgneal@285 38 return HttpResponse(json.dumps(data), content_type='application/json')
bgneal@285 39
bgneal@285 40 # It isn't in the database, try to find it from our providers
bgneal@285 41 providers = Provider.objects.all()
bgneal@285 42 for provider in providers:
bgneal@285 43 if re.match(provider.url_regex, url):
bgneal@285 44 try:
bgneal@361 45 data = get_oembed(provider.api_endpoint,
bgneal@907 46 url,
bgneal@907 47 maxwidth=settings.OEMBED_MAXWIDTH,
bgneal@907 48 maxheight=settings.OEMBED_MAXHEIGHT,
bgneal@907 49 scheme='https')
bgneal@285 50 except IOError, e:
bgneal@285 51 return HttpResponseBadRequest(
bgneal@285 52 "Sorry, we could not retrieve your video (%s)" % e)
bgneal@285 53
bgneal@1154 54 data_type = data.get('type', '?')
bgneal@1154 55 if data_type not in Oembed.ALLOWED_TYPES:
bgneal@285 56 return HttpResponseBadRequest(
bgneal@1154 57 "Hey, this doesn't look like a media type we support..??")
bgneal@285 58
bgneal@285 59 oembed = Oembed(url=url,
bgneal@1154 60 type=Oembed.MEDIA_TYPE_REVERSE[data_type],
bgneal@907 61 title=data.get('title', ''),
bgneal@907 62 width=int(data.get('width', 0)),
bgneal@907 63 height=int(data.get('height', 0)),
bgneal@907 64 html=data.get('html', ''))
bgneal@285 65 oembed.save()
bgneal@285 66
bgneal@285 67 data = dict(id=oembed.id, embed=oembed.html)
bgneal@285 68 return HttpResponse(json.dumps(data),
bgneal@285 69 content_type='application/json')
bgneal@285 70
bgneal@287 71 return HttpResponseBadRequest("Sorry, we couldn't find that video.")
bgneal@286 72
bgneal@286 73
bgneal@286 74 def fetch_saved_media(request):
bgneal@286 75 """
bgneal@286 76 This view returns the HTML embed information for previously saved Oembed
bgneal@286 77 objects as JSON. This view is the target of an AJAX request.
bgneal@286 78 """
bgneal@286 79 if not request.user.is_authenticated():
bgneal@286 80 return HttpResponseForbidden('Please login or register.')
bgneal@286 81
bgneal@286 82 embed_ids = request.GET.getlist('embeds')
bgneal@286 83 if not embed_ids:
bgneal@286 84 return HttpResponseBadRequest('Missing embed list.')
bgneal@286 85
bgneal@286 86 embeds = Oembed.objects.in_bulk(embed_ids)
bgneal@286 87
bgneal@286 88 # build results in order
bgneal@286 89 results = []
bgneal@286 90 for pk in embeds:
bgneal@286 91 results.append(dict(id=pk, html=embeds[pk].html))
bgneal@286 92
bgneal@286 93 return HttpResponse(json.dumps(results), content_type='application/json')