Created
August 13, 2012 22:33
-
-
Save snay2/3344551 to your computer and use it in GitHub Desktop.
cs462server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto | |
from boto.sqs.message import RawMessage | |
import ConfigParser | |
import json | |
import urllib, urllib2 | |
#read config | |
config = ConfigParser.ConfigParser() | |
config.read(["/etc/boto.cfg"]) | |
def image(req): | |
sqs = boto.connect_sqs() | |
q = sqs.create_queue(config.get('sqs', 'approvalqueue')) | |
q.set_message_class(RawMessage) | |
details = '{"imagekey": "none"}' | |
while True: | |
msg = q.read() | |
if not msg: | |
break | |
details = msg.get_body() | |
msg.delete() | |
if _isValidImageMessage(details): | |
break | |
imagekey = json.loads(details)['imagekey'] | |
if imagekey != "none": | |
_logMessage("Sending image %s to approval client (/nextapproval/image)" % imagekey) | |
req.content_type = "text/plain" | |
return details | |
def comment(req): | |
sqs = boto.connect_sqs() | |
q = sqs.create_queue(config.get('sqs', 'commentqueue')) | |
q.set_message_class(RawMessage) | |
details = '{"commentkey": "none"}' | |
while True: | |
msg = q.read() | |
if not msg: | |
break | |
details = msg.get_body() | |
msg.delete() | |
if _isValidCommentMessage(details): | |
break | |
commentkey = json.loads(details)['commentkey'] | |
if commentkey != "none": | |
_logMessage("Sending comment %s to approval client (/nextapproval/comment)" % commentkey) | |
req.content_type = "text/plain" | |
return details | |
def _isValidImageMessage(msg): | |
details = json.loads(msg) | |
return not(details['imagekey'] is None and \ | |
details['imageURL'] is None and \ | |
details['imageheight'] is None and \ | |
details['imagewidth'] is None and \ | |
details['tag'] is None and \ | |
details['description'] is None and \ | |
details['submituser'] is None and \ | |
details['submitdate'] is None) | |
def _isValidCommentMessage(msg): | |
details = json.loads(msg) | |
return not(details['commentkey'] is None and \ | |
details['submitdate'] is None and \ | |
details['comment'] is None and \ | |
details['submituser'] is None) | |
def _logMessage(message): | |
print message | |
l = urllib.urlencode({'student': "TA", 'type': "INFO", 'system': "processingserver", 'message':message}) | |
f = urllib2.urlopen("http://localhost:8080/log/submit", l) | |
def testimage(req): | |
data = dict({ | |
"imagekey": "57db562c-c0c8-11dd-95f2-123139005037", | |
"imageURL": "http://google.com/", | |
"imageheight": "350", | |
"imagewidth": "350", | |
"tag": "something", | |
"description": "A description", | |
"submituser": "snay2", | |
"submitdate": "today" | |
}) | |
req.content_type = "text/plain" | |
return json.dumps(data) | |
def testcomment(req): | |
data = dict({ | |
"commentkey":"guid", | |
"submitdate":"submitdate", | |
"comment":"Some bogus comment", | |
"submituser":"snay2" | |
}) | |
req.content_type = "text/plain" | |
return json.dumps(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment