media gallery: delete item, edit item, edit gallery title

This commit is contained in:
Sanj 2012-11-20 16:22:34 +05:30
parent 26677493ee
commit 675f418b13
5 changed files with 143 additions and 21 deletions

View File

@ -3,6 +3,9 @@ from django.conf.urls.defaults import *
urlpatterns = patterns('mediagallery.views', urlpatterns = patterns('mediagallery.views',
(r'^add/(?P<ctype_id>\d+)/(?P<object_id>\d+)', 'add'), (r'^add/(?P<ctype_id>\d+)/(?P<object_id>\d+)', 'add'),
(r'^edit/(?P<id>\d+)', 'edit'), (r'^edit/(?P<id>\d+)', 'edit'),
(r'^edit_title/(?P<id>\d+)', 'edit_title'),
(r'^delete_photo/(?P<id>\d+)', 'delete_photo'),
(r'^edit_photo/(?P<id>\d+)', 'edit_photo'),
(r'^upload/', 'upload'), (r'^upload/', 'upload'),
(r'^(?P<id>\d+)/chunk', 'chunk'), (r'^(?P<id>\d+)/chunk', 'chunk'),

View File

@ -2,11 +2,12 @@
from django.shortcuts import render_to_response, get_object_or_404 from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext from django.template import RequestContext
from django.http import HttpResponse, HttpResponseRedirect 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.contrib.contenttypes.models import ContentType
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from django import forms from django import forms
from ox.django.shortcuts import render_to_json_response from ox.django.shortcuts import render_to_json_response
from sorl.thumbnail import get_thumbnail
def add(request, ctype_id, object_id): def add(request, ctype_id, object_id):
ctype = get_object_or_404(ContentType, pk=ctype_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) 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 @csrf_exempt
def upload(request): def upload(request):
if request.method == 'POST': if request.method == 'POST':
@ -78,10 +106,10 @@ def chunk(request, id):
item.save() item.save()
response = { response = {
'resultUrl': '/files/' + str(item.id), 'resultUrl': '/files/' + str(item.id),
'fileId': item.id, 'id': item.id,
'title': name, 'title': name,
#'type': item.type, #'type': item.type,
#'thumbnail': item.get_thumb("100x100") 'thumbnail': get_thumbnail(item.image, "100x100", crop="center").url
} }
response['done'] = 1 response['done'] = 1

View File

@ -32,6 +32,7 @@ function ChunkUploader(options) {
retries = 0, retries = 0,
request, request,
that = {}; that = {};
that.extra_data = options.extra_data;
initUpload(); initUpload();
@ -39,7 +40,8 @@ function ChunkUploader(options) {
options.callback({ options.callback({
status: that.status, status: that.status,
progress: that.progress, progress: that.progress,
responseText: that.responseText responseText: that.responseText,
extra: that.extra_data
}); });
} }
@ -101,7 +103,8 @@ function ChunkUploader(options) {
that.progress = p; that.progress = p;
options.progress({ options.progress({
progress: that.progress, progress: that.progress,
status: that.status status: that.status,
extra: that.extra_data
}); });
} }

View File

@ -1,20 +1,21 @@
(function($) { (function($) {
ItfFileUpload = function(file, Q) { ItfFileUpload = function(file, Q) {
this.uploaded = false;
this.file = file; this.file = file;
this.Q = Q; this.Q = Q;
this.$li = this.getLi().prependTo('#photosContainer');
this.$li = this.getLi().appendTo(Q.$ul);
}; };
ItfFileUpload.prototype.upload = function() { ItfFileUpload.prototype.upload = function() {
var that = this; var that = this;
var Q = that.Q; var Q = that.Q;
var fil = this.file; var fil = this.file;
// console.log("uploading", fil); // console.log("uploading", fil);
var data = Q.getData(fil); var data = Q.getData(fil);
// var $li = this.getLi(no); // this.$li = this.getLi().appendTo(that.Q.$elem);
Q.isUploading = true; this.Q.isUploading = true;
var url = Q.options.addURL; var url = Q.options.addURL;
this.showProgress(); this.showProgress();
// $('#progressbar').show(); // $('#progressbar').show();
@ -23,26 +24,50 @@ ItfFileUpload.prototype.upload = function() {
'file': fil, 'file': fil,
'url': url, 'url': url,
'data': data, 'data': data,
'extra_data': that,
'callback': function(response) { 'callback': function(response) {
var data = JSON.parse(response.responseText); var data = JSON.parse(response.responseText);
//console.log("data", data); //console.log("data", data);
if (data.resultUrl) { if (data.resultUrl) {
that.markDone(data); that.markDone(data);
} else { } 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 = $('<img />').attr("src", data.thumbnail).appendTo(that.$li);
var $title = $('<input />').addClass("photoTitle").val(data.title).appendTo(that.$li);
var $delete = $('<span />').addClass("deleteItem").text("X").appendTo(that.$li);
var $id = $('<input />').attr("hidden", "hidden").addClass("photoId").val(data.id).appendTo(that.$li);
this.Q.uploadNext();
}; };
ItfFileUpload.prototype.showProgress = function() { 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) { 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() { ItfFileUpload.prototype.getLi = function() {
@ -67,11 +92,24 @@ ItfUploadQueue = function(options, $elem) {
this.$ul = $("<ul />").addClass("uploadFileList").appendTo($elem); this.$ul = $("<ul />").addClass("uploadFileList").appendTo($elem);
}; };
ItfUploadQueue.prototype.uploadNext = function() {
for (var i=0; i<this.files.length; i++) {
if (this.files[i].uploaded === false) {
this.files[i].upload();
return;
}
}
this.isUploading = false;
return;
};
ItfUploadQueue.prototype.addFile = function(f) { ItfUploadQueue.prototype.addFile = function(f) {
var that = this; var that = this;
var fileUpload = new ItfFileUpload(f, that); var fileUpload = new ItfFileUpload(f, that);
this.files.push(fileUpload); this.files.push(fileUpload);
// this.addToList(f); // this.addToList(f);
// var no = this.len() - 1;
if (!this.isUploading) { if (!this.isUploading) {
fileUpload.upload(); fileUpload.upload();
} }

View File

@ -6,18 +6,67 @@
<script src="/static/js/upload/chunkupload.js"></script> <script src="/static/js/upload/chunkupload.js"></script>
<script src="/static/js/upload/itfUpload.js"></script> <script src="/static/js/upload/itfUpload.js"></script>
<script> <script>
GALLERY_ID = {{ gallery.id }};
$(function() { $(function() {
$('#uploadImages').uploadQueue({'addURL': '/mediagallery/upload/', 'id': {{ gallery.id }} }); $('#uploadImages').uploadQueue({'addURL': '/mediagallery/upload/', 'id': GALLERY_ID });
//var itfQ = ItfUploadQueue({'addURL': '/mediagallery/upload/'}, $('#uploadImages')); //var itfQ = ItfUploadQueue({'addURL': '/mediagallery/upload/'}, $('#uploadImages'));
$('#uploadImages').delegate(".deleteItem", "click", function(e) {
if (!confirm("Are you sure you wish to delete this image?")) {
return false;
}
var $this = $(e.target);
//console.log($this);
var id = $this.parents('.photoContainer').find('.photoId').val();
var url = "/mediagallery/delete_photo/" + id;
$.get(url, {}, function(response) {
//alert("hi");
});
$this.parents('.photoContainer').fadeOut();
});
$('#uploadImages').delegate(".photoTitle", "blur", function(e) {
var $this = $(e.target);
//alert($this.val());
var title = $this.val();
var id = $this.parents('.photoContainer').find('.photoId').val();
var url = "/mediagallery/edit_photo/" + id;
$.get(url, {'title': title}, function(response) {
});
});
$('.galleryTitle').blur(function() {
var title = $(this).val();
var url = "/mediagallery/edit_title/" + GALLERY_ID;
$.get(url, {'title': title}, function(response) {
});
});
}); });
</script> </script>
<style> <style>
#uploadImages { #uploadImages {
min-width: 400px; min-width: 400px;
min-height: 300px; min-height: 300px;
max-width: 50%;
background: #f00; background: #f00;
} }
.progressBar {
height: 10px;
background: #00f;
}
.deleteItem {
font-weight: bold;
cursor: pointer;
}
</style> </style>
{% endblock %} {% endblock %}
@ -27,19 +76,20 @@ $(function() {
<div id="uploadImages"> <div id="uploadImages">
<input type="file" id="uploadImageFiles" multiple /> <input type="file" id="uploadImageFiles" multiple />
<div id="photosContainer"> <ul id="photosContainer">
{% for photo in photos %} {% for photo in photos %}
<div class="photoContainer"> <li class="photoContainer">
{% if photo.image %} {% if photo.image %}
{% thumbnail photo.image "100x100" crop="center" as thumb %} {% thumbnail photo.image "100x100" crop="center" as thumb %}
<img src="{{ thumb.url }}" /> <img src="{{ thumb.url }}" />
{% endthumbnail %} {% endthumbnail %}
{% endif %} {% endif %}
<input class="photoTitle" value="{{ photo.title }}" /> <input class="photoTitle" value="{{ photo.title }}" />
<span class="deleteItem">X</span>
<input type="hidden" class="photoId" value="{{ photo.id }}" /> <input type="hidden" class="photoId" value="{{ photo.id }}" />
</div> </li>
{% endfor %} {% endfor %}
</div> </ul>
</div> </div>
{% endblock %} {% endblock %}