From a9b0eec5a28b1959b0e1469a76508be6d27c1e9d Mon Sep 17 00:00:00 2001 From: Sanj Date: Fri, 1 Jul 2011 15:33:05 +0530 Subject: [PATCH] editing files, deleting files, moving files works --- edgware/files/models.py | 5 ++ edgware/files/urls.py | 7 +-- edgware/files/views.py | 35 ++++++++++---- edgware/static/js/files/browse.js | 71 +++++++++++++++++++++++++++- edgware/templates/files/browser.html | 4 +- 5 files changed, 106 insertions(+), 16 deletions(-) diff --git a/edgware/files/models.py b/edgware/files/models.py index 401aa04..de7e98d 100755 --- a/edgware/files/models.py +++ b/edgware/files/models.py @@ -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) diff --git a/edgware/files/urls.py b/edgware/files/urls.py index 1bab731..6c68312 100644 --- a/edgware/files/urls.py +++ b/edgware/files/urls.py @@ -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) ) diff --git a/edgware/files/views.py b/edgware/files/views.py index 70d51d0..bf34b38 100644 --- a/edgware/files/views.py +++ b/edgware/files/views.py @@ -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", '') diff --git a/edgware/static/js/files/browse.js b/edgware/static/js/files/browse.js index 95ab538..e3b7579 100644 --- a/edgware/static/js/files/browse.js +++ b/edgware/static/js/files/browse.js @@ -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 = $('
').addClass("itemForm"); var $titleP = $('

').appendTo($form); var $titleLabel = $('').addClass("itemFormLabel").text("Title: ").appendTo($titleP); - var $titleInput = $('').val(f.title).appendTo($titleP); + var $titleInput = $('').addClass("itemTitle").val(f.title).appendTo($titleP); var $descriptionP = $('

').appendTo($form); var $descriptionLabel = $('').addClass("itemFormLabel").text("Description: ").appendTo($descriptionP); - var $descriptionInput = $('