annotate oembed/views.py @ 1112:654b55ad9134

Tweak countdown admin settings.
author Brian Neal <bgneal@gmail.com>
date Mon, 11 Jul 2016 21:44:09 -0500
parents 344f7914d421
children 4da4e32b314c
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@907 54 if data.get('type') != 'video':
bgneal@285 55 return HttpResponseBadRequest(
bgneal@285 56 "Hey, this doesn't look like a video..??")
bgneal@285 57
bgneal@285 58 oembed = Oembed(url=url,
bgneal@907 59 type=Oembed.VIDEO,
bgneal@907 60 title=data.get('title', ''),
bgneal@907 61 width=int(data.get('width', 0)),
bgneal@907 62 height=int(data.get('height', 0)),
bgneal@907 63 html=data.get('html', ''))
bgneal@285 64 oembed.save()
bgneal@285 65
bgneal@285 66 data = dict(id=oembed.id, embed=oembed.html)
bgneal@285 67 return HttpResponse(json.dumps(data),
bgneal@285 68 content_type='application/json')
bgneal@285 69
bgneal@287 70 return HttpResponseBadRequest("Sorry, we couldn't find that video.")
bgneal@286 71
bgneal@286 72
bgneal@286 73 def fetch_saved_media(request):
bgneal@286 74 """
bgneal@286 75 This view returns the HTML embed information for previously saved Oembed
bgneal@286 76 objects as JSON. This view is the target of an AJAX request.
bgneal@286 77 """
bgneal@286 78 if not request.user.is_authenticated():
bgneal@286 79 return HttpResponseForbidden('Please login or register.')
bgneal@286 80
bgneal@286 81 embed_ids = request.GET.getlist('embeds')
bgneal@286 82 if not embed_ids:
bgneal@286 83 return HttpResponseBadRequest('Missing embed list.')
bgneal@286 84
bgneal@286 85 embeds = Oembed.objects.in_bulk(embed_ids)
bgneal@286 86
bgneal@286 87 # build results in order
bgneal@286 88 results = []
bgneal@286 89 for pk in embeds:
bgneal@286 90 results.append(dict(id=pk, html=embeds[pk].html))
bgneal@286 91
bgneal@286 92 return HttpResponse(json.dumps(results), content_type='application/json')