view project1/project1.py @ 25:4a4bd554b4c1 tip

Closing this sub branch.
author Daniele Barchiesi <daniele.barchiesi@eecs.qmul.ac.uk>
date Mon, 25 Mar 2013 14:02:54 +0000
parents 032bc65ebafc
children
line wrap: on
line source
"""
Tutorial - Multiple objects

This tutorial shows you how to create a site structure through multiple
possibly nested request handler objects.
"""

import cherrypy, os
from cherrypy.lib.static import serve_file

PATH = os.path.abspath(os.path.dirname(__file__))
class Root(object): pass
cherrypy.tree.mount(Root(), '/mp3/beat.mp3', config={
        '/': {
                'tools.staticdir.on': True,
                'tools.staticdir.dir': PATH,
                'tools.staticdir.index': 'mp3/beat.mp3',
            },
    })



class HomePage:

	
	def index(self):
		return '''
			<p>Hi, this is the home page! Check out the other
			fun stuff on this site:</p>
			
			<ul>
				<li><a href="/joke/">A silly joke</a></li>
				<li><a href="/links/">Useful links</a></li>
				<li><a href="/audio/">Audio</a></li>
			</ul>'''
	index.exposed = True


class MP3Page:
	def index(self):
		filepath = '/Users/tmc/Documents/LiveMesh/cherry/beat.mp3'
		return serve_file(filepath, "application/x-download", "attachment")
	index.exposed = True



class AudioPage:
	def index(self):
		#mp3file = 'http://www.voanews.com/MediaAssets2/learningenglish/2011_08/se-exp-death-valley-fixed-03aug11.mp3'
		#mp3file = "/Users/tmc/Documents/LiveMesh/cherry/mp3/mp3_1.html"
		#return open(mp3file)
		mp3file = 'http://localhost:8080/mp3/beat.mp3'
		return '''
			<embed src="%s" width="300"/>''' %mp3file
	index.exposed = True
	
class JokePage:
	def index(self):
		return '''
			<p>"In Python, how do you create a string of random
			characters?" -- "Read a Perl file!"</p>
			<p>[<a href="../">Return</a>]</p>'''
	index.exposed = True


class LinksPage:
	def __init__(self):
		# Request handler objects can create their own nested request
		# handler objects. Simply create them inside their __init__
		# methods!
		self.extra = ExtraLinksPage()
	
	def index(self):
		# Note the way we link to the extra links page (and back).
		# As you can see, this object doesn't really care about its
		# absolute position in the site tree, since we use relative
		# links exclusively.
		return '''
			<p>Here are some useful links:</p>
			
			<ul>
				<li><a href="http://www.cherrypy.org">The CherryPy Homepage</a></li>
				<li><a href="http://www.python.org">The Python Homepage</a></li>
			</ul>
			
			<p>You can check out some extra useful
			links <a href="./extra/">here</a>.</p>
			
			<p>[<a href="../">Return</a>]</p>
		'''
	index.exposed = True


class ExtraLinksPage:
	def index(self):
		# Note the relative link back to the Links page!
		return '''
			<p>Here are some extra useful links:</p>
			
			<ul>
				<li><a href="http://del.icio.us">del.icio.us</a></li>
				<li><a href="http://www.mornography.de">Hendrik's weblog</a></li>
			</ul>
			
			<p>[<a href="../">Return to links page</a>]</p>'''
	index.exposed = True


# Of course we can also mount request handler objects right here!
root = HomePage()
root.joke = JokePage()
root.links = LinksPage()
root.audio = AudioPage()
root.mp3 = MP3Page()

# Remember, we don't need to mount ExtraLinksPage here, because
# LinksPage does that itself on initialization. In fact, there is
# no reason why you shouldn't let your root object take care of
# creating all contained request handler objects.


import os.path
tutconf = os.path.join(os.path.dirname(__file__), '/Users/tmc/Documents/LiveMesh/cherry/CherryPy-3.2.0/py2/cherrypy/tutorial/tutorial.conf')

if __name__ == '__main__':
	# CherryPy always starts with app.root when trying to map request URIs
	# to objects, so we need to mount a request handler root. A request
	# to '/' will be mapped to HelloWorld().index().
	cherrypy.quickstart(root, config=tutconf)
else:
	# This branch is for the test suite; you can ignore it.
	cherrypy.tree.mount(root, config=tutconf)