editing files, deleting files, moving files works

This commit is contained in:
Sanj 2011-07-01 15:33:05 +05:30
parent 58f955d2bb
commit a9b0eec5a2
5 changed files with 106 additions and 16 deletions

View File

@ -206,6 +206,11 @@ class File(models.Model):
'''
return str(self.file)
def move_to(self, study):
self.categories.clear()
self.categories.add(study)
self.save()
return self
class Category(models.Model):
name = models.CharField(max_length=255)

View File

@ -5,10 +5,11 @@ urlpatterns = patterns('',
(r'addFolder', views.add_folder),
(r'upload', views.upload_files),
(r'add_category', views.add_category),
(r'add', views.add),
# (r'add', views.add),
(r'^([A-Z0-9].*)/chunk$', views.chunk),
(r'^json_edit', views.editFile),
(r'json_delete', views.deleteFile),
(r'^editFile/', views.editFile),
(r'^deleteFiles/', views.deleteFiles),
(r'^moveFiles/', views.moveFiles),
(r'json_list', views.fileList),
(r'browse', views.browse)
)

View File

@ -13,6 +13,10 @@ from ox.django.shortcuts import render_to_json_response
from django.shortcuts import get_object_or_404
from models import TYPE_CHOICES
from django.core.paginator import Paginator, InvalidPage, EmptyPage
try:
import json
except:
import simplejson as json
'''
class folder_names(object):
@ -207,8 +211,9 @@ def editFile(request):
errors.append("Invalid File ID")
keys = ['title', 'description', 'tags']
for k in keys:
val = request.POST[k]
fil.__setattr__(k, val)
val = request.POST.get(k, "")
if val != '':
fil.__setattr__(k, val)
fil.save()
response = {
'errors': errors
@ -218,19 +223,31 @@ def editFile(request):
@csrf_exempt
@login_required
def deleteFile(request):
def deleteFiles(request):
errors = []
try:
id = request.POST['id']
fil = File.objects.get(pk=id)
except:
errors.append("Invalid File ID")
fil.delete()
files = json.loads(request.POST.get("ids", "[]"))
for f in files:
fil = File.objects.get(pk=f)
fil.delete()
response = {
'errors': errors
}
return render_to_json_response(response)
@csrf_exempt
@login_required
def moveFiles(request):
errors = []
files = json.loads(request.POST.get("ids", "[]"))
study_id = int(request.POST.get("study", "0"))
study = Category.objects.get(pk=study_id)
for f in files:
fil = File.objects.get(pk=f)
fil.move_to(study)
return render_to_json_response(errors)
@csrf_exempt
def fileList(request):
category = request.POST.get("category", '')

View File

@ -1,5 +1,9 @@
$(function() {
// alert("hi");
var selectHTML = $('#id_category').html();
$('#moveSelectedSelect').html(selectHTML);
$('#filterForm').submit(function(e) {
e.preventDefault();
var formData = $(this).serializeArray();
@ -38,6 +42,39 @@ $(function() {
$('#filterForm').submit();
});
$('#deleteSelectedBtn').click(function(e) {
e.preventDefault();
var $this = $(this);
$this.attr("disabled", "disabled");
var ids = getSelectedIds();
var url = "/files/deleteFiles/";
var params = {'ids': JSON.stringify(ids)};
$.post(url, params, function(response) {
$this.removeAttr("disabled");
$('#filterForm').submit();
});
});
$('#moveSelectedBtn').click(function(e) {
e.preventDefault();
var $this = $(this);
$this.attr("disabled", "disabled");
var ids = getSelectedIds();
var url = "/files/moveFiles/";
var study_id = $('#moveSelectedSelect').val();
var params = {
'ids': JSON.stringify(ids),
'study': study_id
}
$.post(url, params, function(response) {
$this.removeAttr("disabled");
$('#filterForm').submit();
}, "json");
});
});
function getJQ(f) {
@ -80,12 +117,29 @@ function getItemForm(f) {
var $form = $('<div />').addClass("itemForm");
var $titleP = $('<p />').appendTo($form);
var $titleLabel = $('<span />').addClass("itemFormLabel").text("Title: ").appendTo($titleP);
var $titleInput = $('<input />').val(f.title).appendTo($titleP);
var $titleInput = $('<input />').addClass("itemTitle").val(f.title).appendTo($titleP);
var $descriptionP = $('<p />').appendTo($form);
var $descriptionLabel = $('<span />').addClass("itemFormLabel").text("Description: ").appendTo($descriptionP);
var $descriptionInput = $('<textarea />').val(f.description).appendTo($descriptionP);
var $descriptionInput = $('<textarea />').addClass("itemDescription").val(f.description).appendTo($descriptionP);
var $hiddenInput = $('<input />').addClass("itemId").attr("type", "hidden").val(f.id).appendTo($descriptionP);
var $submitP = $('<p />').appendTo($form);
var $submit = $('<input />').addClass("submitItem").attr("type", "submit").val("Submit").appendTo($submitP);
var $submitStatus = $('<span />').addClass("submitStatus").appendTo($submitP);
$submit.click(function(e) {
var url = "/files/editFile/";
var parent = $(this).parents('.itemForm');
$('.submitStatus').text("Submitting...");
$('.submitItem').attr("disabled", "disabled");
var params = {
'id': parent.find('.itemId').val(),
'title': parent.find('.itemTitle').val(),
'description': parent.find('.itemDescription').val()
}
$.post(url, params, function(response) {
$('.submitStatus').text('Saved');
$('.submitItem').removeAttr("disabled");
}, "json");
});
return $form;
}
@ -98,3 +152,16 @@ function getListElement(f) {
var $title = $('<span />').addClass("fileTitle").text(f.title).appendTo($item);
return $item;
}
function getSelectedIds() {
var ids = [];
$('.fileItem').each(function() {
var $this = $(this);
var checked = $this.find(".fileCheckbox").is(":checked");
if (checked) {
var id = $this.data("data").id;
ids.push(id);
}
});
return ids;
}

View File

@ -32,8 +32,8 @@
Selected Items: <button id="deleteSelectedBtn">Delete</button>
<p id="em">OR</p> <p>Move to:
<select id="moveSelectedSelect">
<option value="">...............</option>
</select></p>
</select> <button id="moveSelectedBtn">Move</button></p>
</div>
</div>
</div>