Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oembed album #137

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pari/album/static/album/js/album_embed.js
Original file line number Diff line number Diff line change
@@ -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 = "//www.ruralindiaonline.org/albums/talking/embed/?url=" + encodeURIComponent(__pariAlbum.url)
+ "&width=" + __pariAlbum.widgetWidth
+ "&height=" + __pariAlbum.widgetHeight
+ "&callback=pariAlbumParseResponse";
document.body.appendChild(sc);
})();
15 changes: 14 additions & 1 deletion pari/album/templates/album/album_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,24 @@

{% block extra_uncompressed_js %}
<script src="//connect.soundcloud.com/sdk.js"></script>
{% if embed %}
<script type="text/javascript">
$(function() {
Album.init();
$(".album-controls").trigger("click");
});
</script>
{% endif %}
{% endblock %}

{% block extra_css %}
<link type="text/css" rel="stylesheet" href="{{ STATIC_URL }}css/magnific-popup.css" charset="utf-8">
<link type="text/less" rel="stylesheet" href="{{ STATIC_URL }}css/album.less" charset="utf-8">
<style>
.mfp-container .mfp-figure .mfp-close {
display: none;
}
</style>
{% endblock %}

{% block main %}
Expand Down Expand Up @@ -79,4 +92,4 @@ <h2>{{ album.title }}</h2>
</div>
{% endfor %}
</div>
{% endblock %}
{% endblock %}
2 changes: 2 additions & 0 deletions pari/album/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<id>\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<slug>.+)/$', AlbumDetail.as_view(), name='album-detail'),
Expand Down
60 changes: 60 additions & 0 deletions pari/album/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -32,3 +40,55 @@ 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": """<iframe src="{0}" width="{1}" height="{2}"></iframe>""".format(
album_embed_url,
width, height
),
"width": width,
"height": height
})
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)
return render(request, "album/album_detail.html", {
"album": album,
"embed": True
})