From 07264983c632c17ea3311ffe10bde5b003f057af Mon Sep 17 00:00:00 2001 From: Sanj Date: Tue, 8 Feb 2011 18:57:17 +0530 Subject: [PATCH] add folder form --- edgware/editor/models.py | 6 ++-- edgware/editor/views.py | 4 +-- edgware/files/views.py | 69 ++++++++++++++++++++++++++++++++++++++++ edgware/urls.py | 1 + 4 files changed, 75 insertions(+), 5 deletions(-) diff --git a/edgware/editor/models.py b/edgware/editor/models.py index 8f31085..4e6da7e 100644 --- a/edgware/editor/models.py +++ b/edgware/editor/models.py @@ -9,7 +9,7 @@ from os.path import join from PIL import Image from django.template import Template, Context from django.template.loader import get_template -from settings import MEDIA_ROOT +from settings import MEDIA_ROOT, PROJECT_PATH from django.contrib.auth.models import User, Group from tagging.fields import TagField from tagging.models import Tag @@ -383,7 +383,7 @@ class Page(models.Model): 'id': v.id, 'title': v.fil.title, 'description': v.fil.description, - 'file': v.fil.file.url, + 'file': v.fil.file.url.replace(PROJECT_PATH, ""), 'mime': 'video' } videos.append(r) @@ -394,7 +394,7 @@ class Page(models.Model): 'id': a.id, 'title': a.fil.title, 'description': a.fil.description, - 'file': a.fil.file.url, + 'file': a.fil.file.url.replace(PROJECT_PATH, ""), 'mime': 'audio' } audios.append(r) diff --git a/edgware/editor/views.py b/edgware/editor/views.py index 2a0d7a9..4d9c377 100644 --- a/edgware/editor/views.py +++ b/edgware/editor/views.py @@ -13,7 +13,7 @@ except: from django.template import Template, Context from django.template.loader import get_template from os.path import join -from settings import MEDIA_ROOT, SITE_BASE, UPLOAD_ROOT +from settings import MEDIA_ROOT, SITE_BASE, UPLOAD_ROOT, PROJECT_PATH from PIL import Image import os from print_pdf import print_url_list @@ -413,7 +413,7 @@ def category_json(request): iconPath = "/static/images/binimages/jpg.jpg" resizedPath = filePath else: - filePath = r.file.url + filePath = r.file.url.replace(PROJECT_PATH, "") #FIXME: Why does file.url return system path and not relative URL?? iconPath = "/static/images/binimages/%s.jpg" % (r.type) resizedPath = filePath if r.type == 'video': diff --git a/edgware/files/views.py b/edgware/files/views.py index 48d0488..cc2b887 100644 --- a/edgware/files/views.py +++ b/edgware/files/views.py @@ -1,6 +1,75 @@ # Create your views here. from django.shortcuts import render_to_response from django.http import HttpResponseRedirect +from django.contrib.auth.decorators import login_required +from django import forms +from models import Category, File +from settings import UPLOAD_ROOT +import os +from os.path import join, isdir, getmtime, basename + +''' +class folder_names(object): + def __iter__(self): + return iter(map(lambda x: (x,x), os.listdir(UPLOAD_ROOT))) +''' + +def getFolderList(): + os.chdir(UPLOAD_ROOT) + dirs = filter(isdir, os.listdir(UPLOAD_ROOT)) + full_dirs = [join(UPLOAD_ROOT, d) for d in dirs] + full_dirs.sort(key=lambda x: getmtime(x), reverse=True) + return map(lambda x: (basename(x), basename(x)), full_dirs) + +class FolderSelect(forms.widgets.Select): + def _get_choices(self): + return getFolderList() +# return map(lambda x: (x,x), os.listdir(UPLOAD_ROOT)) + + def _set_choices(self, value): + self._choices = value + + choices = property(_get_choices, _set_choices) + + + +class FolderField(forms.ChoiceField): + widget = FolderSelect + + def _get_choices(self): + return getFolderList() +# return map(lambda x: (x,x), os.listdir(UPLOAD_ROOT)) + + +class AddFolderForm(forms.Form): + folder_name = FolderField(label="Name of Folder") + category = forms.ModelChoiceField(Category.objects, required=False, label="Study") + category_name = forms.CharField(required=False, label="Create New Study") + +@login_required +def add_folder(request): + if request.POST: + form = AddFolderForm(request.POST) + if form.is_valid(): + folder_name = form.cleaned_data['folder_name'] + category = form.cleaned_data['category'] + if not category: + category_name = form.cleaned_data['category_name'] + category = Category(name=category_name) + category.save() + user = request.user +# import pdb;pdb.set_trace() + File.addFiles(category, user, folder_name) + return render_to_response("files/added_folder.html", { + 'folder_name': folder_name, + 'category_name': category.name, + 'username': user.username + }) + else: + return render_to_response("files/add_folder.html", {'form': form}) + else: + form = AddFolderForm() + return render_to_response("files/add_folder.html", {'form': form}) def editor(request): return render_to_response("editor.html") diff --git a/edgware/urls.py b/edgware/urls.py index 401391b..6ec2d0c 100644 --- a/edgware/urls.py +++ b/edgware/urls.py @@ -17,6 +17,7 @@ urlpatterns = patterns('', # (r'^admin/doc/', include('django.contrib.admindocs.urls')), (r'^comments/', include('django.contrib.comments.urls')), (r'^edit/', include('editor.urls')), + (r'files/', include('files.urls')), (r'^gallery/', 'files.views.gallery'), # Uncomment the next line to enable the admin: (r'^admin/', include(admin.site.urls)),