Alvin Delagon
By: a guest | Apr 17th, 2009 | Syntax:
Python | Size: 1.62 KB | Hits: 98 | Expires: Never
import cherrypy
import time
class TweetEntry:
def __init__(self, subject, author, content):
self.subject = subject
self.author = author
self.content = content
self.tstamp = time.strftime("%Y-%m-%d %H:%M:%S")
def prettyPrint(self):
s = "<b>Subject: </b>%s<br/>" % (self.subject)
s += "<b>By: </b>%s<br/>" % (self.author)
s += "<b>At: </b>%s<br/>" % (self.tstamp)
s += "<b>Tweet: </b><br/>%s<br/>" % (self.content)
return (s)
class Tweet:
tweets = []
def index(self):
disp = "<h1>Welcome to Simple Twitter 1.0.0</h1>"
disp += "<form action='edit' method='POST'><input type='submit' value='New Post'/><br/><br/>"
for tweet in self.tweets:
disp += tweet.prettyPrint() + "<br/><br/>"
return (disp)
index.exposed = True
def edit(self):
return ("""
<h1>Compose new Tweet</h1>
<form action='publish' method='POST'>
Subject: <input type='text' name='subject' size='25'><br/><br/>
Author Name: <input type='text' name='author' size='25'><br/><br/>
Content: <br/><textarea name='content' rows=5 cols=80></textarea><br/><br/>
<input type='submit' value='Tweet!'/>
</form>
<br/>
""")
edit.exposed = True
def publish(self, subject, author, content):
self.tweets.append(TweetEntry(subject, author, content))
return ("""<meta http-equiv='REFRESH' content='0;url=index'>""")
publish.exposed = True
cherrypy.server.socket_port = 8000
cherrypy.quickstart(Tweet())