basics of file browser working

This commit is contained in:
Sanj 2011-06-29 19:36:10 +05:30
parent 1c8811df84
commit 2710ff5709
6 changed files with 164 additions and 1 deletions

View File

@ -10,7 +10,9 @@ from django.db.models.signals import post_save
from settings import UPLOAD_ROOT, MEDIA_ROOT
from utils.add_file import hashFile, fileInfo, getFileType
from django.core.files.base import ContentFile
import operator
from django.db.models import Q
from ox.text import smartSplit
#FIXME: The following two functions are ridiculous. please remove and clean up all references to them.
def baseFileName(filename):
@ -21,9 +23,17 @@ def extFileName(filename):
r = filename.rindex('.') + 1
return filename[r:]
def splitSearch(string):
ret = []
for s in smartSplit(string):
word = s.replace("'", "").replace('"', '')
ret.append(word)
return ret
MIME_TYPES = (
('jpg', 'Image'),
('png', 'Image'),
('ogv', 'Video'),
('mp3', 'Audio (mp3)'),
('ogg', 'Audio (Ogg)'),
@ -37,6 +47,7 @@ TYPE_CHOICES = (
('audio', 'Audio'),
('text', 'Text'),
('video', 'Video'),
('other', 'Other'),
)
class Folder(models.Model):
@ -88,6 +99,41 @@ class File(models.Model):
# folder = models.ForeignKey("Folder")
info = models.CharField(max_length=1024, blank=True, null=True)
fts_fields = ['title', 'description']
@classmethod
def filter_category(kls, category_id, qset):
return qset.filter(categories=category_id)
@classmethod
def filter_type(kls, typ, qset):
return qset.filter(type__iexact=typ)
@classmethod
def fts(kls, search, qset):
terms = splitSearch(search)
qobjects = []
for t in terms:
for f in kls.fts_fields:
qstring = f + '__icontains'
qobject = Q(**{qstring:t})
qobjects.append(qobject)
return qset.filter(reduce(operator.or_, qobjects))
def get_dict(self):
#FIXME
try:
url = self.file.url
except:
url = ''
return {
'id': self.id,
'url': url,
'title': self.title,
'description': self.description,
'type': self.type
}
def save_chunk(self, chunk, name='data.bin'):
if not self.done:
if not self.file:

View File

@ -7,6 +7,10 @@ urlpatterns = patterns('',
(r'add_category', views.add_category),
(r'add', views.add),
(r'^([A-Z0-9].*)/chunk$', views.chunk),
(r'^json_edit', views.editFile),
(r'json_delete', views.deleteFile),
(r'json_list', views.fileList),
(r'browse', views.browse)
)
# urlpatterns += patterns('views',

View File

@ -11,6 +11,7 @@ import shutil
from django.views.decorators.csrf import csrf_exempt
from ox.django.shortcuts import render_to_json_response
from django.shortcuts import get_object_or_404
from models import TYPE_CHOICES
'''
class folder_names(object):
@ -76,6 +77,11 @@ class CategoryForm(forms.Form):
category = forms.ModelChoiceField(Category.objects, required=False, label="Study")
category_name = forms.CharField(required=False, label="Create New Study")
class FileFilterForm(forms.Form):
category = forms.ModelChoiceField(Category.objects, required=False, label="Study")
fileType = forms.ChoiceField(TYPE_CHOICES, required=False, label="File Type")
search = forms.CharField(required=False)
@login_required
def add_folder(request):
if request.POST:
@ -207,3 +213,45 @@ def editFile(request):
'errors': errors
}
return render_to_json_response(response)
@csrf_exempt
@login_required
def deleteFile(request):
errors = []
try:
id = request.POST['id']
fil = File.objects.get(pk=id)
except:
errors.append("Invalid File ID")
fil.delete()
response = {
'errors': errors
}
return render_to_json_response(response)
def fileList(request):
category = request.POST.get("category", False)
typ = request.POST.get("type", False)
search = request.POST.get("search", False)
page = request.POST.get("page", 1)
qset = File.objects.all()
if category:
qset = File.filter_category(category, qset)
if typ:
qset = File.filter_type(typ, qset)
if search:
qset = File.fts(search, qset)
d = {}
d['files'] = []
for f in qset:
d['files'].append(f.get_dict())
return render_to_json_response(d)
def browse(request):
form = FileFilterForm()
return render_to_response("files/browser.html", {
'filterForm': form
})

View File

@ -0,0 +1,5 @@
#previewPane {
position: absolute;
left: 250px;
top: 10px;
}

View File

@ -0,0 +1,34 @@
$(function() {
$('#filterForm').submit(function(e) {
e.preventDefault();
var formData = $(this).serializeArray();
$.post("files/json_list", formData, function(response) {
var data = JSON.parse(response);
var files = data.files;
// console.log(files);
$('#fileList').empty();
for (var i=0; i<files.length; i++) {
// console.log(files[i]);
var $file = getJQ(files[i]);
$('#fileList').append($file);
}
});
});
});
function getJQ(f) {
var jq = $("<div />").data("data", f).addClass("fileItem").text(f.type + ": " + f.title);
jq.click(function() {
var d = $(this).data("data");
switch (d.type) {
case "image":
var html = "<img src='" + d.url + "' />";
break;
default:
var html = "<a href='" + d.url + "'>Click to view</a>";
}
$('#previewPane').html(html);
});
return jq;
}

View File

@ -0,0 +1,26 @@
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="/static/css/files/browse.css" />
<script type="text/javascript" src="/static/js/jquery.js"></script>
<script type="text/javascript" src="/static/js/files/browse.js"></script>
</head>
<body>
<div id="leftCol">
<div id="fileFilters">
<form id="filterForm">
{{ filterForm.as_p }}
<p>
<input type="submit" value="Submit" />
</p>
</form>
</div>
<div id="fileList">
</div>
</div>
<div id="previewPane">
</div>
</body>
</html>