#!/usr/bin/env python """ Test: To run: $ python ./test_text.py """ import urllib from quixote.publish import Publisher from quixote.directory import Directory from quixote import get_request, redirect from quixote.errors import TraversalError from quixote.html import TemplateIO, url_quote from quixote.form import Form, StringWidget from durus.file_storage import FileStorage from durus.connection import Connection #### class TestPublisher(Publisher): def __init__(self, *args, **kwargs): Publisher.__init__(self, *args, **kwargs) # Get test_db_conn self.db_conn = Connection(FileStorage("./test_text.durus")) def cleanup(self): del self.db_conn def start_request(self): request = get_request() request.response.set_charset('UTF-8') request.db_conn = self.db_conn #### class RootDirectory(Directory): _q_exports = ['', 'hello'] def _q_index(self): request = get_request() f = _define_form(request) r = TemplateIO(html=False) if not f.is_submitted() or f.has_errors(): r += header(request, request.get_path()) r += f.render() r += footer(request) else: sbmt = f.get_submit() if sbmt == 'add': request.db_conn.get_root()[f['key']] = request.form request.db_conn.commit() redirect(f['key']) return r.getvalue() def _q_lookup(self, component): return TextItemUI(urllib.unquote(component)) class TextItemUI(Directory): _q_exports=[''] def __init__(self, component): request = get_request() self.root = request.db_conn.get_root() self.item = self.root.get(component) if self.item is None: raise TraversalError('No such key: %s' %(component)) self.key = component self.atts = ('en','fr','es') print request.form def _q_index (self): request = get_request() r = TemplateIO(html=False) r += header(request, request.get_path()) f = _define_form(request, self.key) sbmt = f.get_submit() if not sbmt: for att in self.atts: f.get_widget(att).set_value( self.item.get(att) ) elif sbmt == 'modify': for att in self.atts: self.item[ att ] = f[att] request.db_conn.get_root()[self.key] = self.item get_request().db_conn.commit() elif sbmt == 'delete': del self.root[self.key] del self.item get_request().db_conn.commit() redirect('../') elif sbmt == 'clear': redirect('../') r += f.render() r += footer(request) return r.getvalue() #### def _define_form(request, key=None): f = Form() if key is None: f.add(StringWidget,"key",title="Key",size=20,required=True) else: f.add(StringWidget,"key",title="Key",size=20,required=True, value=key, readonly="readonly") for att in ('en','fr','es'): f.add(StringWidget, att, title=att, size=60) if key is None: f.add_submit("add", 'Add' ) else: f.add_submit('clear', 'Clear' ) f.add_submit('delete', 'Delete' ) f.add_submit('modify', 'Modify' ) return f def header (request, title): r = TemplateIO(html=False) r += ''' Test Text: %(title)s

Test Text: %(title)s

''' % locals() return r.getvalue() def footer (request): r = TemplateIO(html=False) r += '

Keys in root PersistentDict

' r += '''''' return r.getvalue() #### def create_publisher(): return TestPublisher(RootDirectory(), display_exceptions='plain') if __name__ == '__main__': from quixote.server.simple_server import run print 'Listening on http://localhost:8081/' run(create_publisher, host='localhost', port=8081) ####