diff --git a/spa/audio.py b/spa/audio.py index 082a3b6..2bfea99 100644 --- a/spa/audio.py +++ b/spa/audio.py @@ -18,10 +18,11 @@ def download(request, mix_id): try: mix = Mix.objects.get(pk=mix_id) if mix is not None: - filename = mix.local_file.path # Select your file here. - file, ext = os.path.splitext(filename) - response = sendfile(request, filename, attachment=True, attachment_filename="%s.%s" % (mix.title, ext)) - return response + if mix.download_allowed: + filename = mix.local_file.path # Select your file here. + file, ext = os.path.splitext(filename) + response = sendfile(request, filename, attachment=True, attachment_filename="%s.%s" % (mix.title, ext)) + return response except Exception, ex: print ex diff --git a/spa/models/Mix.py b/spa/models/Mix.py index 8a7783a..529a751 100644 --- a/spa/models/Mix.py +++ b/spa/models/Mix.py @@ -33,6 +33,7 @@ class Mix(_BaseModel): user = models.ForeignKey(UserProfile, editable=False) waveform_generated = models.BooleanField(default=False) uid = models.CharField(max_length=38, blank=True, unique=True) + download_allowed = models.BooleanField(default=False) def __unicode__(self): return self.title diff --git a/static/js/app/views/mix.js b/static/js/app/views/mix.js index c6607c0..8d9e1af 100644 --- a/static/js/app/views/mix.js +++ b/static/js/app/views/mix.js @@ -14,7 +14,8 @@ window.MixListItemView = Backbone.View.extend({ "click .play-button-small-pause":"pauseMix", "click .like-button a":"likeMix", "click .favourite-button a":"favouriteMix", - "click .share-button":"shareLink" + "click .share-button":"shareLink", + "click .download-button a":"downloadMix" }, initialize:function () { $(this.el).attr("id", "mixitem-" + this.model.get("id")); @@ -49,6 +50,11 @@ window.MixListItemView = Backbone.View.extend({ else if (mode == "twitter") sharePageToTwitter(this.model); }, + downloadMix:function (e) { + var id = $(e.currentTarget).data("id"); + var mode = $(e.currentTarget).data("mode"); + com.podnoms.utils.downloadURL("/audio/download/" + id); + }, likeMix:function (e) { var id = $(e.currentTarget).data("id"); var mode = $(e.currentTarget).data("mode"); diff --git a/static/js/com.podnoms.utils.js b/static/js/com.podnoms.utils.js index 4576fc5..c2bc436 100644 --- a/static/js/com.podnoms.utils.js +++ b/static/js/com.podnoms.utils.js @@ -88,6 +88,17 @@ com.podnoms.utils = { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); + }, + downloadURL:function downloadURL(url) { + var iframe; + iframe = document.getElementById("hiddenDownloader"); + if (iframe === null) { + iframe = document.createElement('iframe'); + iframe.id = "hiddenDownloader"; + iframe.style.visibility = 'hidden'; + document.body.appendChild(iframe); + } + iframe.src = url; } }; diff --git a/templates/inc/_MixItemInsert.html b/templates/inc/_MixItemInsert.html index 8d0d8b8..4ae0e6b 100644 --- a/templates/inc/_MixItemInsert.html +++ b/templates/inc/_MixItemInsert.html @@ -16,7 +16,6 @@ Audio waveform -
@@ -48,6 +47,14 @@ + {% if user.is_authenticated %} + <% if (item.download_allowed) { %> + + <% } %> + {% endif %}