diff options
| author | ianb <devnull@localhost> | 2005-07-06 09:08:10 +0000 |
|---|---|---|
| committer | ianb <devnull@localhost> | 2005-07-06 09:08:10 +0000 |
| commit | 87f4f3dfd4f99c0150abcbdf27b88eb59c1e88e0 (patch) | |
| tree | 38f7aa80426622b3317463fbf453c639b17ac102 /examples | |
| parent | 46217776dca22bfaa3c6ef083743fa36db99db35 (diff) | |
| download | paste-87f4f3dfd4f99c0150abcbdf27b88eb59c1e88e0.tar.gz | |
Seperated out file types into more classes, with more delegation to the class; added templates for particular views
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/filebrowser/pathobj.py | 72 | ||||
| -rw-r--r-- | examples/filebrowser/templates/edit_html.pt | 27 | ||||
| -rw-r--r-- | examples/filebrowser/templates/edit_text.pt | 62 | ||||
| -rw-r--r-- | examples/filebrowser/templates/view_file.pt | 15 | ||||
| -rw-r--r-- | examples/filebrowser/templates/view_html.pt | 9 | ||||
| -rw-r--r-- | examples/filebrowser/templates/view_image.pt | 6 | ||||
| -rw-r--r-- | examples/filebrowser/templates/view_text.pt | 7 |
7 files changed, 176 insertions, 22 deletions
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 @@ +<html metal:use-macro="here/standard_template.pt/macros/page"> + +<metal:head fill-slot="extra_head"> +<span tal:replace="structure servlet/setup_xinha"/> +<script type="text/javascript"> +xinha_editors = ['txt-content']; + +</script> +</metal:head> + +<metal:body fill-slot="body"> + +<form tal:attributes="action options/action" method="POST"> +<input type="hidden" name="action" value="save"> +Edit:<br> +<textarea name="content" id="txt-content" cols=60 style="width: 100%" + tal:attributes="rows options/ta_height" + tal:content="options/content"></textarea> +<br> +<input type="submit" value="Save"> +<button tal:attributes="onclick + servlet/pathurl/action=view/js_set_location">Cancel</button> + +</form> + +</metal:body> +</html>
\ 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 @@ <html metal:use-macro="here/standard_template.pt/macros/page"> + +<metal:head fill-slot="extra_head"> +<script type="text/javascript"> + +function adjust_textarea(id, adjustment) { + var el = $(id); + current = parseInt(el.getAttribute('rows')); + current += adjustment; + if (current < 2) { + current = 2; + } + el.setAttribute('rows', current); + createCookie('default_ta_height', current, 365*2); + return false; +} + +function find_text(txt_id, input_id) { + var textarea = $(txt_id); + var txt = textarea.value.toLowerCase(); + var input = $(input_id); + input.style.backgroundColor = '#fff'; + var search_term = input.value.toLowerCase(); + if (! search_term) { + return false; + } + var search_txt = txt; + var offset = 0; + if (textarea.selectionEnd - textarea.selectionStart) { + search_txt = txt.substring(textarea.selectionEnd); + offset = textarea.selectionEnd; + } + var pos = search_txt.indexOf(search_term); + if (pos == -1) { + input.style.backgroundColor = '#fdd'; + textarea.selectionStart = textarea.selectionEnd = 0; + return false; + } + textarea.selectionStart = pos + offset; + textarea.selectionEnd = pos + offset + search_term.length; + return false; +} + +</script> +</metal:head> + <metal:body fill-slot="body"> <form tal:attributes="action options/action" method="POST"> <input type="hidden" name="action" value="save"> Edit:<br> -<textarea name="content" rows=10 cols=60 style="width: 100%" +<textarea name="content" id="txt-content" cols=60 style="width: 100%" + tal:attributes="rows options/ta_height" tal:content="options/content"></textarea> <br> <input type="submit" value="Save"> -<button tal:attributes="onclick servlet/pathurl/js_set_location">Cancel</button> +<button tal:attributes="onclick + servlet/pathurl/action=view/js_set_location">Cancel</button> +<button onclick="return adjust_textarea('txt-content', 1)">+</button> +<button onclick="return adjust_textarea('txt-content', -1)">-</button> +<a href="#" onclick="toggle('find-txt', {link: this}); return false" + shownhtml="&lt;&lt;&lt;" + style="text-decoration: none; color: #226">find...</a> +<span id="find-txt" style="display: none"> +<input type="text" id="find-input" style="width: 5em"> +<button onclick="return find_text('txt-content', + 'find-input')">find</button> +</span> + </form> </metal:body> 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 @@ +<metal:tpl define-macro="view"> <html metal:use-macro="here/standard_template.pt/macros/page"> <metal:body fill-slot="body"> @@ -15,15 +16,9 @@ Mimetype: <span tal:replace="servlet/mimetype"/><br> </p> -<pre tal:condition="python: options['content'] and not options['use_iframe']" - tal:content="options/content">content</pre> - -<img tal:condition="options/is_image" - tal:attributes="src servlet/pathurl/action=download"> - -<iframe tal:condition="options/use_iframe" - tal:attributes="src servlet/pathurl/action=download/html=clean" - width="100%" height="200" name="test"></iframe> +<metal:slot define-slot="view_inline"> +</metal:slot> </metal:body> -</html>
\ No newline at end of file +</html> +</metal:tpl>
\ 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 @@ +<html metal:use-macro="here/view_file.pt/macros/view"> +<metal:slot fill-slot="view_inline"> +<iframe width="100%" height="200" name="test" + tal:attributes="src + servlet/pathurl/action=download/html=clean"></iframe> + +</metal:slot> +</html> + 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 @@ +<html metal:use-macro="here/view_file.pt/macros/view"> +<metal:slot fill-slot="view_inline"> +<img tal:attributes="src servlet/pathurl/action=download"> +</metal:slot> +</html> + 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 @@ +<html metal:use-macro="here/view_file.pt/macros/view"> +<metal:slot fill-slot="view_inline"> +<pre style="overflow: auto" + tal:content="options/content">content</pre> +</metal:slot> +</html> + |
