From 87f4f3dfd4f99c0150abcbdf27b88eb59c1e88e0 Mon Sep 17 00:00:00 2001
From: ianb
Date: Wed, 6 Jul 2005 09:08:10 +0000
Subject: Seperated out file types into more classes, with more delegation to
the class; added templates for particular views
---
examples/filebrowser/pathobj.py | 72 ++++++++++++++++++++++++----
examples/filebrowser/templates/edit_html.pt | 27 +++++++++++
examples/filebrowser/templates/edit_text.pt | 62 +++++++++++++++++++++++-
examples/filebrowser/templates/view_file.pt | 15 ++----
examples/filebrowser/templates/view_html.pt | 9 ++++
examples/filebrowser/templates/view_image.pt | 6 +++
examples/filebrowser/templates/view_text.pt | 7 +++
7 files changed, 176 insertions(+), 22 deletions(-)
create mode 100644 examples/filebrowser/templates/edit_html.pt
create mode 100644 examples/filebrowser/templates/view_html.pt
create mode 100644 examples/filebrowser/templates/view_image.pt
create mode 100644 examples/filebrowser/templates/view_text.pt
(limited to 'examples')
diff --git a/examples/filebrowser/pathobj.py b/examples/filebrowser/pathobj.py
index 015b882..15a7628 100644
--- a/examples/filebrowser/pathobj.py
+++ b/examples/filebrowser/pathobj.py
@@ -23,8 +23,15 @@ class PathContext(object):
ptype = 'dir'
else:
ptype = os.path.splitext(filename)[1]
- path_class = self.path_classes.get(
- ptype, self.path_classes['*'])
+ path_class = self.path_classes.get(ptype)
+ if not path_class:
+ mimetype, encoding = mimetypes.guess_type(filename)
+ if mimetype:
+ path_class = self.path_classes.get(mimetype)
+ if mimetype and not path_class:
+ path_class = self.path_classes.get(mimetype.split('/')[0]+'*')
+ if not path_class:
+ path_class = self.path_classes['*']
return path_class(path, filename, self)
@classmethod
@@ -46,6 +53,8 @@ class Path(sitepage.SitePage):
default_action='view_raw')
isdir = False
+ allow_edit = False
+ view_file_view = 'view_file.pt'
def __init__(self, path, filename, context):
super(Path, self).__init__()
@@ -71,6 +80,7 @@ class Path(sitepage.SitePage):
'url': self.pathurl.up(),
'name': self.pathurl.up().name() or 'root',
}
+ self.options.allow_edit = self.allow_edit
self.setup_file()
def setup_file(self):
@@ -79,12 +89,20 @@ class Path(sitepage.SitePage):
self.options.content = self.read()
else:
self.options.content = None
- self.options.is_image = mime and mime.startswith('image/')
self.options.use_iframe = mime == 'text/html'
def action_view(self):
- self.view = 'view_file.pt'
- self.options.allow_edit = self.mimetype.startswith('text/')
+ self.view = self.view_file_view
+
+ def action_download(self):
+ # @@: This loads the entire file into memory, but currently
+ # Wareweb has no streaming method -- really it should forward
+ # the request to a WSGI file-serving application
+ self.view = None
+ self.set_header('content-type', self.mimetype)
+ f = open(self.filename, 'rb')
+ self.write(f.read())
+ f.close()
def join(self, name):
parts = name.split('/')
@@ -121,11 +139,6 @@ class Path(sitepage.SitePage):
content = self.bad_regex.sub('', content)
self.write(content)
- def action_edit(self):
- self.view = 'edit_text.pt'
- self.options.content = self.read()
- self.options.action = str(self.pathurl)
-
def action_save(self):
content = self.fields.content
f = open(self.filename, 'wb')
@@ -142,6 +155,45 @@ class Path(sitepage.SitePage):
PathContext.register_class(Path)
+class Image(Path):
+
+ extensions = ['.png', '.gif', '.jpg', 'image/*']
+ view_file_view = 'view_image.pt'
+
+PathContext.register_class(Image)
+
+class TextFile(Path):
+
+ extensions = ['.txt', 'text/plain']
+ allow_edit = True
+ edit_view = 'edit_text.pt'
+
+ def action_view(self):
+ if self.fragment:
+ # The HTML iframe is nice in a fragment
+ self.view = 'view_html.pt'
+ else:
+ self.view = 'view_text.pt'
+
+ def action_edit(self):
+ self.view = self.edit_view
+ self.options.content = self.read()
+ self.options.action = str(self.pathurl)
+ self.options.ta_height = self.cookies.get('default_ta_height', 10)
+
+PathContext.register_class(TextFile)
+
+class HTMLFile(TextFile):
+
+ allow_edit = True
+ extensions = ['.html', '.htm', 'text/html']
+ edit_view = 'edit_html.pt'
+
+ def action_view(self):
+ self.view = 'view_html.pt'
+
+PathContext.register_class(HTMLFile)
+
class Dir(Path):
extensions = ['dir']
diff --git a/examples/filebrowser/templates/edit_html.pt b/examples/filebrowser/templates/edit_html.pt
new file mode 100644
index 0000000..10abb71
--- /dev/null
+++ b/examples/filebrowser/templates/edit_html.pt
@@ -0,0 +1,27 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/examples/filebrowser/templates/edit_text.pt b/examples/filebrowser/templates/edit_text.pt
index e2d7ba7..26ab12d 100644
--- a/examples/filebrowser/templates/edit_text.pt
+++ b/examples/filebrowser/templates/edit_text.pt
@@ -1,14 +1,72 @@
+
+
+
+
+
diff --git a/examples/filebrowser/templates/view_file.pt b/examples/filebrowser/templates/view_file.pt
index d6ad93a..6872ffa 100644
--- a/examples/filebrowser/templates/view_file.pt
+++ b/examples/filebrowser/templates/view_file.pt
@@ -1,3 +1,4 @@
+
@@ -15,15 +16,9 @@ Mimetype:
-content
-
-
-
-
+
+
-
\ No newline at end of file
+
+
\ No newline at end of file
diff --git a/examples/filebrowser/templates/view_html.pt b/examples/filebrowser/templates/view_html.pt
new file mode 100644
index 0000000..b467426
--- /dev/null
+++ b/examples/filebrowser/templates/view_html.pt
@@ -0,0 +1,9 @@
+
+
+
+
+
+
+
diff --git a/examples/filebrowser/templates/view_image.pt b/examples/filebrowser/templates/view_image.pt
new file mode 100644
index 0000000..193be66
--- /dev/null
+++ b/examples/filebrowser/templates/view_image.pt
@@ -0,0 +1,6 @@
+
+
+
+
+
+
diff --git a/examples/filebrowser/templates/view_text.pt b/examples/filebrowser/templates/view_text.pt
new file mode 100644
index 0000000..7606d4f
--- /dev/null
+++ b/examples/filebrowser/templates/view_text.pt
@@ -0,0 +1,7 @@
+
+
+content
+
+
+
--
cgit v1.2.1