""" open/DurusWorks/proto/lib/ui/slash.qpy """ from datetime import datetime from os.path import join from pprint import pformat from proto.ui.forms import FormsDirectory from proto.ui.qwiki import QwikiDirectory from qp.fill.directory import Directory, get_crumb_tree from qp.fill.durus_directory import DurusDirectory from qp.fill.html import href from qp.fill.static import StaticFile, StaticDirectory from qp.pub.common import get_publisher, get_path, get_site from qp.pub.common import get_request, header, footer, page from qp.pub.common import get_response from qp.pub.common import get_session, get_user, redirect from qp.pub.publish import DurusPublisher, RespondNow class SitePublisher (DurusPublisher): configuration = dict( # Host and port of durus server. durus_address=('localhost', 7002), # Host and port of scgi server. scgi_address=('localhost', 7003), # Host and port of SSL service provided by Apache (or other). https_address=('', 443), # Location of database, log, and pid files. var_directory='/www/var', ) def display_exceptions(self): return True def ensure_signed_in(self): self.ensure_signed_in_using_digest() def header:xml(self, title, style=None, meta=None, **kwargs): '' '' '' '' % ( get_request().get_script_name(), '/proto.js') '' if meta: meta '%s' % title if style: '\n' % style '' '\n' % get_publisher().get_agent_class() '
' '
' title '
' format_menu() '
' '
' def footer:xml(self, *args, **kwargs): format_sessions() '
' # body_middle '
' get_request().get_url() if get_user(): href('%s/sign_out' % get_request().get_script_name(), 'Sign %s out.' % get_user().get_id(), style="float:right") '
' href('%s/restricted' % get_request().get_script_name(), "Ban %s" % get_request().get_remote_address()) '' def format_menu:xml(): try: crumb_tree = get_crumb_tree() except RespondNow: crumb_tree = [] if crumb_tree: ' > '.join([href(menu[0][0], menu[0][1], title=menu[0][2]) for menu in crumb_tree[:-1] if menu]) menu = crumb_tree[-1] component = get_path().split(str('/'))[-1] selected_path = './' + component '' class SiteDirectory (Directory): """ This site demonstrates authentication, sessions, static files, and exception reporting. """ def __init__(self): self.all_css = StaticFile( join(get_site().get_static_directory(), 'all.css'), mime_type='text/css', cache_time=1) self.favicon_png = StaticFile( join(get_site().get_static_directory(), 'favicon.png'), mime_type="image/png", cache_time=300) self.favicon_ico = StaticFile( join(get_site().get_static_directory(), 'favicon.ico'), mime_type="image/x-icon", cache_time=300) self.proto_js = StaticFile( join(get_site().get_static_directory(), 'proto.js'), mime_type="text/javascript", cache_time=10) self.static = StaticDirectory(join(get_site().get_static_directory()), list_directory=True) def get_exports(self): yield ('', 'index', 'Home', 'The Home page of this site.') yield ('exception', 'exception', 'Exception', 'Raise an exception') yield ('request', 'request', 'Request', 'View the request') yield ('hello', 'hello', 'Hello', 'A simple hello page.') yield ('hello_user', 'hello_user', 'Hello User', 'Authenticate for access. (requires SSL).') yield ('all.css', 'all_css', None, None) yield ('favicon.png', 'favicon_png', None, None) yield ('favicon.ico', 'favicon_ico', None, None) yield ('proto.js', 'proto_js', None, None) yield ('request_', 'request_', None, None) yield ('callback', 'callback', "Callback", "Javascript Callback Demo") yield ('sign_out', 'sign_out', None, None) yield ('restricted', 'ban', None, None) yield ('robots.txt', 'robots', None, None) yield ('database', 'database', 'Database', 'Browse Database') yield ('forms', 'forms', 'Form Demo', 'Basic Form Demonstration') yield ('qwiki', 'qwiki', 'Qwiki', 'A Quickie Wiki') yield ('static', 'static', 'Static', "The directory in the site package called 'static'.") database = DurusDirectory() forms = FormsDirectory() qwiki = QwikiDirectory() def robots:str(self): get_response().set_content_type('text/plain', 'utf-8') # Directions for robots. # Some areas aren't worth crawling 'User-agent: *\n' 'Disallow: /restricted\n' def ban(self): publisher = get_publisher() site = publisher.get_site() banned = site.get_banned() if not banned: redirect('.') session_cookie = get_request().get_cookie(site.get_name()) if session_cookie in get_publisher().get_sessions() : redirect('.') banned_addresses = open(banned, 'a') banned_addresses.write(' %s\n' % get_request().get_remote_address()) banned_addresses.close() get_publisher().send_to_administrator( 'Banned: %s\nTo remove, edit the following file:\n%s\n' % ( get_request().get_remote_address(), banned)) return page( 'Banned', 'You (or your software) is not being friendly to this site. ' 'To protect the site, we are no longer answering you. ' 'If you need to be un-banned, please contact us directly. ') def exception(self): raise Exception('This is an exception.') def sign_out(self): get_publisher().sign_out('/') def index:xml(self): header(get_site().get_name()) "This is the demo app named 'proto'." footer() load_time = datetime.utcnow() def hello:xml(self): header(get_site().get_name()) "Hello!" '

Time that this module was loaded: %s

' % self.load_time footer() def hello_user:xml(self): get_publisher().ensure_signed_in() header(get_site().get_name()) "Hello %s!" % get_user().get_id() footer() def callback:xml(self): header("callback demo") 'Send new request' '
' footer() def request_:xml(self): '
' items = sorted(get_request().__dict__.items()) for key, value in items: '
%s
' % key '
%s
' % pformat(value) '
body
' '
' # get_request().read_body() would be useful if you wanted # to parse the request body as xml, or json, or whatever. repr(get_request().read_body()) '
' '
' def request:xml(self): header('HTTP Request') self.request_() footer() def format_sessions:xml(): session_items = sorted(get_publisher().get_sessions().items()) if not session_items: '

No sessions.

' else: '' '' ('' '' '' '' '' '' '') for id, session in session_items: if get_session() is session: '' else: '' '' % id '' % session.get_owner().get_id() '' % session.get_effective_user().get_id() '' % session.get_authentication_time() '' % session.get_remote_address() '' '
Saved Sessions
session keyownereffective_userauthentication_timeremote_address
%s%s%s%s%s
'