From 2349091f401bd7fd6da7ffbd3447901d1fbe4ded Mon Sep 17 00:00:00 2001 From: Thejaswi Puthraya Date: Tue, 16 Dec 2014 18:18:57 +0530 Subject: [PATCH 1/4] Initial iteration of the album embed. --- pari/album/templates/album/album_detail.html | 15 +++++- pari/album/urls.py | 2 + pari/album/views.py | 55 ++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/pari/album/templates/album/album_detail.html b/pari/album/templates/album/album_detail.html index e50bf18..63d47cd 100644 --- a/pari/album/templates/album/album_detail.html +++ b/pari/album/templates/album/album_detail.html @@ -13,11 +13,24 @@ {% block extra_uncompressed_js %} + {% if embed %} + + {% endif %} {% endblock %} {% block extra_css %} + {% endblock %} {% block main %} @@ -79,4 +92,4 @@

{{ album.title }}

{% endfor %} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/pari/album/urls.py b/pari/album/urls.py index 759ab99..d7a1b6c 100644 --- a/pari/album/urls.py +++ b/pari/album/urls.py @@ -8,6 +8,8 @@ urlpatterns = patterns('pari.album.views', url(r'^$', AlbumList.as_view(), {'albums': get_all_albums}, name='album-list'), url(r'^talking/$', AlbumList.as_view(), {'albums': get_talking_albums}, name='talking-album-list'), + url(r'^talking/embed/$', 'embed_talking_album', name='embed-talking-album'), + url(r'^talking/embed/(?P\d+)/$', 'embed_talking_album_detail', name='embed-talking-album-detail'), url(r'^other/$', AlbumList.as_view(), {'albums': get_other_albums}, name='other-album-list'), url(r'^(?P.+)/$', AlbumDetail.as_view(), name='album-detail'), diff --git a/pari/album/views.py b/pari/album/views.py index 7017803..16513c5 100644 --- a/pari/album/views.py +++ b/pari/album/views.py @@ -1,6 +1,14 @@ from django.views.generic import DetailView, ListView from pari.album.models import Album, AlbumImage, ImageCollectionImage +import json +from django.http import HttpResponse +from django.contrib.sites.models import RequestSite +from django.core.urlresolvers import reverse +from django.shortcuts import render, get_object_or_404 + +from mezzanine.conf import settings + class AlbumList(ListView): model = Album @@ -32,3 +40,50 @@ class ImageCollectionImageList(DetailView): def get_object(self, queryset=None): album = Album.objects.get(slug=self.kwargs['slug']) return album.image_collection.images + + +def embed_talking_album(request): + url = request.GET.get("url") + # http://www.ruralindiaonline.org/albums/the-green-army/ + slug = filter(lambda x: x, url.split("/"))[-1] + album = Album.objects.get(slug=slug) + is_request_secure = request.is_secure() + domain = RequestSite(request).domain + album_embed_url = "http{0}://{1}{2}".format( + "s" if is_request_secure else "", + domain, + reverse("embed-talking-album-detail", kwargs={"id": album.id}) + ) + author_url = "http{0}://{1}".format( + "s" if is_request_secure else "", + domain, + album.photographer.get_absolute_url() + ) + width = request.GET.get("width", "1024") + height = request.GET.get("height", "768") + response = json.dumps({ + "type": "rich", + "version": 1.0, + "title": album.title, + "author_name": album.photographer.title, + "author_url": author_url, + "provider_name": settings.SITE_FULL_TITLE, + "provider_url": "http{0}://{1}/".format( + "s" if is_request_secure else "", + domain + ), + "html": """""".format( + album_embed_url, + width, height + ), + "width": width, + "height": height + }) + return HttpResponse(response, content_type="application/json") + +def embed_talking_album_detail(request, id=None): + album = get_object_or_404(Album, id=id) + return render(request, "album/album_detail.html", { + "album": album, + "embed": True + }) From e46766252ff2eae5d0596042f6990cf08bffd6f3 Mon Sep 17 00:00:00 2001 From: Thejaswi Puthraya Date: Tue, 16 Dec 2014 18:53:27 +0530 Subject: [PATCH 2/4] JSONP support and the Album embed script. --- pari/album/static/album/js/album_embed.js | 13 +++++++++++++ pari/album/views.py | 6 +++++- 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 pari/album/static/album/js/album_embed.js diff --git a/pari/album/static/album/js/album_embed.js b/pari/album/static/album/js/album_embed.js new file mode 100644 index 0000000..3b154f0 --- /dev/null +++ b/pari/album/static/album/js/album_embed.js @@ -0,0 +1,13 @@ +(function() { + var sc = document.createElement("script"); + function cb(data) { + var holder = document.getElementById(__pariAlbum.widgetId); + holder.innerHTML = data.html; + } + window.pariAlbumParseResponse = cb; + sc.src = "//pari.tld/albums/talking/embed/?url=" + encodeURIComponent(__pariAlbum.url) + + "&width=" + __pariAlbum.widgetWidth + + "&height=" + __pariAlbum.widgetHeight + + " &&callback=pariAlbumParseResponse"; + document.body.appendChild(sc); +})(); diff --git a/pari/album/views.py b/pari/album/views.py index 16513c5..dd31299 100644 --- a/pari/album/views.py +++ b/pari/album/views.py @@ -79,7 +79,11 @@ def embed_talking_album(request): "width": width, "height": height }) - return HttpResponse(response, content_type="application/json") + content_type = "application/json" + if request.GET.get("callback"): + response = "{0}({1})".format(request.GET["callback"], response) + content_type = "application/javascript" + return HttpResponse(response, content_type=content_type) def embed_talking_album_detail(request, id=None): album = get_object_or_404(Album, id=id) From 19798e29f4e9979772dc3a9c327e3553efb5598a Mon Sep 17 00:00:00 2001 From: Thejaswi Puthraya Date: Tue, 16 Dec 2014 19:01:01 +0530 Subject: [PATCH 3/4] Remove the hardcode from the local. --- pari/album/static/album/js/album_embed.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pari/album/static/album/js/album_embed.js b/pari/album/static/album/js/album_embed.js index 3b154f0..c888db6 100644 --- a/pari/album/static/album/js/album_embed.js +++ b/pari/album/static/album/js/album_embed.js @@ -5,9 +5,9 @@ holder.innerHTML = data.html; } window.pariAlbumParseResponse = cb; - sc.src = "//pari.tld/albums/talking/embed/?url=" + encodeURIComponent(__pariAlbum.url) - + "&width=" + __pariAlbum.widgetWidth - + "&height=" + __pariAlbum.widgetHeight + - " &&callback=pariAlbumParseResponse"; + sc.src = "//www.ruralindiaonline.org/albums/talking/embed/?url=" + encodeURIComponent(__pariAlbum.url) + + "&width=" + __pariAlbum.widgetWidth + + "&height=" + __pariAlbum.widgetHeight + + "&callback=pariAlbumParseResponse"; document.body.appendChild(sc); })(); From dc93272edfbd6acfe23222c20bf12cf1f29e700d Mon Sep 17 00:00:00 2001 From: Thejaswi Puthraya Date: Fri, 19 Dec 2014 16:12:29 +0530 Subject: [PATCH 4/4] Flake8 changes. --- pari/album/views.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pari/album/views.py b/pari/album/views.py index dd31299..4b7ce5e 100644 --- a/pari/album/views.py +++ b/pari/album/views.py @@ -85,6 +85,7 @@ def embed_talking_album(request): content_type = "application/javascript" return HttpResponse(response, content_type=content_type) + def embed_talking_album_detail(request, id=None): album = get_object_or_404(Album, id=id) return render(request, "album/album_detail.html", {