def is_ready(request): request.content_type = "application/json" db = DBConnection(autocommit=False) form = util.FieldStorage(request) assignmentid = form['assignmentid'].value workerid = form['workerid'].value if form.has_key('videoid'): videoid = int(form['videoid'].value) else: is_slow = form.has_key('slow') and form['slow'] == "1" result = unlabeledVideos(db, is_slow) logging.debug(result) if is_slow: # have I already labeled this video? result = filter(lambda x: not haveCompleted(x['pk'], workerid, db), result) if len(result) == 0: request.write(json.dumps({'is_ready': False})) return else: # grab the most recent video upload logging.debug("Videos needing labels: " + str(result)) videoid = result[0]['pk'] video = getAndAssignVideo(assignmentid, videoid) db.commit() request.write(json.dumps(video, cls=location_ping.DecimalEncoder))
def locationPing(request): request.content_type = "application/json" db = DBConnection(autocommit = False) start = datetime.now() try: (phase, assignment, location, video_id) = getArgs(request) servertime = unixtime(datetime.now()) # logging.debug("Location %s for video %s on phase %s" % (location, video_id, phase)) cur_phase = getMostRecentPhase(video_id, db) # Ensure that there isn't already a newer phase that # we should be returning to the client if cur_phase['phase'] != phase: request.write(json.dumps(cur_phase, cls=DecimalEncoder)) else: # Record where we are pushLocation(location, phase, assignment, video_id, servertime, db) # Has the phase already converged? (i.e., too small to be divisible) (is_new_phase, new_min, new_max) = compareLocations(cur_phase, servertime, db) if is_new_phase: closePhase(cur_phase['phase'], servertime, False, db) # Agreement! Create a new phase. new_phase = createPhase(video_id, new_min, new_max, servertime, cur_phase['phase_list'], db) logging.debug("Creating new phase: %s" % new_phase) if not phaseIsDivisible(new_phase): logging.debug("Picture has converged!") closePhase(new_phase['phase'], servertime, False, db) takePicture(new_phase, video_id, db) request.write(json.dumps(new_phase, cls=DecimalEncoder)) else: request.write(json.dumps(cur_phase, cls=DecimalEncoder)) db.commit() except Exception, e: db.rollback() logging.exception(e) raise