Example #1
0
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
Example #2
0
def persist_state():
    # Check if the user is logged in.
    if g.user is None:
        abort(401)

    state = request.values.get('state', None)
    output = {}

    if state is None:
        output['state'] = state
        output['status'] = '404'
        abort(404)

    db_state = State.query.filter(g.user.id == State.user_id).first()
    if db_state is not None:
       db_state.views += 1
       db_state.last_used = datetime.datetime.now()
       db_state.state = state
    else:
        db_state = State(user_id=g.user.id, state=state)
        db_session.add(db_state)
    db_session.commit()

    output['url'] = short_url.encode_url(db_state.id)
    return jsonify(output=output)
Example #3
0
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
Example #4
0
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
Example #5
0
def stateToJSON(state):
    output = {}
    output["url"] = short_url.encode_url(state.id)
    output["user_id"] = state.user_id
    output["state"] = state.state
    output["version"] = state.version
    output["views"] = state.views
    output["lastUsed"] = str(state.last_used)
    return output
Example #6
0
def stateToJSON(state):
    output = {}
    output['url'] = short_url.encode_url(state.id)
    output['user_id'] = state.user_id
    output['state'] = state.state
    output['version'] = state.version
    output['views'] = state.views
    output['lastUsed'] = str(state.last_used)
    return output
Example #7
0
def stateToJSON(state):
   output = {}
   output['url'] = short_url.encode_url(state.id)
   output['user_id'] = state.user_id
   output['state'] = state.state
   output['version'] = state.version
   output['views'] = state.views
   output['lastUsed'] = str(state.last_used)
   return output
Example #8
0
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
Example #9
0
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
Example #10
0
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
Example #11
0
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
Example #12
0
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
Example #13
0
def state_to_json(state):
   output = {}
   output['url'] = short_url.encode_url(state.id)
   output['user_id'] = state.user_id
   output['state'] = state.state
   output['version'] = state.version
   output['views'] = state.views
   output['lastUsed'] = str(state.last_used)
   return output



   # todo -- check if this can be deleted
   #@portal_state.route('/state', methods=['GET'])
   #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)] = state_to_json(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



#@portal_state.route('/state/<stateUrl>', methods = ['DELETE'])
#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