Пример #1
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'))
Пример #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'))
Пример #3
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)
Пример #4
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
Пример #5
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()
Пример #6
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))
Пример #7
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
Пример #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
Пример #9
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
Пример #10
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()
Пример #11
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()
Пример #12
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
Пример #13
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