def getWFSData(): from owslib.wfs import WebFeatureService import string params = getWFSParams() # Get parameters params = checkParams(params) # Check parameters wfs = WebFeatureService(params['baseURL'].value, version=params['version'].value) response = wfs.getfeature(typename=str(params['typeName'].value), featureid=[params['featureID'].value]) # Contact server if string.find(params['baseURL'].value, 'bodc', 0): response = processBODCResponse(response.read(), params) # Get data from response else: pass current_app.logger.debug('Jsonifying response...') # DEBUG # Convert to json try: jsonData = jsonify(output = response) except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError('2-06', None, g.user.id, "views/wfs.py:getWFSData - Type error, returning 500 to user. Exception %s" % e, request) abort(500) # If we fail to jsonify the data return 500 current_app.logger.debug('Request complete, Sending results') # DEBUG return jsonData # return json
def getBboxData(params, method): import os, errno try: return getData(params, method) except urllib2.URLError as e: if hasattr(e, 'code'): # check for the code attribute from urllib2.urlopen if e.code == 400: ## THIS IS BAD BAD CODE PLEASE DO NOT KILL ME. HOWEVER, DO DELETE ASAP ## On the first attempt, it may return a 400 due to vertical attribute in data ## The second try removes the negative to attempt to fix. try: if "vertical" in params: current_app.logger.debug(params["vertical"].value) params["vertical"]._value = params["vertical"].value[1:] params['url'] = createURL(params) datavar = getData(params, method) current_app.logger.debug(datavar) return datavar except urllib2.URLError as e: if hasattr(e, 'code'): # check for the code attribute from urllib2.urlopen if e.code == 400: g.error = "Failed to access url, make sure you have entered the correct parameters." if e.code == 500: g.error = "Sorry, looks like one of the servers you requested data from is having trouble at the moment. It returned a 500." abort(400) ## / BAD CODE g.error = "Failed to access url, make sure you have entered the correct parameters." if e.code == 500: g.error = "Sorry, looks like one of the servers you requested data from is having trouble at the moment. It returned a 500." abort(400) g.error = "Failed to access url, make sure you have entered the correct parameters" error_handler.setError('2-06', None, g.user.id, "views/wcs.py:getBboxData - Failed to access url, returning 400 to user. Exception %s" % e, request)
def getWFSData(): from owslib.wfs import WebFeatureService import string params = getWFSParams() # Get parameters params = checkParams(params) # Check parameters wfs = WebFeatureService(params['baseURL'].value, version=params['version'].value) response = wfs.getfeature(typename=str(params['typeName'].value), featureid=[params['featureID'].value ]) # Contact server if string.find(params['baseURL'].value, 'bodc', 0): response = processBODCResponse(response.read(), params) # Get data from response else: pass current_app.logger.debug('Jsonifying response...') # DEBUG # Convert to json try: jsonData = jsonify(output=response) except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError( '2-06', None, g.user.id, "views/wfs.py:getWFSData - Type error, returning 500 to user. Exception %s" % e, request) abort(500) # If we fail to jsonify the data return 500 current_app.logger.debug('Request complete, Sending results') # DEBUG return jsonData # return json
def setError(): error_code = request.values.get('error_code', None) state = request.values.get('state', None) user_id = -1 if g.user is not None: user_id = g.user.id details = request.values.get('details', None) output = {} if error_code is None: output['message'] = 'failed to store errors, error code was not provided' output['status'] = '404' else: error_handler.setError(error_code, state, user_id, details, request) output['message'] = 'error stored' output['status'] = '200' try: jsonData = jsonify(output = output) return jsonData except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e abort(500) # If we fail to jsonify the data return 500
def get_state(state_url): # Decode url into a number to match to a state state_id = short_url.decode_url(state_url) output = {} if state_id is not None: state = State.query.filter(state_id == State.id).first() if state is not None: state.views += 1 state.last_used = datetime.datetime.now() db_session.commit() output = state_to_json(state) output['status'] = '200' else: output['error'] = 'Failed to find a state matching that url' output['status'] = '404' else: output['error'] = 'Invalid state url' output['status'] = '400' try: return jsonify(output=output) except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError('2-0', None, g.user.id, "views/state.py:removeStates - Type Error exception, returning 500 to user. Exception %s" % e, request) abort(500) # If we fail to jsonify the data return 500
def setError(): error_code = request.values.get('error_code', None) state = request.values.get('state', None) user_id = -1 if g.user is not None: user_id = g.user.id details = request.values.get('details', None) output = {} if error_code is None: output[ 'message'] = 'failed to store errors, error code was not provided' output['status'] = '404' else: error_handler.setError(error_code, state, user_id, details, request) output['message'] = 'error stored' output['status'] = '200' try: jsonData = jsonify(output=output) return jsonData except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e abort(500) # If we fail to jsonify the data return 500
def getError(errorID): output = {} if errorID is not None: error = Errors.query.filter(errorID == Errors.id).first() if error is not None: output = errorToJSON(error) output['status'] = '200' else: output['error'] = 'Failed to find an error matching the id' output['status'] = '404' error_handler.setError( '2-07', None, None, "views/errors.py:getError - Failed to find error matching the id, returning 404 to user.", request) try: jsonData = jsonify(output=output) return jsonData except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError( '2-05', None, g.user.id, "views/errors.py:getError - Type Error exception, returning 500 to user. Exception %s" % e, request) abort(500) # If we fail to jsonify the data return 500
def getErrors(): output = {} fromDate = request.args.get('from', None) toDate = request.args.get('to', None) if fromDate is None and toDate is None: errors = Errors.query.all() else: errors = Errors.query.filter(Errors.date_time.between( fromDate, toDate)) if errors is not None: output['errors'] = {} for error in errors: output['errors'][error.id] = errorToJSON(error) output['status'] = '200' else: output['error'] = 'Failed to find an error matching the id' output['status'] = '404' error_handler.setError( '2-07', None, None, "views/errors.py:getError - Failed to find error matching the id, returning 404 to user.", request) try: jsonData = jsonify(output=output) return jsonData except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError( '2-05', None, g.user.id, "views/errors.py:getError - Type Error exception, returning 500 to user. Exception %s" % e, request) abort(500) # If we fail to jsonify the data return 500
def getErrors(): output = {} fromDate = request.args.get('from', None) toDate = request.args.get('to', None) if fromDate is None and toDate is None: errors = Errors.query.all() else: errors = Errors.query.filter(Errors.date_time.between(fromDate, toDate)) if errors is not None: output['errors'] = {} for error in errors: output['errors'][error.id] = errorToJSON(error) output['status'] = '200' else: output['error'] = 'Failed to find an error matching the id' output['status'] = '404' error_handler.setError('2-07', None, None, "views/errors.py:getError - Failed to find error matching the id, returning 404 to user.", request) try: jsonData = jsonify(output = output) return jsonData except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError('2-05', None, g.user.id, "views/errors.py:getError - Type Error exception, returning 500 to user. Exception %s" % e, request) abort(500) # If we fail to jsonify the data return 500
def getGraphs(): state = request.values.get('state', None) # Check if the user is logged in. if g.user is None: error_handler.setError('2-01', state, g.user.id, "views/graphs.py:getGraphs - The user is not logged in, returning 401 to user.", request) abort(401) #TODO: Return available states filtered by email or other provided parameters. email = g.user.email if email is None: return 'You need to enter an email' user = User.query.filter(User.email == email).first() if user is None: return 'No user with that email.' graphs = user.graphs.order_by(Graph.id.desc()).all() output = OrderedDict() for graph in graphs: output[short_url.encode_url(graph.id)] = graphToJSON(graph) #output[graph.id] = graphToJSON(graph) try: jsonData = jsonify(output = output) #current_app.logger.debug('Request complete, Sending results') # DEBUG return jsonData except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError('2-06', state, g.user.id, "views/graph.py:getGraphs - Type Error exception, returning 400 to user. Exception %s" % e, request) abort(400) # If we fail to jsonify the data return 400
def rotate(): import urllib2 angle = request.args.get('angle') if( angle == None ): error_handler.setError('2-08', None, g.user.id, "views/proxy.py:proxy - Angle not set.", request) abort(502) url = request.args.get('url', 'http://www.openlayers.org') current_app.logger.debug("Rotating image") current_app.logger.debug(url) try: host = url.split("/")[2] current_app.logger.debug(host) # Check if the image is at an allowed server if host and allowedHosts and not host in allowedHosts: error_handler.setError('2-01', None, g.user.id, "views/proxy.py:proxy - Host is not in the whitelist, returning 502 to user.", request) abort(502) # Abort if the url doesnt start with http if not url.startswith("http://") and not url.startswith("https://"): g.error = 'Missing protocol. Add "http://" or "https://" to the front of your request url.' error_handler.setError('2-06', None, g.user.id, "views/proxy.py:proxy - The protocol is missing, returning 400 to user.", request) abort(400) # Setup the urllib request for POST or GET if request.method == "POST": contentType = request.environ["CONTENT_TYPE"] headers = {"Content-Type": request.environ["CONTENT_TYPE"]} body = request r = urllib2.Request(url, body, headers) y = urllib2.urlopen(r) else: y = urllib2.urlopen(url) except urllib2.URLError as e: if hasattr(e, 'code'): # check for the code attribute from urllib2.urlopen g.error = "Failed to access url, server replied with " + str(e.code) + "." abort(400) g.error = "Failed to access url, make sure you have entered the correct parameters" error_handler.setError('2-06', None, g.user.id, "views/proxy.py:proxy - URL error, returning 400 to user. Exception %s" % e, request) abort(400) # return 400 if we can't get an exact code except Exception, e: if hasattr(e, 'code'): # check for the code attribute from urllib2.urlopen g.error = "Failed to access url, server replied with " + str(e.code) + "." abort(e.code) g.error = "Failed to access url, make sure you have entered the correct parameters" error_handler.setError('2-06', None, g.user.id, "views/proxy.py:proxy - URL error, returning 400 to user. Exception %s" % e, request) abort(400) # return 400 if we can't get an exact code
def createURL(params): urlParams = {} for param in params.itervalues(): if param.neededInUrl(): urlParams[param.getName()] = param.value query = urllib.urlencode(urlParams) url = params['baseURL'].value + query current_app.logger.debug('URL: ' + url) # DEBUG if "wcs2json/wcs" in params['baseURL'].value: g.error = 'possible infinite recursion detected, cancelled request' error_handler.setError('2-06', None, g.user.id, "views/wcs.py:createURL - Possible recursion detected, returning 400 to user.", request) abort(400) return Param("url", False, False, url)
def getStates(): # Check if the user is logged in. if g.user is None: error_handler.setError('2-04', None, None, "views/state.py:getStates - Failed to store state data because the user is not logged in, returning 401 to user.", request) abort(401) #TODO: Return available states filtered by email or other provided parameters. email = g.user.email output = {} if email is None: output['message'] = 'You need to enter an email' output['status'] = '400' error_handler.setError('2-04', None, g.user.id, "views/state.py:getStates - Email address is missing, returning 400 to user.", request) else: user = User.query.filter(User.email == email).first() if user is None: output['message'] = 'No user with that email.' output['status'] = '400' error_handler.setError('2-06', None, g.user.id, "views/state.py:getStates - There is no user with the email address provided, returning 400 to user.", request) else: states = user.states.all() for state in states: output[short_url.encode_url(state.id)] = stateToJSON(state) try: jsonData = jsonify(output = output) #current_app.logger.debug('Request complete, Sending results') # DEBUG return jsonData except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError('2-05', None, g.user.id, "views/state.py:getStates - Type Error exception, returning 500 to user. Exception %s" % e, request) abort(500) # If we fail to jsonify the data return 500
def getDataSafe(params, method): try: return getData(params, method) except urllib2.URLError as e: print(e) if hasattr(e, 'code'): # check for the code attribute from urllib2.urlopen if e.code == 400: g.error = "Failed to access url, make sure you have entered the correct parameters." if e.code == 500: g.error = "Sorry, looks like one of the servers you requested data from is having trouble at the moment. It returned a 500." abort(400) g.error = "Failed to access url, make sure you have entered the correct parameters" error_handler.setError('2-06', None, g.user.id, "views/wcs.py:getDataSafe - Failed to access url, returning 400 to user. Exception %s" % e, request) abort(400) # return 400 if we can't get an exact code
def checkParams(params): checkedParams = {} for key in params.iterkeys(): if params[key].value == None or len(params[key].value) == 0: if not params[key].isOptional(): g.error = 'required parameter "%s" is missing or is set to an invalid value' % key user = None if g.user: user = g.user.id error_handler.setError('2-06', None, user, "views/wcs.py:checkParams - Parameter is missing or invalid, returning 400 to user. Parameter %s" % key, request) abort(400) else: checkedParams[key] = params[key] return checkedParams
def checkParams(params): checkedParams = {} for key in params.iterkeys(): if params[key].value is None or len(params[key].value) == 0: if not params[key].isOptional(): error = 'required parameter "%s" is missing or is set to an invalid value' % key g.error = error print(error) user_id = g.user.id if g.user is not None else '' error_handler.setError('2-06', None, user_id, "views/wcs.py:checkParams - Parameter is missing or invalid, returning 400 to user. Parameter %s" % key, request) abort(400) else: checkedParams[key] = params[key] return checkedParams
def removeState(stateUrl): # Check if the user is logged in. if g.user is None: error_handler.setError('2-04', None, None, "views/state.py:removeState - Failed to remove state data because the user is not logged in, returning 401 to user.", request) abort(401) email = g.user.email # Decode url into a number to match to a state stateID = short_url.decode_url(stateUrl) output = {} if email is None or stateID is None: output['status'] = '404' output['message'] = 'Failed to remove state' output['email'] = email output['stateID'] = stateID error_handler.setError('2-04', None, g.user.id, "views/state.py:removeState - Failed to remove state data, not enough data provided, returning 404 to user.", request) else: # Might be able to use 'g.user' instead. Only reason I havn't is I'm not # sure on the reliability of it. user = User.query.filter(User.email == email).first() if user is None: # Create new user user = User(email) db_session.add(user) db_session.commit() state = user.states.filter(State.id == stateID).first() if state != None: db_session.delete(state) db_session.commit() output['message'] = 'Successfully removed state.' output['status'] = '200' else: output['message'] = 'Failed to remove state as no state with that ID could be found.' output['status'] = '404' error_handler.setError('2-04', None, None, "views/state.py:removeStates - Failed to remove state because the state id could not be found, returning 404 to user.", request) try: jsonData = jsonify(output = output) #current_app.logger.debug('Request complete, Sending results') # DEBUG return jsonData except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError('2-05', None, g.user.id, "views/state.py:removeStates - Type Error exception, returning 500 to user. Exception %s" % e, request) abort(500) # If we fail to jsonify the data return 500
def getError(errorID): output = {} if errorID is not None: error = Errors.query.filter(errorID == Errors.id).first() if error is not None: output = errorToJSON(error) output['status'] = '200' else: output['error'] = 'Failed to find an error matching the id' output['status'] = '404' error_handler.setError('2-07', None, None, "views/errors.py:getError - Failed to find error matching the id, returning 404 to user.", request) try: jsonData = jsonify(output = output) return jsonData except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError('2-05', None, g.user.id, "views/errors.py:getError - Type Error exception, returning 500 to user. Exception %s" % e, request) abort(500) # If we fail to jsonify the data return 500
def getGraph(graphUrl): state = request.values.get('state', None) # Decode url into a number to match to a state graphID = short_url.decode_url(graphUrl) print graphID output = {} if graphID is not None: graph = Graph.query.filter(graphID == Graph.id).first() if graph != None: print graph graph.retrievals += 1 graph.last_used = datetime.datetime.now() db_session.commit() output = graphToJSON(graph) output['status'] = '200' else: output['error'] = "Failed to find a graph matching that id" output['status'] = '404' error_handler.setError('2-07', state, g.user.id, "views/graph.py:getGraph - There was no graph found matching the id given, returning 404 to user.", request) else: output['error'] = "You must enter a valid graph id" output['status'] = '400' error_handler.setError('2-06', state, g.user.id, "views/graph.py:getGraph - The graph id is invalid, returning 400 to user.", request) try: jsonData = jsonify(output = output) #current_app.logger.debug('Request complete, Sending results') # DEBUG return jsonData except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError('2-06', state, g.user.id, "views/graph.py:getGraph - Type Error exception, returning 400 to user. Exception %s" % e, request) abort(400) # If we fail to jsonify the data return 500
def getState(stateUrl): # Decode url into a number to match to a state stateID = short_url.decode_url(stateUrl) output = {} if stateID is not None: state = State.query.filter(stateID == State.id).first() if state is not None: state.views += 1 state.last_used = datetime.datetime.now() db_session.commit() output = stateToJSON(state) output['status'] = '200' else: output['error'] = 'Failed to find a state matching that url' output['status'] = '404' error_handler.setError('2-07', None, None, "views/state.py:getState - Failed to find state matching the url, returning 404 to user.", request) else: output['error'] = 'You must enter a valid state url' output['status'] = '400' error_handler.setError('2-04', None, None, "views/state.py:getStates - Failed to find state, no state url was provided, returning 400 to user.", request) try: jsonData = jsonify(output = output) #current_app.logger.debug('Request complete, Sending results') # DEBUG return jsonData except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError('2-0', None, g.user.id, "views/state.py:getState - Type Error exception, returning 500 to user. Exception %s" % e, request) abort(500) # If we fail to jsonify the data return 500
def getGraphs(): state = request.values.get('state', None) # Check if the user is logged in. if g.user is None: error_handler.setError( '2-01', state, g.user.id, "views/graphs.py:getGraphs - The user is not logged in, returning 401 to user.", request) abort(401) #TODO: Return available states filtered by email or other provided parameters. email = g.user.email if email is None: return 'You need to enter an email' user = User.query.filter(User.email == email).first() if user is None: return 'No user with that email.' graphs = user.graphs.order_by(Graph.id.desc()).all() output = OrderedDict() for graph in graphs: output[short_url.encode_url(graph.id)] = graphToJSON(graph) #output[graph.id] = graphToJSON(graph) try: jsonData = jsonify(output=output) #current_app.logger.debug('Request complete, Sending results') # DEBUG return jsonData except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError( '2-06', state, g.user.id, "views/graph.py:getGraphs - Type Error exception, returning 400 to user. Exception %s" % e, request) abort(400) # If we fail to jsonify the data return 400
def getWcsData(): graph_to_method = { 'histogram': get_histogram, 'timeseries': get_timeseries, 'hovmollerLon': get_hovmoller, 'hovmollerLat': get_hovmoller } wkt = request.headers['wkt'] g.graphError = "" params = getParams(wkt) # Gets any parameters params = checkParams(params) # Checks what parameters where entered params['url'] = createURL(params) current_app.logger.debug('Processing request...') # DEBUG current_app.logger.debug(params['url'].value) graph_type = params['graphType'].value method = graph_to_method[graph_type] output = getDataSafe(params, method) current_app.logger.debug('Jsonifying response...') # DEBUG try: jsonData = jsonify(output=output, type=params['graphType'].value, coverage=params['coverage'].value, error=g.graphError) except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError('2-06', None, g.user.id, "views/wcs.py:getWcsData - Type error, returning 400 to user. Exception %s" % e, request) current_app.logger.debug('Error: ' + str(e)) abort(400) # If we fail to jsonify the data return 400 current_app.logger.debug('Request complete, Sending results') return jsonData
def getStates(): # Check if the user is logged in. if g.user is None: error_handler.setError( '2-04', None, None, "views/state.py:getStates - Failed to store state data because the user is not logged in, returning 401 to user.", request) abort(401) #TODO: Return available states filtered by email or other provided parameters. email = g.user.email output = {} if email is None: output['message'] = 'You need to enter an email' output['status'] = '400' error_handler.setError( '2-04', None, g.user.id, "views/state.py:getStates - Email address is missing, returning 400 to user.", request) else: user = User.query.filter(User.email == email).first() if user is None: output['message'] = 'No user with that email.' output['status'] = '400' error_handler.setError( '2-06', None, g.user.id, "views/state.py:getStates - There is no user with the email address provided, returning 400 to user.", request) else: states = user.states.all() for state in states: output[short_url.encode_url(state.id)] = stateToJSON(state) try: jsonData = jsonify(output=output) #current_app.logger.debug('Request complete, Sending results') # DEBUG return jsonData except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError( '2-05', None, g.user.id, "views/state.py:getStates - Type Error exception, returning 500 to user. Exception %s" % e, request) abort(500) # If we fail to jsonify the data return 500
def setState(): # Check if the user is logged in. email = None if g.user is not None: email = g.user.email state = request.values.get('state', None) output = {} if state is None: output['message'] = 'failed to store state' output['email'] = email output['state'] = state output['status'] = '404' error_handler.setError('2-04', state, g.user.id, "views/state.py:setState - Failed to store state data, returning 404 to user.", request) else: # Might be able to use 'g.user' instead. Only reason I havn't is I'm not # sure on the reliability of it. user = User.query.filter(User.email == email).first() if user is None and email is not None: # Create new user user = User(email) db_session.add(user) db_session.commit() if user is None: user_id = -1 else: user_id = user.id s = State(user_id, state) s.session_id = request.values.get('session_id', None); print s.session_id checksumMatch = State.query.filter(State.checksum == s.checksum).first() if checksumMatch == None: db_session.add(s) db_session.commit() output['url'] = short_url.encode_url(s.id) output['message'] = 'state stored' output['status'] = '200' else: output['url'] = short_url.encode_url(checksumMatch.id) output['message'] = 'Failed to add state as state already exists' output['status'] = '400' error_handler.setError('2-05', state, user_id, "views/state.py:setState - The state already exists in the database, returning 400 to the user.", request) try: jsonData = jsonify(output = output) #current_app.logger.debug('Request complete, Sending results') # DEBUG return jsonData except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError('2-06', state, user_id, "views/state.py:setState - Type Error exception, returning 500 to user. Exception %s" % e, request) abort(500) # If we fail to jsonify the data return 500
def setGraph(): state = request.values.get('state', None) # Check if the user is logged in. if g.user is None: error_handler.setError( '2-01', state, None, "views/graphs.py:setGraph - The user is not logged in, returning 401 to user.", request) abort(401) email = g.user.email graphJSON = request.values.get('graph', None) output = {} if email is None or graphJSON is None: output['message'] = 'failed to store graph' output['email'] = email output['graph'] = graphJSON output['status'] = '404' error_handler.setError( '2-04', state, g.user.id, "views/graphs.py:setGraph - Failed to store the graph, email or graphJSON were empty, returning 404 to user.", request) else: user = User.query.filter(User.email == email).first() if user is None: # Create new user user = User(email) db_session.add(user) db_session.commit() graph = Graph(user.id, graphJSON) db_session.add(graph) db_session.commit() output['url'] = short_url.encode_url(graph.id) output['status'] = 'graph stored' try: jsonData = jsonify(output=output) #current_app.logger.debug('Request complete, Sending results') # DEBUG return jsonData except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError( '2-06', state, g.user.id, "views/graph.py:setGraph - Type Error exception, returning 400 to user. Exception %s" % e, request) abort(400) # If we fail to jsonify the data return 500
def getGraph(graphUrl): state = request.values.get('state', None) # Decode url into a number to match to a state graphID = short_url.decode_url(graphUrl) print graphID output = {} if graphID is not None: graph = Graph.query.filter(graphID == Graph.id).first() if graph != None: print graph graph.retrievals += 1 graph.last_used = datetime.datetime.now() db_session.commit() output = graphToJSON(graph) output['status'] = '200' else: output['error'] = "Failed to find a graph matching that id" output['status'] = '404' error_handler.setError( '2-07', state, g.user.id, "views/graph.py:getGraph - There was no graph found matching the id given, returning 404 to user.", request) else: output['error'] = "You must enter a valid graph id" output['status'] = '400' error_handler.setError( '2-06', state, g.user.id, "views/graph.py:getGraph - The graph id is invalid, returning 400 to user.", request) try: jsonData = jsonify(output=output) #current_app.logger.debug('Request complete, Sending results') # DEBUG return jsonData except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError( '2-06', state, g.user.id, "views/graph.py:getGraph - Type Error exception, returning 400 to user. Exception %s" % e, request) abort(400) # If we fail to jsonify the data return 500
def setGraph(): state = request.values.get('state', None) # Check if the user is logged in. if g.user is None: error_handler.setError('2-01', state, None, "views/graphs.py:setGraph - The user is not logged in, returning 401 to user.", request) abort(401) email = g.user.email graphJSON = request.values.get('graph', None) output = {} if email is None or graphJSON is None: output['message'] = 'failed to store graph' output['email'] = email output['graph'] = graphJSON output['status'] = '404' error_handler.setError('2-04', state, g.user.id, "views/graphs.py:setGraph - Failed to store the graph, email or graphJSON were empty, returning 404 to user.", request) else: user = User.query.filter(User.email == email).first() if user is None: # Create new user user = User(email) db_session.add(user) db_session.commit() graph = Graph(user.id, graphJSON) db_session.add(graph) db_session.commit() output['url'] = short_url.encode_url(graph.id) output['status'] = 'graph stored' try: jsonData = jsonify(output = output) #current_app.logger.debug('Request complete, Sending results') # DEBUG return jsonData except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError('2-06', state, g.user.id, "views/graph.py:setGraph - Type Error exception, returning 400 to user. Exception %s" % e, request) abort(400) # If we fail to jsonify the data return 500
def getState(stateUrl): # Decode url into a number to match to a state stateID = short_url.decode_url(stateUrl) output = {} if stateID is not None: state = State.query.filter(stateID == State.id).first() if state is not None: state.views += 1 state.last_used = datetime.datetime.now() db_session.commit() output = stateToJSON(state) output['status'] = '200' else: output['error'] = 'Failed to find a state matching that url' output['status'] = '404' error_handler.setError( '2-07', None, None, "views/state.py:getState - Failed to find state matching the url, returning 404 to user.", request) else: output['error'] = 'You must enter a valid state url' output['status'] = '400' error_handler.setError( '2-04', None, None, "views/state.py:getStates - Failed to find state, no state url was provided, returning 400 to user.", request) try: jsonData = jsonify(output=output) #current_app.logger.debug('Request complete, Sending results') # DEBUG return jsonData except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError( '2-0', None, g.user.id, "views/state.py:getState - Type Error exception, returning 500 to user. Exception %s" % e, request) abort(500) # If we fail to jsonify the data return 500
def proxy(): import urllib2 url = request.args.get('url', 'http://www.openlayers.org') current_app.logger.debug("Checking logger") current_app.logger.debug(url) try: host = url.split("/")[2] current_app.logger.debug(host) if host and allowedHosts and not host in allowedHosts: error_handler.setError( '2-01', None, g.user.id, "views/proxy.py:proxy - Host is not in the whitelist, returning 502 to user.", request) abort(502) if url.startswith("http://") or url.startswith("https://"): if request.method == "POST": contentType = request.environ["CONTENT_TYPE"] headers = {"Content-Type": request.environ["CONTENT_TYPE"]} body = request r = urllib2.Request(url, body, headers) y = urllib2.urlopen(r) else: y = urllib2.urlopen(url) # print content type header i = y.info() #if i.has_key("Content-Type"): # print "Content-Type: %s" % (i["Content-Type"]) #else: # print "Content-Type: text/plain" #print #resp = y.read() resp = make_response(y.read(), y.code) if i.has_key("Content-Type"): resp.headers.add('Content-Type', i['Content-Type']) #for key in y.headers.dict.iterkeys(): #resp.headers[key] = y.headers.dict[key] y.close() return resp else: g.error = 'Missing protocol. Add "http://" or "https://" to the front of your request url.' error_handler.setError( '2-06', None, g.user.id, "views/proxy.py:proxy - The protocol is missing, returning 400 to user.", request) abort(400) except urllib2.URLError as e: if hasattr( e, 'code'): # check for the code attribute from urllib2.urlopen if e.code == 400: g.error = "Failed to access url, make sure you have entered the correct parameters." if e.code == 500: g.error = "Sorry, looks like one of the servers you requested data from is having trouble at the moment. It returned a 500." abort(400) g.error = "Failed to access url, make sure you have entered the correct parameters" error_handler.setError( '2-06', None, g.user.id, "views/proxy.py:proxy - URL error, returning 400 to user. Exception %s" % e, request) abort(400) # return 400 if we can't get an exact code except Exception, e: if hasattr( e, 'code'): # check for the code attribute from urllib2.urlopen if e.code == 400: g.error = "Failed to access url" abort(e.code) g.error = "Failed to access url, make sure you have entered the correct parameters" error_handler.setError( '2-06', None, g.user.id, "views/proxy.py:proxy - URL error, returning 400 to user. Exception %s" % e, request) abort(400) # return 400 if we can't get an exact code
def removeState(stateUrl): # Check if the user is logged in. if g.user is None: error_handler.setError( '2-04', None, None, "views/state.py:removeState - Failed to remove state data because the user is not logged in, returning 401 to user.", request) abort(401) email = g.user.email # Decode url into a number to match to a state stateID = short_url.decode_url(stateUrl) output = {} if email is None or stateID is None: output['status'] = '404' output['message'] = 'Failed to remove state' output['email'] = email output['stateID'] = stateID error_handler.setError( '2-04', None, g.user.id, "views/state.py:removeState - Failed to remove state data, not enough data provided, returning 404 to user.", request) else: # Might be able to use 'g.user' instead. Only reason I havn't is I'm not # sure on the reliability of it. user = User.query.filter(User.email == email).first() if user is None: # Create new user user = User(email) db_session.add(user) db_session.commit() state = user.states.filter(State.id == stateID).first() if state != None: db_session.delete(state) db_session.commit() output['message'] = 'Successfully removed state.' output['status'] = '200' else: output[ 'message'] = 'Failed to remove state as no state with that ID could be found.' output['status'] = '404' error_handler.setError( '2-04', None, None, "views/state.py:removeStates - Failed to remove state because the state id could not be found, returning 404 to user.", request) try: jsonData = jsonify(output=output) #current_app.logger.debug('Request complete, Sending results') # DEBUG return jsonData except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError( '2-05', None, g.user.id, "views/state.py:removeStates - Type Error exception, returning 500 to user. Exception %s" % e, request) abort(500) # If we fail to jsonify the data return 500
def setState(): # Check if the user is logged in. email = None if g.user is not None: email = g.user.email state = request.values.get('state', None) output = {} if state is None: output['message'] = 'failed to store state' output['email'] = email output['state'] = state output['status'] = '404' error_handler.setError( '2-04', state, g.user.id, "views/state.py:setState - Failed to store state data, returning 404 to user.", request) else: # Might be able to use 'g.user' instead. Only reason I havn't is I'm not # sure on the reliability of it. user = User.query.filter(User.email == email).first() if user is None and email is not None: # Create new user user = User(email) db_session.add(user) db_session.commit() if user is None: user_id = -1 else: user_id = user.id s = State(user_id, state) checksumMatch = State.query.filter( State.checksum == s.checksum).first() if checksumMatch == None: db_session.add(s) db_session.commit() output['url'] = short_url.encode_url(s.id) output['message'] = 'state stored' output['status'] = '200' else: output['url'] = short_url.encode_url(checksumMatch.id) output['message'] = 'Failed to add state as state already exists' output['status'] = '400' error_handler.setError( '2-05', state, user_id, "views/state.py:setState - The state already exists in the database, returning 400 to the user.", request) try: jsonData = jsonify(output=output) #current_app.logger.debug('Request complete, Sending results') # DEBUG return jsonData except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e error_handler.setError( '2-06', state, user_id, "views/state.py:setState - Type Error exception, returning 500 to user. Exception %s" % e, request) abort(500) # If we fail to jsonify the data return 500
def proxy(): import urllib2 url = request.args.get('url', 'http://www.openlayers.org') current_app.logger.debug("Checking logger") current_app.logger.debug(url) try: host = url.split("/")[2] current_app.logger.debug(host) if host and allowedHosts and not host in allowedHosts: error_handler.setError('2-01', None, g.user.id, "views/proxy.py:proxy - Host is not in the whitelist, returning 502 to user.", request) abort(502) if url.startswith("http://") or url.startswith("https://"): if request.method == "POST": contentType = request.environ["CONTENT_TYPE"] headers = {"Content-Type": request.environ["CONTENT_TYPE"]} body = request r = urllib2.Request(url, body, headers) y = urllib2.urlopen(r) else: y = urllib2.urlopen(url) # print content type header i = y.info() #if i.has_key("Content-Type"): # print "Content-Type: %s" % (i["Content-Type"]) #else: # print "Content-Type: text/plain" #print #resp = y.read() resp = make_response(y.read(), y.code) if i.has_key("Content-Type"): resp.headers.add('Content-Type', i['Content-Type']) #for key in y.headers.dict.iterkeys(): #resp.headers[key] = y.headers.dict[key] y.close() return resp else: g.error = 'Missing protocol. Add "http://" or "https://" to the front of your request url.' error_handler.setError('2-06', None, g.user.id, "views/proxy.py:proxy - The protocol is missing, returning 400 to user.", request) abort(400) except urllib2.URLError as e: if hasattr(e, 'code'): # check for the code attribute from urllib2.urlopen if e.code == 400: g.error = "Failed to access url, make sure you have entered the correct parameters." if e.code == 500: g.error = "Sorry, looks like one of the servers you requested data from is having trouble at the moment. It returned a 500." abort(400) g.error = "Failed to access url, make sure you have entered the correct parameters" error_handler.setError('2-06', None, g.user.id, "views/proxy.py:proxy - URL error, returning 400 to user. Exception %s" % e, request) abort(400) # return 400 if we can't get an exact code except Exception, e: if hasattr(e, 'code'): # check for the code attribute from urllib2.urlopen if e.code == 400: g.error = "Failed to access url" abort(e.code) g.error = "Failed to access url, make sure you have entered the correct parameters" error_handler.setError('2-06', None, g.user.id, "views/proxy.py:proxy - URL error, returning 400 to user. Exception %s" % e, request) abort(400) # return 400 if we can't get an exact code
def getWcsData(): import random g.graphError = ""; params = getParams() # Gets any parameters params = checkParams(params) # Checks what parameters where entered params['url'] = createURL(params) current_app.logger.debug('Processing request...') # DEBUG current_app.logger.debug(params['url'].value) type = params['type'].value if type == 'histogram': # Outputs data needed to create a histogram output = getBboxData(params, histogram) elif type == 'timeseries': # Outputs a set of standard statistics if params.get('output_format') is not None: if params['output_format']._value == 'csv': if 'polygon' in params : data = getIrregularData(params) elif 'line' in params: data = getIrregularData(params, poly_type='line') else: data = getBboxData(params, basic) output = toCSV(data) else: if 'polygon' in params : output = getIrregularData(params) elif 'line' in params: output = getIrregularData(params, poly_type='line') else: output = getBboxData(params, basic) elif type == 'scatter': # Outputs a scatter graph output = getBboxData(params, scatter) elif type == 'hovmollerLon' or 'hovmollerLat': # outputs a hovmoller graph output = getBboxData(params, hovmoller) elif type == 'point': output = getPointData(params, raw) elif type == 'raw': # Outputs the raw values output = getBboxData(params, raw) elif type == 'test': # Used to test new code output = getBboxData(params, test) elif type == 'error': # Used to test error handling client-side choice = random.randrange(1,7) if choice == 1: g.error = "test 404" abort(400) elif choice == 2: abort(401) elif choice == 3: abort(404) elif choice == 4: return jsonify(output = "edfwefwrfewf") elif choice == 5: abort(502) elif choice == 6: x = y # Should create a 500 from apache else: g.error = '"%s" is not a valid option' % type return abort(400) current_app.logger.debug('Jsonifying response...') # DEBUG try: if params.get('output_format') is not None: if params['output_format']._value == 'csv': current_app.logger.debug(output) outputData = Response(output, mimetype='text/csv') else: outputData = jsonify(output = output, type = params['type'].value, coverage = params['coverage'].value, error = g.graphError) except TypeError as e: g.error = "Request aborted, exception encountered: %s" % e user = None if g.user: user = g.user.id error_handler.setError('2-06', None, user, "views/wcs.py:getWcsData - Type error, returning 400 to user. Exception %s" % e, request) abort(400) # If we fail to jsonify the data return 400 current_app.logger.debug('Request complete, Sending results') # DEBUG return outputData
def rotate(): import urllib2 angle = request.args.get('angle') if (angle == None): error_handler.setError('2-08', None, g.user.id, "views/proxy.py:proxy - Angle not set.", request) abort(502) url = request.args.get('url', 'http://www.openlayers.org') current_app.logger.debug("Rotating image") current_app.logger.debug(url) try: host = url.split("/")[2] current_app.logger.debug(host) # Check if the image is at an allowed server if host and allowedHosts and not host in allowedHosts: error_handler.setError( '2-01', None, g.user.id, "views/proxy.py:proxy - Host is not in the whitelist, returning 502 to user.", request) abort(502) # Abort if the url doesnt start with http if not url.startswith("http://") and not url.startswith("https://"): g.error = 'Missing protocol. Add "http://" or "https://" to the front of your request url.' error_handler.setError( '2-06', None, g.user.id, "views/proxy.py:proxy - The protocol is missing, returning 400 to user.", request) abort(400) # Setup the urllib request for POST or GET if request.method == "POST": contentType = request.environ["CONTENT_TYPE"] headers = {"Content-Type": request.environ["CONTENT_TYPE"]} body = request r = urllib2.Request(url, body, headers) y = urllib2.urlopen(r) else: y = urllib2.urlopen(url) except urllib2.URLError as e: if hasattr( e, 'code'): # check for the code attribute from urllib2.urlopen g.error = "Failed to access url, server replied with " + str( e.code) + "." abort(400) g.error = "Failed to access url, make sure you have entered the correct parameters" error_handler.setError( '2-06', None, g.user.id, "views/proxy.py:proxy - URL error, returning 400 to user. Exception %s" % e, request) abort(400) # return 400 if we can't get an exact code except Exception, e: if hasattr( e, 'code'): # check for the code attribute from urllib2.urlopen g.error = "Failed to access url, server replied with " + str( e.code) + "." abort(e.code) g.error = "Failed to access url, make sure you have entered the correct parameters" error_handler.setError( '2-06', None, g.user.id, "views/proxy.py:proxy - URL error, returning 400 to user. Exception %s" % e, request) abort(400) # return 400 if we can't get an exact code
def hovmoller(dataset, params): xAxisVar = params['graphXAxis'].value yAxisVar = params['graphYAxis'].value zAxisVar = params['graphZAxis'].value xVar = getCoordinateVariable(dataset, xAxisVar) xArr = np.array(xVar) yVar = getCoordinateVariable(dataset, yAxisVar) yArr = np.array(yVar) zArr = np.array(dataset.variables[zAxisVar]) if xVar == None: g.graphError = "could not find %s dimension" % xAxisVar return if yVar == None: g.graphError = "could not find %s dimension" % yAxisVar return # Create a masked array ignoring nan's zMaskedArray = np.ma.masked_invalid(zArr) time = None lat = None lon = None if xAxisVar == 'Time': times = xArr time = xVar lat = yArr else: lon = xArr times = yArr time = yVar output = {} timeUnits = getUnits(time) start = None if timeUnits: start = (netCDF.num2date(times[0], time.units, calendar='standard')).isoformat() else: start = ''.join(times[0]) output['global'] = {'time': start} output['data'] = [] numDimensions = len(zMaskedArray.shape) direction = None if lat != None: direction = 'lat' elif lon != None: direction = 'lon' if numDimensions == 4: zMaskedArray = zMaskedArray.swapaxes(2,3) else: zMaskedArray = zMaskedArray.swapaxes(1,2) # Make it use Lon instead of Lat # If 4 dimensions, assume depth and switch with time if numDimensions == 4: depth = np.array(getDepth(dataset)) if len(depth.shape) > 1: current_app.logger.debug('WARNING: There are multiple depths.') else: # Presume 1 depth, set to contents of depth # This way, it will enumerate over correct array # whether depth or not zMaskedArray = zMaskedArray.swapaxes(0,1)[0] output['depth'] = float(depth[0]) for i, timelatlon in enumerate(zMaskedArray): date = None if timeUnits: date = netCDF.num2date(time[i], time.units, calendar='standard').isoformat() else: date = ''.join(times[i]) for j, row in enumerate(timelatlon): if direction == "lat": pos = lat[j] elif direction == "lon": pos = lon[j] mean = getMean(row) if not np.isnan(mean): output['data'].append([date, float(pos), mean]) if len(output['data']) < 1: g.graphError = "no valid data available to use" error_handler.setError('2-07', None, g.user.id, "views/wcs.py:hovmoller - No valid data available to use.") return output return output