Commits

Joseph Tate committed a8f9c99

Simple wrapper around tw2.jqplugins.elfinder

Comments (0)

Files changed (5)

+# See http://peak.telecommunity.com/DevCenter/setuptools#namespace-packages
+__import__('pkg_resources').declare_namespace(__name__)

c5t/fileadmin/__init__.py

Empty file added.

c5t/fileadmin/controllers.py

+import os
+from c5t.core.lib.base import BaseController
+from tg import tmpl_context, config, expose, request, response
+import tw2.jqplugins.elfinder.widgets as elf_widgets
+import tw2.jqplugins.elfinder.connector as elf_connector
+
+import simplejson
+
+def normalize_api_url(url):
+    if not url.endswith('/'):
+        url = url[:url.rfind('/')]
+    url += 'api'
+    return url
+
+def cleanup_filename(filename):
+    'Some browsers (IE) send a full windows path.  This cleans those up.'
+    filen = filename.replace('\\', '/')
+    #Not sure if this will work on non POSIX systems
+    return os.path.basename(filen)
+
+
+def fix_uploads(kw):
+    if 'upload[]' in kw:
+        nnew = dict()
+        for item in kw.get('upload[]'):
+            if isinstance(item, basestring):
+                pass
+            else:
+                filen = cleanup_filename(item.filename)
+                nnew[filen] = item.file
+        kw['upload[]'] = nnew
+    return kw
+
+class FileAdminController(BaseController):
+    ''''''
+    def __init__(self, root):
+        self.root = root
+        self.file_widget = elf_widgets.elFinderWidget(id='file_admin_controller', root=root)
+        self.elf_connector = elf_connector.elFinder.connector({'root': self.root})
+
+    @expose('c5t.fileadmin.templates.index')
+    def index(self):
+        apiurl = normalize_api_url(request.url)
+        tmpl_context.widget = self.file_widget
+        return dict(apiurl=apiurl)
+
+    @expose()
+    def api(self, **kw):
+        kw = fix_uploads(kw)
+        status, headers, cresponse = self.elf_connector.run(kw)
+        response.headers.update(headers)
+        response.status_int = status
+        if status == 200 and not cresponse is None:
+            if 'file' in cresponse and isinstance(cresponse['file'], file):
+                return elf_connector.file_streamer(cresponse['file'])
+        return simplejson.dumps(cresponse)

c5t/fileadmin/templates/__init__.py

Empty file added.

c5t/fileadmin/templates/index.mak

+<%inherit file="local:templates.master" />
+<%! from c5t.core.lib import helpers as c5t_h %>
+
+<h1 class="underlined-gray">News</h1>
+
+%if c5t_h.can_add(None):
+<a href="new">New Item</a>
+%endif
+${tmpl_context.widget.display(url=apiurl)|n}