From 675f418b1364303918cceae178ea18425e9eb179 Mon Sep 17 00:00:00 2001 From: Sanj Date: Tue, 20 Nov 2012 16:22:34 +0530 Subject: [PATCH] media gallery: delete item, edit item, edit gallery title --- itf/mediagallery/urls.py | 3 ++ itf/mediagallery/views.py | 34 ++++++++++++-- itf/static/js/upload/chunkupload.js | 7 ++- itf/static/js/upload/itfUpload.js | 58 +++++++++++++++++++----- itf/templates/mediagallery/upload.html | 62 +++++++++++++++++++++++--- 5 files changed, 143 insertions(+), 21 deletions(-) diff --git a/itf/mediagallery/urls.py b/itf/mediagallery/urls.py index 4ea695a..e0744fe 100644 --- a/itf/mediagallery/urls.py +++ b/itf/mediagallery/urls.py @@ -3,6 +3,9 @@ from django.conf.urls.defaults import * urlpatterns = patterns('mediagallery.views', (r'^add/(?P\d+)/(?P\d+)', 'add'), (r'^edit/(?P\d+)', 'edit'), + (r'^edit_title/(?P\d+)', 'edit_title'), + (r'^delete_photo/(?P\d+)', 'delete_photo'), + (r'^edit_photo/(?P\d+)', 'edit_photo'), (r'^upload/', 'upload'), (r'^(?P\d+)/chunk', 'chunk'), diff --git a/itf/mediagallery/views.py b/itf/mediagallery/views.py index 9a4c289..7872bde 100644 --- a/itf/mediagallery/views.py +++ b/itf/mediagallery/views.py @@ -2,11 +2,12 @@ from django.shortcuts import render_to_response, get_object_or_404 from django.template import RequestContext from django.http import HttpResponse, HttpResponseRedirect -from models import GalleryAlbum, Photo +from models import GalleryAlbum, GalleryItem, Photo from django.contrib.contenttypes.models import ContentType from django.views.decorators.csrf import csrf_exempt from django import forms from ox.django.shortcuts import render_to_json_response +from sorl.thumbnail import get_thumbnail def add(request, ctype_id, object_id): ctype = get_object_or_404(ContentType, pk=ctype_id) @@ -33,6 +34,33 @@ def edit(request, id): return render_to_response("mediagallery/upload.html", context) +def edit_title(request, id): + gallery = get_object_or_404(GalleryAlbum, pk=id) + title = request.GET.get("title", "") + if title.strip() == '': + return render_to_json_response({'error': 'Title is empty!'}) + gallery.title = title + gallery.save() + return render_to_json_response({'success': 'true'}) + + +def edit_photo(request, id): + item = get_object_or_404(Photo, pk=id) + title = request.GET.get("title", "") + if title.strip() == '': + return render_to_json_response({'error': 'Title is empty!'}) + item.title = title + item.save() + return render_to_json_response({'success': 'true'}) + + +#FIXME: check permissions +def delete_photo(request, id): + item = get_object_or_404(Photo, pk=id) + item.delete() + return render_to_json_response({'success': 'true'}) + + @csrf_exempt def upload(request): if request.method == 'POST': @@ -78,10 +106,10 @@ def chunk(request, id): item.save() response = { 'resultUrl': '/files/' + str(item.id), - 'fileId': item.id, + 'id': item.id, 'title': name, #'type': item.type, - #'thumbnail': item.get_thumb("100x100") + 'thumbnail': get_thumbnail(item.image, "100x100", crop="center").url } response['done'] = 1 diff --git a/itf/static/js/upload/chunkupload.js b/itf/static/js/upload/chunkupload.js index a2ffc42..616ffc2 100644 --- a/itf/static/js/upload/chunkupload.js +++ b/itf/static/js/upload/chunkupload.js @@ -32,6 +32,7 @@ function ChunkUploader(options) { retries = 0, request, that = {}; + that.extra_data = options.extra_data; initUpload(); @@ -39,7 +40,8 @@ function ChunkUploader(options) { options.callback({ status: that.status, progress: that.progress, - responseText: that.responseText + responseText: that.responseText, + extra: that.extra_data }); } @@ -101,7 +103,8 @@ function ChunkUploader(options) { that.progress = p; options.progress({ progress: that.progress, - status: that.status + status: that.status, + extra: that.extra_data }); } diff --git a/itf/static/js/upload/itfUpload.js b/itf/static/js/upload/itfUpload.js index b1fd3e5..fff5123 100644 --- a/itf/static/js/upload/itfUpload.js +++ b/itf/static/js/upload/itfUpload.js @@ -1,20 +1,21 @@ (function($) { ItfFileUpload = function(file, Q) { + this.uploaded = false; this.file = file; this.Q = Q; - - this.$li = this.getLi().appendTo(Q.$ul); + this.$li = this.getLi().prependTo('#photosContainer'); }; + ItfFileUpload.prototype.upload = function() { var that = this; var Q = that.Q; var fil = this.file; // console.log("uploading", fil); var data = Q.getData(fil); -// var $li = this.getLi(no); - Q.isUploading = true; +// this.$li = this.getLi().appendTo(that.Q.$elem); + this.Q.isUploading = true; var url = Q.options.addURL; this.showProgress(); // $('#progressbar').show(); @@ -23,26 +24,50 @@ ItfFileUpload.prototype.upload = function() { 'file': fil, 'url': url, 'data': data, + 'extra_data': that, 'callback': function(response) { var data = JSON.parse(response.responseText); //console.log("data", data); if (data.resultUrl) { that.markDone(data); } else { - that.$elem.find("progressStatus").html(response.status); + that.$li.find(".progressStatus").html(response.status); } - }, - 'progress': that.doProgress - }); + }, + 'progress': that.doProgress + }); +}; + +ItfFileUpload.prototype.markDone = function(data) { + var that = this; + //console.log(data); + //alert("hi"); + this.uploaded = true; + //var d = JSON.parse(data.responseText); + this.$li.removeClass("uploadFileItem").addClass("photoContainer").empty(); + var $img = $('').attr("src", data.thumbnail).appendTo(that.$li); + var $title = $('').addClass("photoTitle").val(data.title).appendTo(that.$li); + var $delete = $('').addClass("deleteItem").text("X").appendTo(that.$li); + var $id = $('').attr("hidden", "hidden").addClass("photoId").val(data.id).appendTo(that.$li); + this.Q.uploadNext(); }; ItfFileUpload.prototype.showProgress = function() { - this.Q.$elem.find(".fileProgress").show(); + var that = this; + //console.log(that); + this.$li.find(".fileProgress").show(); }; ItfFileUpload.prototype.doProgress = function(progress) { - console.log(progress); +// console.log(progress); +// var that = this; +// console.log(that); +// console.log(progress); +// GLOB = progress; + var widthPercent = progress.progress * 100; + progress.extra.$li.find(".progressBar").css({'width': widthPercent + "%"}); + //progress.extra.$li.find(".progressBar").text(progress.progress); }; ItfFileUpload.prototype.getLi = function() { @@ -67,11 +92,24 @@ ItfUploadQueue = function(options, $elem) { this.$ul = $("
    ").addClass("uploadFileList").appendTo($elem); }; +ItfUploadQueue.prototype.uploadNext = function() { + for (var i=0; i {% endblock %} @@ -27,19 +76,20 @@ $(function() {
    -
    +
      {% for photo in photos %} -
      +
    • {% if photo.image %} {% thumbnail photo.image "100x100" crop="center" as thumb %} {% endthumbnail %} - {% endif %} + {% endif %} + X -
    • + {% endfor %} -
    +
{% endblock %}