Example #1
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 #2
0
def create_user():
    print('in create user')
    if g.user is not None or 'openid' not in session:
        return redirect(url_for('portal_user.index'))
    if request.method == 'POST':
        print('in post')
        email = request.form['email']
        if '@' not in email:
            flash(u'Error: you have to enter a valid email address')
        else:
            flash(u'Profile successfully created')
            db_session.add(User(email, session['openid']))
            db_session.commit()
            return redirect(oid.get_next_url())
    elif request.method == 'GET':
        print('in get')
        email = request.args.get('email', None)
        if '@' not in email:
            flash(u'Error: you have to enter a valid email address')
        else:
            flash(u'Profile successfully created')
            db_session.add(User(email, session['openid']))
            db_session.commit()
            # The index has a script that closes popup when logged in
            #return redirect(oid.get_next_url())
            return redirect(url_for('portal_user.index'))
    print('returning')
    return redirect(url_for('portal_user.index'))
Example #3
0
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
Example #4
0
def create_user():
   print('in create user')
   if g.user is not None or 'openid' not in session:
      return redirect(url_for('portal_user.index'))
   if request.method == 'POST':
      print ('in post')
      email = request.form['email']
      if '@' not in email:
         flash(u'Error: you have to enter a valid email address')
      else:
         flash(u'Profile successfully created')
         db_session.add(User(email, session['openid']))
         db_session.commit()
         return redirect(oid.get_next_url())
   elif request.method == 'GET':
      print('in get')
      email = request.args.get('email', None)
      if '@' not in email:
         flash(u'Error: you have to enter a valid email address')
      else:
         flash(u'Profile successfully created')
         db_session.add(User(email, session['openid']))
         db_session.commit()
         # The index has a script that closes popup when logged in
         #return redirect(oid.get_next_url()) 
         return redirect(url_for('portal_user.index'))
   print('returning')
   return redirect(url_for('portal_user.index'))
Example #5
0
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
Example #6
0
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
Example #7
0
def persist(shapefile_name, path):
    shape_names = shapefile_support.get_shape_names(path)
    persistent_shapes = []
    for (record_number, shape_name) in shape_names:
        shape = Shape(record_number, shape_name)
        persistent_shapes.append(shape)
        db_session.add(shape)
    db_session.add(Shapefile(name=shapefile_name, path=path, children=persistent_shapes))
    db_session.commit()
Example #8
0
def add_user_to_db(email, username, full_name, group_names):
    db_groups = UserGroup.query.all()
    for group_name in group_names:
        group_is_already_in_db = group_name in [db_group.group_name for db_group in db_groups]
        if not group_is_already_in_db:
            db_session.add(UserGroup(group_name))
    db_session.commit()
    user_groups = UserGroup.query.filter(UserGroup.group_name.in_(group_names)).all()
    db_session.add(User(email=email, openid=session['openid'], username=username, full_name=full_name, groups=user_groups))
Example #9
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 #10
0
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
Example #11
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 #12
0
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
Example #13
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 #14
0
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
Example #15
0
def setError(error_code, state, user_id, details, request):

    # These warning codes are errors that may not need to
    # be added the the database, depending on the error
    # logging level
    warning_codes = ['1-03', '1-04', '2-03', '2-05']

    if settings.ERROR_LEVEL is not "W" and error_code in warning_codes:
        return

    state_id = None
    if state is not None:
        s = State(-1, state)
        db_session.add(s)
        db_session.commit()
        state_id = s.id

    ip_address = request.remote_addr

    if error_code is not None:
        e = Errors(error_code, state_id, user_id, ip_address, details)
        db_session.add(e)
        db_session.commit()
Example #16
0
def setError(error_code, state, user_id, details, request):

   # These warning codes are errors that may not need to 
   # be added the the database, depending on the error
   # logging level
   warning_codes = ['1-03', '1-04', '2-03', '2-05']

   if settings.ERROR_LEVEL is not "W" and error_code in warning_codes:
      return
      
   state_id = None
   if state is not None:
      s = State(-1, state)
      db_session.add(s)
      db_session.commit()
      state_id = s.id

   ip_address = request.remote_addr

   if error_code is not None:
      e = Errors(error_code, state_id, user_id, ip_address, details)
      db_session.add(e)
      db_session.commit()
Example #17
0
def create_user():
   if g.user is not None or 'openid' not in session:
      raise ValueError('should never come here')
   if request.method == 'POST':
      print ('in post')
      email = request.form['email']
      group_names = request.form['groups'].split()
      username = request.form['username'].split()
      full_name = request.args.get('full_name', None)
      add_user_to_db(email, username, full_name, group_names)
      db_session.commit()
      print('Profile successfully created')
      return redirect(oid.get_next_url())
   elif request.method == 'GET':
      print('in get')
      email = request.args.get('email', None)
      group_names = request.args.get('groups', None).split()
      username = request.args.get('username', None)
      full_name = request.args.get('full_name', None)
      add_user_to_db(email, username, full_name, group_names)
      db_session.commit()
      print('Profile successfully created')
      return redirect(url_for('portal_user.index'))
   return redirect(url_for('portal_user.index'))
Example #18
0
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
Example #19
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