Posted by Alvin Delagon on Fri 17 Apr 08:51 (modification of post by Alvin Delagon view diff)
report abuse | download | new post
- """
- @name: tweet.py
- @summary: A simple micro-blogging software using CherryPy
- @author: Alvin Delagon
- """
- 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())
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.