Skip to content

Instantly share code, notes, and snippets.

@snay2
Created February 13, 2011 04:09
Show Gist options
  • Save snay2/824428 to your computer and use it in GitHub Desktop.
Save snay2/824428 to your computer and use it in GitHub Desktop.
Example using SimpleDB and the logging server
<p>Submitted an automated comment to that image.
Go <a href="http://imaj.lddi.org/view?imagekey=$key">view it</a>.</p>
<p><a href="/simpldb">Return</a></p>
<p>The most recent image has imagekey $key. You can view it
<a href="http://imaj.lddi.org/view?imagekey=$key">here</a>.</p>
<p><a href="/simpledb/comment">Submit a comment for this image</a></p>
import uuid
import time
import boto
import funcs
import urllib
import urllib2
from Cheetah.Template import Template
AWSKey = "{redacted}"
AWSSecret = "{redacted}"
imageDomain = "picture"
commentDomain = "comment"
#################################################
# SimpleDB: Image domain
#################################################
def index(req):
# Get the imagekey of the most recently submitted image
image = _getMostRecentImage()
imagekey = image.name
# Return the response in a template
t = Template(file='/var/www/examples/index.html')
t.key = imagekey
req.content_type = "text/html"
return t.__str__()
def _getImageDomain():
sdb = boto.connect_sdb(AWSKey, AWSSecret)
domain = sdb.lookup(imageDomain)
return domain
def _getMostRecentImage():
domain = _getImageDomain()
# Query for all approved images with any submit date (magic number), sorted by submit date
query = "['submitdate' <= '2900'] intersection ['status' = 'approved'] sort 'submitdate' desc"
# Get one row
numRows = 1
# Run the query
result = domain.query(query, numRows)
# Result is a generator; get
recentImages = [i for i in result]
return recentImages[0]
#################################################
# SimpleDB: Comment domain
#################################################
def comment(req):
image = _getMostRecentImage()
imagekey = image.name
# Generate a UUID for this comment
commentkey = uuid.uuid1()
# Create a new item in the comment domain
domain = _getCommentDomain()
c = domain.new_item(commentkey)
c['submituser'] = 'ExampleServer'
c['imagekey'] = imagekey
c['comment'] = 'This is an automated comment from an example server'
c['submitdate'] = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime())
c.save()
# Put the comment in the approval queue (implementation not important for Lab 3)
funcs.addCommentToQueue(commentkey, c)
# Return the response in a template
t = Template(file='/var/www/examples/comment.html')
t.key = imagekey
req.content_type = "text/html"
req.write(t.__str__())
def _getCommentDomain():
sdb = boto.connect_sdb(AWSKey, AWSSecret)
domain = sdb.lookup(commentDomain)
return domain
#################################################
# Logging
#################################################
def log(req):
return _postProcessingMessage()
def _postProcessingMessage():
params = urllib.urlencode({'student': 'ExampleServer', \
'type': 'INFO', \
'system': 'appserver', \
'message': 'This is a test logging message sent for example purposes'})
return urllib2.urlopen('{redacted}', params).read()
@snay2
Copy link
Author

snay2 commented Feb 15, 2011

The AWS keys and the location of the logging server endpoint were redacted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment