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 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 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 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 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 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