From 6d05f599a75fe9cbbd55d6a42c2c10873c0e97e5 Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Tue, 26 Oct 2010 22:00:38 -0400 Subject: [PATCH] add buttons are now ajax'd! --- playlist/images/spinner.gif | Bin 0 -> 1737 bytes playlist/templates/add.html | 2 +- playlist/templates/master.html | 35 +++++++++++++++++++++++++++++++++ playlist/views.py | 27 +++++++++++++++++++++---- 4 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 playlist/images/spinner.gif diff --git a/playlist/images/spinner.gif b/playlist/images/spinner.gif new file mode 100644 index 0000000000000000000000000000000000000000..2739d0d53e85f388c3b884610ff30cee0c8c95df GIT binary patch literal 1737 zcmaLXdr(tX9>DQqfV?3I+$2IYgrFgkgoIqYd57FQZXiH-ctxHu0TLlZA_$_^?Y8G~ z@9L`Mj;npyhmKpv@qr?BGp?+Pw%Dz8X|3yY`Fo5+&g^u$|Lm@A-T#>V{dwm6 zWaH=4zzc*Vb0ACucLpc@6hpsH~xJ6@XNnd zSE#icD)dIZsn%3yuj@&4zjgi%WvMbg9RJ|6-qrkg?b8`q|f9J||zmKXLl_uMa(s zD3F4Jg7+WYUHSS!YAm^HU-px4SH`AyzV!OhN8dku{P#y^uAIJb{rto4KEC$eyT5z; z*hgP}c=h&`rB7}z-oJ5q@zS|#XWzc~*3Actx#epQzPf+?UT#M=_1ZhHymID*FXRg~ zg_=8>wsd#c-8R%_J-hJEP-d{&RK5K8{YqWsUN{|Ti)_?yELD{rJjVFLxI`-1ePCj1 zFKILz&gafux^dy|!=*RQogCerK0rUmuykXzVgJlNZ@}yEyVsSkJNoM3!lFW-}TsVu0#&Ub$`Q&v&M ztF2QT-GU?9wkzznr-_&1cZ#{3)r3c$30-;Hy2QQxDh9aMZWVQ#`ZjFW(UI0I4Kjp` z#H#yd!uf3{X)7({R~Ld2w2tf-U1?r$|%0z9Jmh}rThibw*=~y z9c7G}6Z{i&SnC=EPNz8(SA9z1x=v$Hc_fe^S<=y%z9>Bgm^lh?)~p6R^b9BvmW*&? zMh*i4ye6)ss8F2F(DvAl;dZ$^&@n3JQ`(`Zn;AB>soWTZi>`d?M!NWvkN*p z^(<%*=fQ|kkduvWgPD{whZJvx*~~qbTt5L7?txrnXA=zTy}4+0B_!f@ExW4_r_6DO z)&gz+0OK?g;s~^)2gSyIIHe)P1#8!2H?2eXs~P{77)fN4YMrc}0RqE#Qd!qfVpY&V zw@~3jQI2qc^otnsl*g*YXX?6D|4uz5kN*Stli-a^rSe&qCY?7ZFv!j+fk1tX1|0lNi9D0Kc zM;Jij_Q6Owe_ZIL<*Im?#GKl)&Q6@dd|or-uS*U$c0dN zVa+Uwdr??f9i0UNMG!V^Fw7wV2Nd;@b68{$icL3Xo#lT3JG%9A!5$e*il*jdMjUdQ zy2crWlD`bHZIjk^4}oVYlq%YwX-zk`#CYn&Y?xnL8twD_c=yB-{vrUbryt*c=Rcrw B754xD literal 0 HcmV?d00001 diff --git a/playlist/templates/add.html b/playlist/templates/add.html index 0a4244e..c090181 100644 --- a/playlist/templates/add.html +++ b/playlist/templates/add.html @@ -2,4 +2,4 @@ ({{song.addDisallowed.1}}) {% else %} Add! -{% endif %} \ No newline at end of file +{% endif %} diff --git a/playlist/templates/master.html b/playlist/templates/master.html index bebf8b3..a6ca347 100644 --- a/playlist/templates/master.html +++ b/playlist/templates/master.html @@ -232,6 +232,40 @@ $(".playing .votes a[data-vote="+score+"]").addClass("voted"); } +function setupAdds() { + var addbuttons = $("a.add"); + addbuttons.click(function (e) { + // button was clicked + e.preventDefault(); + + var button = $(this); + var parent = button.parent(); + + // Get the add url + var addurl = button.attr("href"); + + // remove the button and put a spinner in its place + + button.remove(); + var spinner = $("adding..."); + parent.append(spinner); + + // fire of a call to add the song + $.getJSON(addurl, function (data) { + // Json event returned from adding a song + spinner.remove(); + if (data[0] == true) { + parent.append("(Added)"); + } else { + parent.append(button); + } + for (i=1; i a").click(function () { args = {}; @@ -324,6 +358,7 @@ //comment box stuff setupComment(); setupVotes(); + setupAdds(); //ajax events stuff ajax_args['position'] = songPosition; diff --git a/playlist/views.py b/playlist/views.py index c15667c..f616daa 100644 --- a/playlist/views.py +++ b/playlist/views.py @@ -1060,10 +1060,20 @@ def add(request, songid=0): raise Http404 profile = request.user.get_profile() oldtokens = profile.tokens + + # is this an ajax request? + try: + isajax = (request.META['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest") + toret = [1] + except KeyError: + isajax = False + try: song.playlistAdd(request.user) except AddError, e: msg = "Error: %s" % (e.args[0]) + if isajax: + return HttpResponse(json.dumps((0, msg))) request.user.message_set.create(message=msg) return HttpResponseRedirect(reverse("playlist")) @@ -1072,17 +1082,26 @@ def add(request, songid=0): msg = "You already had a dong on the playlist, so you've used up a token to add this one. You have %d left" % (profile.tokens) else: msg = "You already had a dong on the playlist, so you've used up a token to add this one. That was your last one!" - request.user.message_set.create(message=msg) - else: + if not isajax: + request.user.message_set.create(message=msg) + else: + toret.append(msg) + elif not isajax: request.user.message_set.create(message="Track added successfully!") if song.isOrphan(): song.uploader = request.user song.save() msg = "This dong was an orphan, so you have automatically adopted it. Take good care of it!" - request.user.message_set.create(message=msg) + if isajax: + toret.append(msg) + else: + request.user.message_set.create(message=msg) - return HttpResponseRedirect(reverse("playlist")) + if isajax: + return HttpResponse(json.dumps(toret)) + else: + return HttpResponseRedirect(reverse("playlist")) def next(request, authid): """Go to the next song in the playlist, and return a string for ices to parse.