jiahuimbzuai/CoQuIR huggingface dataset:
// updateConsent [javascript]
function updateConsent(database){
  //Still need to grab emails from a sensitive data file
  const emails = JSON.parse(fs.readFileSync('./tmp/consented-latest.json'))
  const emailString = emails.consented.map(x => "'" + x + "'").toString();
  const pool = new Pool({database});
  const sql = `
    INSERT INTO consented_email (email,audio,permission,consent) 
    SELECT x,TRUE,TRUE,TRUE 
    FROM unnest(ARRAY[${emailString}]) x;`;
  return pool.query(sql);
}
// write_keyval [javascript]
    let write_keyval = function(key, val) {
        obj.db.run(
          "INSERT INTO tb_events (uid, key, value) VALUES ( ? , ? , ? );",
          [event.uid, key, val]
        );
    };
// write_keyval [javascript]
    let write_keyval = function(key, val) {
        let query = "INSERT INTO tb_events (uid, key, value) VALUES "
                  + "("
                  + "'" + event.uid + "', "
                  + "'" + key + "', "
                  + "'" + val + "'"
                  + " );";
        obj.db.run(query);
    };
// module.exports.find [javascript]
module.exports.find = function(soc, successNext, errNext) {
    var connection = mysql.createConnection(config);
    connection.connect();

    var targetFields = ['Occupation.soc', 'title', 'wageType', 'averageWage', 'averageWageOutOfRange', 'lowWage', 'lowWageOutOfRange', 'medianWage', 'medianWageOutOfRange', 'highWage', 'highWageOutOfRange', 'educationRequired', 'currentEmployment', 'futureEmployment', 'careerGrowth', 'jobOpenings', 'naturalistPercent', 'musicalPercent', 'logicalPercent', 'existentialPercent', 'interpersonalPercent', 'bodyPercent', 'linguisticPercent', 'intrapersonalPercent', 'spatialPercent', 'skillsText'];
    var queryString = "SELECT ";
    for (i = 0; i < targetFields.length; i++) {
        queryString += targetFields[i];
        if ((i+1) < targetFields.length) {
            queryString += ", ";
        } else {
            queryString += " ";
        }
    }

    queryString += "FROM Skills, Occupation WHERE Occupation.soc = ? && Skills.soc = ?;";

    connection.query(queryString, [soc, soc], function(err, rows, fields) {
        if (err === null && rows.length == 1) {
            successNext(rows[0]);
            connection.end();
        } else {
            connection.query('SELECT * FROM Occupation WHERE soc = ?;', [soc], function(err2, rows2, fields2) {
                if (err2 === null && rows2.length == 1) {
                    successNext(rows2[0]);
                }
                else {
                    errNext(err2);
                };
            });
            connection.end();
        }
    });

/*
    connection.query('SELECT * FROM Occupation WHERE soc = ?;', [soc], function(err, rows, fields) {
        if (err === null && rows.length == 1) {
            successNext(rows[0]);
        }
        else {
            errNext(err);
        };
    });
*/
}
// module.exports.find [javascript]
module.exports.find = function(soc, successNext, errNext) {
    var connection = mysql.createConnection(config);
    connection.connect();

    var targetFields = ['Occupation.soc', 'title', 'wageType', 'averageWage', 'averageWageOutOfRange', 'lowWage', 'lowWageOutOfRange', 'medianWage', 'medianWageOutOfRange', 'highWage', 'highWageOutOfRange', 'educationRequired', 'currentEmployment', 'futureEmployment', 'careerGrowth', 'jobOpenings', 'naturalistPercent', 'musicalPercent', 'logicalPercent', 'existentialPercent', 'interpersonalPercent', 'bodyPercent', 'linguisticPercent', 'intrapersonalPercent', 'spatialPercent', 'skillsText'];
    var queryString = "SELECT ";
    for (i = 0; i < targetFields.length; i++) {
        queryString += targetFields[i];
        if ((i+1) < targetFields.length) {
            queryString += ", ";
        } else {
            queryString += " ";
        }
    }

    queryString += "FROM Skills, Occupation WHERE Occupation.soc = '" + soc + "' && Skills.soc = '" + soc + "';";

    connection.query(queryString, function(err, rows, fields) {
        if (err === null && rows.length == 1) {
            successNext(rows[0]);
            connection.end();
        } else {
            connection.query('SELECT * FROM Occupation WHERE soc = ?;', [soc], function(err2, rows2, fields2) {
                if (err2 === null && rows2.length == 1) {
                    successNext(rows2[0]);
                }
                else {
                    errNext(err2);
                };
            });
            connection.end();
        }
    });

/*
    connection.query('SELECT * FROM Occupation WHERE soc = ?;', [soc], function(err, rows, fields) {
        if (err === null && rows.length == 1) {
            successNext(rows[0]);
        }
        else {
            errNext(err);
        };
    });
*/
}
// insertUser [javascript]
const insertUser = (data,cb)=>{
  const sqlQuery = 'INSERT INTO users(email,privileges,first_name,last_name)VALUES($1,$2,$3,$4)';
  client.query(sqlQuery,[data.email,'1',data.firstName,data.lastName],(err,result)=>{
    cb(err,result);
  });
};
// insertUser [javascript]
const insertUser = (data,cb)=>{
  const sqlQuery = `INSERT INTO users(email,priveleges,first_name,last_name)VALUES('${data.email}','1','${data.firstName}','${data.lastName}')`;
  client.query(sqlQuery,(err,result)=>{
    cb(err,result);
  });
};
// (anonymous) [javascript]
    connection.query("SELECT * FROM Skills WHERE soc = ?", [soc], function(err, rows, fields) {
        if (err === null && rows.length == 1) {
            successNext(rows[0]);
        } else {
            successNext(null);
        }
    });
// (anonymous) [javascript]
    connection.query("SELECT * FROM Skills WHERE soc = '" + soc + "'", function(err, rows, fields) {
        if (err === null && rows.length == 1) {
            successNext(rows[0]);
        } else {
            successNext(null);
        }
    });
// (anonymous) [javascript]
    this.db.each("SELECT * FROM tb_events WHERE uid = ? ;", [uid], function(err, row) {
        event.uid = uid;
        obj.keyval_parse(event, row.key, row.value, row.payload);
        if (typeof event.times === 'string') {
          event.times = event.times.split(',').map(time=>+time)
        }
    }, function(err, rows) {
// (anonymous) [javascript]
    this.db.each("SELECT * FROM tb_events WHERE uid = '" + uid + "';", function(err, row) {
        event.uid = uid;
        obj.keyval_parse(event, row.key, row.value, row.payload);
        if (typeof event.times === 'string') {
          event.times = event.times.split(',').map(time=>+time)
        }
    }, function(err, rows) {
// selectUser [javascript]
const selectUser = (email,cb)=>{
  const sqlQuery = 'SELECT id,email,first_name,last_name from users WHERE email=$1';
  client.query(sqlQuery,[email],(err,result)=>{
    cb(err,result);
  });
};
// selectUser [javascript]
const selectUser = (data,cb)=>{
  const sqlQuery = `SELECT email,first_name,last_name from users WHERE email='${data.email}'`;
  client.query(sqlQuery,(err,result)=>{
    cb(err,result);
  });
};
// Database.prototype.write_event [javascript]
Database.prototype.write_event = function(event) {
    let obj = this;
    let write_keyval = function(key, val) {
        obj.db.run(
          "INSERT INTO tb_events (uid, key, value) VALUES ( ? , ? , ? );",
          [event.uid, key, val]
        );
    };

    [["name",        event.name],
     ["description", event.description],
     ["date",        event.date],
     ["times",       event.times.join(',')],
     ["owner",       event.owner]]
     .forEach(function(keyval) {
        write_keyval(keyval[0], keyval[1]);
    });
}; // end of function Database#write_event
// Database.prototype.write_event [javascript]
Database.prototype.write_event = function(event) {
    let obj = this;
    let write_keyval = function(key, val) {
        let query = "INSERT INTO tb_events (uid, key, value) VALUES "
                  + "("
                  + "'" + event.uid + "', "
                  + "'" + key + "', "
                  + "'" + val + "'"
                  + " );";
        obj.db.run(query);
    };

    [["name",        event.name],
     ["description", event.description],
     ["date",        event.date],
     ["times",       event.times],
     ["owner",       event.owner]]
     .forEach(function(keyval) {
        write_keyval(keyval[0], keyval[1]);
    });
}; // end of function Database#write_event
// (anonymous) [javascript]
    db.get("SELECT id,note FROM notes WHERE id = ?",[data.id],function(err,row){
      if(row){
        socket.emit('setNote', { note: row.note});
      } else {
        socket.emit('setNote', { note: "" });
      }
    //res.send(row.note);
    });
// (anonymous) [javascript]
    db.get("SELECT id,note FROM notes WHERE id = '"+data.id+"'",function(err,row){
      if(row){
        socket.emit('setNote', { note: row.note });
      } else {
        socket.emit('setNote', { note: "" });
      }
    //res.send(row.note);
    });
// (anonymous) [javascript]
    db.get("SELECT id,note FROM notes WHERE id = ?",[data.id],function(err,row){
      var newval;
      if(row){
        newval = row.note;
      } else {
        newval= "";
      }
      var op = data.op;
      if(op.d!==null) {
        newval = newval.slice(0,op.p)+newval.slice(op.p+op.d);
      }
      if(op.i!==null){
        newval = newval.insert(op.p,op.i);
      } 
      db.run("INSERT OR REPLACE INTO notes ('id', 'note') VALUES (?,?)",[data.id,newval]);
    });
// (anonymous) [javascript]
    db.get("SELECT id,note FROM notes WHERE id = '"+data.id+"'",function(err,row){
      var newval;
      if(row){
        newval = row.note;
      } else {
        newval= "";
      }
      var op = data.op;
      if(op.d!==null) {
        newval = newval.slice(0,op.p)+newval.slice(op.p+op.d);
      }
      if(op.i!==null){
        newval = newval.insert(op.p,op.i);
      } 
      db.run("INSERT OR REPLACE INTO notes ('id', 'note') VALUES ('"+data.id+"', '"+newval+"' )");
    });
// UserController.createOrJoinTeam [javascript]
UserController.createOrJoinTeam = function(id, code, callback){

  if (!code){
    return callback({
      message: "Please enter a team name."
    });
  }

  if (typeof code !== 'string') {
    return callback({
      message: "Get outta here, punk!"
    });
  }

  User.find({
    teamCode: code
  })
  .select('profile.name')
  .exec(function(err, users){
    // Check to see if this team is joinable (< team max size)
    if (users.length >= maxTeamSize){
      return callback({
        message: "Team is full."
      });
    }

    // Otherwise, we can add that person to the team.
    User.findOneAndUpdate({
      _id: id,
      verified: true
    },{
      $set: {
        teamCode: code
      }
    }, {
      new: true
    },
    callback);

  });
};
// UserController.createOrJoinTeam [javascript]
UserController.createOrJoinTeam = function(id, code, callback){

  if (!code){
    return callback({
      message: "Please enter a team name."
    });
  }

  User.find({
    teamCode: code
  })
  .select('profile.name')
  .exec(function(err, users){
    // Check to see if this team is joinable (< team max size)

    if (users.length >= maxTeamSize){
      return callback({
        message: "Team is full."
      });
    }

    // Otherwise, we can add that person to the team.
    User.findOneAndUpdate({
      _id: id,
      verified: true
    },{
      $set: {
        teamCode: code
      }
    }, {
      new: true
    },
    callback);

  });
};
// (anonymous) [javascript]
socket.on("setNote",function(data){
	oldval = data.note;
	$("#note").html(oldval);
});
// (anonymous) [javascript]
socket.on("setNote",function(data){
	oldval = data.note;
	$("#note").html(data.note);
});
// verify_credentials [python]
    async def verify_credentials(self, login, password):
        """ verify login and password """

        try:
            async with aiopg.create_pool(self.dsn) as pool:
                async with pool.acquire() as conn:
                    async with conn.cursor() as cur:
                        query = f"""
                SELECT id, password, salt
                FROM aio.users
                WHERE aio.users.login = %s"""
                        await cur.execute(query, (login, ))
                        async for user_id, user_password, user_salt in cur:
                            _, test_hash = hashpass(password, user_salt)
                            if test_hash.decode() == user_password:
                                return user_id
        except Exception as err:
            print(err)
            raise web.HTTPForbidden()
// verify_credentials [python]
    async def verify_credentials(self, login, password):
        """ verify login and password """

        try:
            async with aiopg.create_pool(self.dsn) as pool:
                async with pool.acquire() as conn:
                    async with conn.cursor() as cur:
                        query = f"""
                SELECT id, password, salt
                FROM aio.users
                WHERE aio.users.login = '{login}'"""
                        await cur.execute(query)
                        async for user_id, user_password, user_salt in cur:
                            _, test_hash = hashpass(password, user_salt)
                            if test_hash.decode() == user_password:
                                return user_id
        except Exception as err:
            print(err)
            raise HTTPForbidden()
// reset_passwd [python]
@app.route('/reset-password', methods=['GET', 'POST'])
def reset_passwd():
  if request.method == 'POST':
    if 'username' not in session:
      flash('You did something naughty')
      return redirect(url_for('forgot_passwd'))
    new_password = request.form['password']
    new_password2 = request.form['password2']

    # Get the user's email
    query = text("SELECT email FROM members WHERE user_id=:id")
    result = connection.execute(query, id=str(session['user_id']))
    email = result.first()[0]

    if new_password != new_password2:
      flash('Passwords do not match. Please try again!')
      return render_template('reset_password.html')
    elif auth.passwd_reset(session['username'], new_password, connection, \
                           email=email):
      session.pop('username')
      flash('Password successfully changed.')
      return redirect(url_for('home'))
    else:
      flash('An unknown problem has occured. Please contact an admin!')
      return render_template('reset_password.html')
  else:
    user_id = request.args.get('u', None)
    reset_key = request.args.get('r', None)
    if user_id == None or reset_key == None:
      flash("Missing parameter. Try generating the link again?")
      return redirect(url_for('forgot_passwd'))
    query = text("SELECT * FROM users WHERE user_id=:u")
    result = connection.execute(query, u=str(user_id))
    if result.returns_rows and result.rowcount != 0:
      result_cols = result.keys()
      row = result.first()
      q_dict = dict(zip(result_cols, row))
      if int(reset_key) == auth.reset_key(q_dict['passwd'], q_dict['salt'],
                                          q_dict['username']):
        session['username'] = q_dict['username']
        return render_template('reset_password.html')
      flash("Incorrect reset_key. Try generating the link again?")
    return redirect(url_for('forgot_passwd'))
// reset_passwd [python]
@app.route('/reset-password', methods=['GET', 'POST'])
def reset_passwd():
  if request.method == 'POST':
    if 'username' not in session:
      flash('You did something naughty')
      return redirect(url_for('forgot_passwd'))
    new_password = request.form['password']
    new_password2 = request.form['password2']

    # Get the user's email
    query = text("SELECT email FROM members WHERE user_id=:id")
    result = connection.execute(query, id=str(session['user_id']))
    email = result.first()[0]

    if new_password != new_password2:
      flash('Passwords do not match. Please try again!')
      return render_template('reset_password.html')
    elif auth.passwd_reset(session['username'], new_password, connection, \
                           email=email):
      session.pop('username')
      flash('Password successfully changed.')
      return redirect(url_for('home'))
    else:
      flash('An unknown problem has occured. Please contact an admin!')
      return render_template('reset_password.html')
  else:
    user_id = request.args.get('u', None)
    reset_key = request.args.get('r', None)
    if user_id == None or reset_key == None:
      flash("Missing parameter. Try generating the link again?")
      return redirect(url_for('forgot_passwd'))
    query = text("SELECT * FROM users NATURAL JOIN members WHERE user_id=:u")
    result = connection.execute(query, u=str(user_id))
    if result.returns_rows and result.rowcount != 0:
      result_cols = result.keys()
      row = result.first()
      q_dict = dict(zip(result_cols, row))
      if int(reset_key) == auth.reset_key(q_dict['passwd'], q_dict['salt'],
                                          q_dict['username']):
        session['username'] = q_dict['username']
        return render_template('reset_password.html')
      flash("Incorrect reset_key. Try generating the link again?")
    return redirect(url_for('forgot_passwd'))
// create_token [python]
    async def create_token(self, uid):
        """ get session token by user id """
        try:
            token = hashtoken().decode()
            async with aiopg.create_pool(self.dsn) as pool:
                async with pool.acquire() as conn:
                    async with conn.cursor() as cur:
                        query = f"""
                INSERT INTO aio.tokens (token, user_id)
                VALUES ('{token}', '{uid}') """
                        await cur.execute(query)
                        return token
        except Exception as err:
            print(err)
            raise web.HTTPForbidden()
// create_token [python]
    async def create_token(self, uid):
        """ get session token by user id """
        try:
            token = hashtoken().decode()
            async with aiopg.create_pool(self.dsn) as pool:
                async with pool.acquire() as conn:
                    async with conn.cursor() as cur:
                        query = f"""
                INSERT INTO aio.tokens (token, user_id)
                VALUES ('{token}', '{uid}') """
                        await cur.execute(query)
                        return token
        except Exception as err:
            print(err)
            raise HTTPForbidden()
// _create_database [python]
    @staticmethod
    def _create_database(engine: "Engine", db: Text):
        """Create database `db` on `engine` if it does not exist."""

        import psycopg2

        conn = engine.connect()

        cursor = conn.connection.cursor()
        cursor.execute("COMMIT")
        cursor.execute(
            f"SELECT 1 FROM pg_catalog.pg_database WHERE datname = ?", (db,)  # nosec
        )
        exists = cursor.fetchone()
        if not exists:
            try:
                cursor.execute(f"CREATE DATABASE {db}")
            except psycopg2.IntegrityError as e:
                logger.error(f"Could not create database '{db}': {e}")

        cursor.close()
        conn.close()
// _create_database [python]
    @staticmethod
    def _create_database(engine: "Engine", db: Text):
        """Create database `db` on `engine` if it does not exist."""

        import psycopg2

        conn = engine.connect()

        cursor = conn.connection.cursor()
        cursor.execute("COMMIT")
        cursor.execute(f"SELECT 1 FROM pg_catalog.pg_database WHERE datname = '{db}'")
        exists = cursor.fetchone()
        if not exists:
            try:
                cursor.execute(f"CREATE DATABASE {db}")
            except psycopg2.IntegrityError as e:
                logger.error(f"Could not create database '{db}': {e}")

        cursor.close()
        conn.close()
// show_user_profile [python]
@app.route('/users/view/<username>')
def show_user_profile(username):
  """ Procedure to show a user's profile and membership details. """
  cols = [["username"], ["fname", "lname"], ["nickname"], ["bday"], \
          ["email"], ["email2"], ["status"], ["matriculate_year"], \
          ["grad_year"], ["msc"], ["phone"], ["building", "room_num"], \
          ["membership"], ["major"], ["uid"], ["isabroad"]]
  display = ["Username", "Name", "Nickname", "Birthday", "Primary Email", \
             "Secondary Email", "Status", "Matriculation Year", \
             "Graduation Year", "MSC", "Phone Number", "Residence", \
             "Membership", "Major", "UID", "Is Abroad"]
  d_dict = OrderedDict(zip(display, cols))
  #d_dict defines the order and mapping of displayed attributes to sql columns
  query = text("SELECT * FROM users NATURAL JOIN members WHERE username=:u")
  result = connection.execute(query, u=str(username))
  if result.returns_rows and result.rowcount != 0:
    result_cols = result.keys()
    r = result.first()
    q_dict = dict(zip(result_cols, r)) #q_dict maps sql columns to values
    if not q_dict['usenickname']:
      d_dict.pop('Nickname')
    return render_template('view_user.html', display = d_dict, info = q_dict, \
      strftime = strftime)
  else:
    flash("User does not exist!")
    return redirect(url_for('home'))
// show_user_profile [python]
@app.route('/users/view/<username>')
def show_user_profile(username):
  """ Procedure to show a user's profile and membership details. """
  cols = [["username"], ["fname", "lname"], ["nickname"], ["bday"], \
          ["email"], ["email2"], ["status"], ["matriculate_year"], \
          ["grad_year"], ["msc"], ["phone"], ["building", "room_num"], \
          ["membership"], ["major"], ["uid"], ["isabroad"]]
  display = ["Username", "Name", "Nickname", "Birthday", "Primary Email", \
             "Secondary Email", "Status", "Matriculation Year", \
             "Graduation Year", "MSC", "Phone Number", "Residence", \
             "Membership", "Major", "UID", "Is Abroad"]
  d_dict = OrderedDict(zip(display, cols))
  #d_dict defines the order and mapping of displayed attributes to sql columns
  query = text("SELECT * FROM users Natural JOIN members where username=:u")
  result = connection.execute(query, u=str(username))
  if result.returns_rows and result.rowcount != 0:
    result_cols = result.keys()
    r = result.first()
    q_dict = dict(zip(result_cols, r)) #q_dict maps sql columns to values
    if not q_dict['usenickname']:
      d_dict.pop('Nickname')
    return render_template('view_user.html', display = d_dict, info = q_dict, \
      strftime = strftime)
  else:
    flash("User does not exist!")
    return redirect(url_for('home'))
// sign_up_page [python]
def sign_up_page():
  if request.method == 'POST':
    username = request.form['username']
    password = request.form['password']
    hashed_password = pwd_context.encrypt(password)
    email = request.form['email']
    id = 1


    with dbapi2.connect(current_app.config['dsn']) as connection:
      cursor = connection.cursor()

      query = """
            INSERT INTO USERS (USERNAME, PASSWORD, EMAIL) 
            VALUES (%s, %s, %s)"""

      cursor.execute(query, (username, hashed_password, email))

      connection.commit()
    return render_template('home.html')

  else:
    return render_template('sign_up.html')
// sign_up_page [python]
def sign_up_page():
  if request.method == 'POST':
    username = request.form['username']
    password = request.form['password']
    hashed_password = pwd_context.encrypt(password)
    email = request.form['email']
    id = 1


    with dbapi2.connect(current_app.config['dsn']) as connection:
      cursor = connection.cursor()

      query = """
            INSERT INTO USERS (USERNAME, PASSWORD, EMAIL) 
            VALUES ('%s', '%s', '%s')""" % (username, hashed_password, email)

      cursor.execute(query)

      connection.commit()
    return render_template('home.html')

  else:
    return render_template('sign_up.html')
// authenticate [python]
def authenticate(user, passwd, db):
  """ Takes a username, password, and connection object as input, and checks
  if this corresponds to an actual user. Salts the password as necessary. """
  # get salt and add to password if necessary
  saltQuery = db.execute(text("SELECT salt FROM users WHERE username=:u"),
                         u = user)
  # if there's a salt set, then use it
  if saltQuery.returns_rows:
    salt = saltQuery.first()
    # make sure username is found and salt isn't null
    if salt != None and salt[0] != None:
      passwd = salt[0] + passwd

  # query whether there's a match for the username and password
  query = db.execute(text("SELECT * FROM users WHERE username=:u AND " \
                          + "passwd=MD5(:p)"), u = user, p = passwd)

  row = query.first()
  if (query.returns_rows and row != None):
    return row[0]
  return 0
// authenticate [python]
def authenticate(user, passwd, db):
  """ Takes a username, password, and connection object as input, and checks
  if this corresponds to an actual user. Salts the password as necessary. """
  # get salt and add to password if necessary
  saltQuery = db.execute("SELECT salt FROM users WHERE username='" + user \
          + "'")
  # if there's a salt set, then use it
  if saltQuery.returns_rows:
    salt = saltQuery.first()
    # make sure username is found and salt isn't null
    if salt != None and salt[0] != None:
      passwd = salt[0] + passwd

  # query whether there's a match for the username and password
  query = db.execute("SELECT * FROM users WHERE username='" + user + "' AND " \
          + "passwd=MD5('" + passwd + "')")

  row = query.first()
  if (query.returns_rows and row != None):
    return row[0]
  return 0
// urlmatch [python]
def urlmatch(url):
    short = url.lower()
    match = query_db("select dest from urls where short=?", [short])
    if len(match) == 0:
        # no match found
        return "No match found"
    destination = match[0]['dest']
    # work out src ip version
    srcip = request.remote_addr
    ip = ipaddr.IPAddress(srcip)
    hittoday = query_db("select * from hits where short=? and hitdate=date('now')", [short])
    hitfield = "hits%s" % (ip.version)
    # update hit counters
    if len(hittoday) == 0:
        get_db().execute("insert into hits (short,hitdate,%s) values (?,date('now'),1)" % (hitfield) ,[short])
    else:
        get_db().execute("update hits set %s=%s+1 where short=?" % (hitfield,hitfield) ,[short])
    get_db().commit()
    # finally redirect
    return redirect(destination,code=302)
// urlmatch [python]
def urlmatch(url):
    short = url.lower()
    match = query_db("select dest from urls where short='%s'" % (short))
    if len(match) == 0:
        # no match found
        return "No match found"
    destination = match[0]['dest']
    # work out src ip version
    srcip = request.remote_addr
    ip = ipaddr.IPAddress(srcip)
    hittoday = query_db("select * from hits where short='%s' and hitdate=date('now')" % (short))
    hitfield = "hits%s" % (ip.version)
    # update hit counters
    if len(hittoday) == 0:
        get_db().execute("insert into hits (short,hitdate,%s) values (?,date('now'),1)" % (hitfield), [short])
    else:
        get_db().execute("update hits set %s=%s+1 where short=?" % (hitfield,hitfield), [short])
    get_db().commit()
    # finally redirect
    return redirect(destination,code=302)
// unique_short [python]
def unique_short():
    matches = 1
    while matches == 1:
        short = generate_short()
    	matches = query_db("select * from urls where short=?", [short])
    return short
// unique_short [python]
def unique_short():
    matches = 1
    while matches == 1:
        short = generate_short()
    	matches = query_db("select * from urls where short='%s'" % (short))
    return short
// store_order [python]
    async def store_order(self, uid, tree_id, order, ordered_at):
        """ store remote order """

        try:
            async with aiopg.create_pool(self.dsn) as pool:
                async with pool.acquire() as conn:
                    async with conn.cursor() as cur:
                        query = f"""
                INSERT INTO aio.orders(manager, tree, order_data, ordered_at)
                VALUES ('{uid}', '{tree_id}', '{order_data}', '{ordered_at}') """
                        return await cur.scalar(query)
        except Exception as err:
            print(err)
            raise web.HTTPForbidden()
// store_order [python]
    async def store_order(self, uid, tree_id, order, ordered_at):
        """ store remote order """

        try:
            async with aiopg.create_pool(self.dsn) as pool:
                async with pool.acquire() as conn:
                    async with conn.cursor() as cur:
                        query = f"""
                INSERT INTO aio.orders(manager, tree, order_data, ordered_at)
                VALUES ('{uid}', '{tree_id}', '{order_data}', '{ordered_at}') """
                        return await cur.scalar(query)
        except Exception as err:
            print(err)
            raise HTTPForbidden()
// change_user_settings [python]
@app.route('/users/edit/<username>', methods=['GET', 'POST'])
def change_user_settings(username):
  """ Procedure to process the login page. Also handles authentication. """

  if 'username' not in session:
    flash('You must be logged in for that.')
    return redirect(url_for('login'))

  if session['username'] != username:
    flash('You cannot edit this user\'s information.')
    return redirect(url_for('show_user_profile', username=username))


  params = {}
  tags = ['nickname', 'usenickname', 'bday', 'email', 'email2', 'msc', 'phone', \
      'building', 'room_num', 'major', 'isabroad']
  tag_names = ["Nickname", "Use Nickname", "Birthday", "Email Address", \
      "Alt. Email Address", "MSC", "Phone Number", "Building Name", "Room Number", \
      "Major", "Is Abroad"]

  # Get stored values from database
  query = text("SELECT * FROM users NATURAL JOIN members WHERE username=:u")
  result = connection.execute(query, u=str(username))
  if result.returns_rows and result.rowcount != 0:
    result_cols = result.keys()
    r = result.first()
    stored_params = dict(zip(result_cols, r)) #stored_params maps sql columns to values

  # Update if needed
  if request.method == 'POST':

    for (i, tag) in enumerate(tags):
      params[tag] = request.form[tag]


    for (i, tag) in enumerate(tags):
      if str(params[tag]) != str(stored_params[tag]):

        new_val = str(params[tag])
        if tag in ['usenickname', 'msc', 'room_num', 'isabroad']:
          new_val = int(new_val)

        query = text("UPDATE members SET %s = :val WHERE user_id = :u" % tag)
        results = connection.execute(query, u=session['user_id'], val=new_val)

        flash("%s was updated!" % tag_names[i])


  if not params:
    params = stored_params

  return render_template('edit_user.html', params = params)
// change_user_settings [python]
@app.route('/users/edit/<username>', methods=['GET', 'POST'])
def change_user_settings(username):
  """ Procedure to process the login page. Also handles authentication. """

  if 'username' not in session:
    flash('You must be logged in for that.')
    return redirect(url_for('login'))

  if session['username'] != username:
    flash('You cannot edit this user\'s information.')
    return redirect(url_for('show_user_profile', username=username))


  params = {}
  tags = ['nickname', 'usenickname', 'bday', 'email', 'email2', 'msc', 'phone', \
      'building', 'room_num', 'major', 'isabroad']
  tag_names = ["Nickname", "Use Nickname", "Birthday", "Email Address", \
      "Alt. Email Address", "MSC", "Phone Number", "Building Name", "Room Number", \
      "Major", "Is Abroad"]

  # Get stored values from database
  query = text("SELECT * FROM users Natural JOIN members where username=:u")
  result = connection.execute(query, u=str(username))
  if result.returns_rows and result.rowcount != 0:
    result_cols = result.keys()
    r = result.first()
    stored_params = dict(zip(result_cols, r)) #stored_params maps sql columns to values

  # Update if needed
  if request.method == 'POST':

    for (i, tag) in enumerate(tags):
      params[tag] = request.form[tag]


    for (i, tag) in enumerate(tags):
      if str(params[tag]) != str(stored_params[tag]):

        new_val = str(params[tag])
        if tag in ['usenickname', 'msc', 'room_num', 'isabroad']:
          new_val = int(new_val)

        query = text("UPDATE members SET %s = :val WHERE user_id = :u" % tag)
        results = connection.execute(query, u=session['user_id'], val=new_val)

        flash("%s was updated!" % tag_names[i])


  if not params:
    params = stored_params

  return render_template('edit_user.html', params = params)
// login_page [python]
def login_page():
  if request.method == 'POST':
    login_email = request.form['login_email']
    # print( "%s" % login_email)
    login_password = request.form['login_password']
    hashed_login_password = pwd_context.encrypt(login_password)

    with dbapi2.connect(current_app.config['dsn']) as connection:
      cursor = connection.cursor()
      statement = """SELECT USERNAME FROM USERS WHERE USERNAME = %s"""
      cursor.execute(statement, [login_email])
      db_username = cursor.fetchone()

      if db_username is not None:  # check whether the user exists
        print('%s' % db_username)
        user = load_user(db_username)
        login_user(user)
        print("%s" % user.username)
        # print('%s %s' % db_username[0][0], db_username[0][1] ) if the fetchall method is used debug using this line

    return render_template('home.html')
  else:
    return render_template('login.html')
// login_page [python]
def login_page():
  if request.method == 'POST':
    login_email = request.form['login_email']
    # print( "%s" % login_email)
    login_password = request.form['login_password']
    hashed_login_password = pwd_context.encrypt(login_password)

    with dbapi2.connect(current_app.config['dsn']) as connection:
      cursor = connection.cursor()
      statement = """SELECT USERNAME FROM USERS WHERE USERNAME = %s"""
      cursor.execute(statement, [login_email])
      db_username = cursor.fetchone()

      if db_username is not None:  # check whether the user exists
        print('%s' % db_username)
        user = load_user(db_username);
        login_user(user);
        print("%s" % user.username)
        # print('%s %s' % db_username[0][0], db_username[0][1] ) if the fetchall method is used debug using this line

    return render_template('home.html')
  else:
    return render_template('login.html')
// PropertyPage [python]
@site.route('/lost_found', methods=['GET', 'POST'])
def PropertyPage():
    if request.method == "POST":
        formtype = request.form['form-name']
        if formtype == "LostSomething":
            lostdesc = request.form['LostSomethingDescription']
            lostlocation = request.form['LostSomethingPossibleLocation']
            lostdate = request.form['LostSomethingDate']
            lostowner = request.form['LostSomethingOwnerName']
            lostmail = request.form['LostSomethingOwnerMail']
            lostphone = request.form['LostSomethingOwnerPhone']

            with dbapi2.connect(current_app.config['dsn']) as connection:
                cursor = connection.cursor()#prevented sql injection
                query = """INSERT INTO LOSTSTUFF(STUFFDESC, POSSIBLELOC, POSSIBLEDATE, OWNERNAME, OWNERMAIL, OWNERPHONE) VALUES (%s, %s, %s, %s, %s, %s)"""
                cursor.execute(query, (lostdesc, lostlocation, lostdate, lostowner, lostmail, lostphone))
                connection.commit()
        else:
            founddesc = request.form['FoundSomethingDescription']
            foundlocation = request.form['FoundSomethingCurrentLocation']
            founddate = request.form['FoundSomethingDate']
            foundname = request.form['FoundSomethingFinderName']
            foundmail = request.form['FoundSomethingFinderMail']
            foundphone = request.form['FoundSomethingFinderPhone']

            with dbapi2.connect(current_app.config['dsn']) as connection:
                cursor = connection.cursor()#prevented sql injection
                query = """INSERT INTO FOUNDSTUFF(STUFFDESC, CURRENTLOC, FINDINGDATE, FOUNDERNAME, FOUNDERMAIL, FOUNDERPHONE) VALUES (%s, %s, %s, %s, %s, %s)"""
                cursor.execute(query, (founddesc, foundlocation, founddate, foundname, foundmail, foundphone))
                connection.commit()

        return render_template('lost_found.html')
    else:
        return render_template('lost_found.html')
// PropertyPage [python]
@site.route('/lost_found', methods=['GET', 'POST'])
def PropertyPage():
    if request.method == "POST":
        formtype = request.form['form-name']
        if formtype == "LostSomething":
            lostdesc = request.form['LostSomethingDescription']
            lostlocation = request.form['LostSomethingPossibleLocation']
            lostdate = request.form['LostSomethingDate']
            lostowner = request.form['LostSomethingOwnerName']
            lostmail = request.form['LostSomethingOwnerMail']
            lostphone = request.form['LostSomethingOwnerPhone']

            with dbapi2.connect(current_app.config['dsn']) as connection:
                cursor = connection.cursor()
                query = """INSERT INTO LOSTSTUFF(STUFFDESC, POSSIBLELOC, POSSIBLEDATE, OWNERNAME, OWNERMAIL, OWNERPHONE) VALUES ('%s', '%s', '%s', '%s', '%s', '%s')""" % (lostdesc, lostlocation, lostdate, lostowner, lostmail, lostphone)
                cursor.execute(query)
                connection.commit()
        else:
            founddesc = request.form['FoundSomethingDescription']
            foundlocation = request.form['FoundSomethingCurrentLocation']
            founddate = request.form['FoundSomethingDate']
            foundname = request.form['FoundSomethingFinderName']
            foundmail = request.form['FoundSomethingFinderMail']
            foundphone = request.form['FoundSomethingFinderPhone']

            with dbapi2.connect(current_app.config['dsn']) as connection:
                cursor = connection.cursor()
                query = """INSERT INTO FOUNDSTUFF(STUFFDESC, CURRENTLOC, FINDINGDATE, FOUNDERNAME, FOUNDERMAIL, FOUNDERPHONE) VALUES ('%s', '%s', '%s', '%s', '%s', '%s')""" % (founddesc, foundlocation, founddate, foundname, foundmail, foundphone)
                cursor.execute(query)
                connection.commit()

        return render_template('lost_found.html')
    else:
        return render_template('lost_found.html')
// reroute [python]
@app.route('/<short_url>')
def reroute(short_url):

	conn = MySQLdb.connect(host , user , passwrd, db)
	cursor = conn.cursor()
	platform = request.user_agent.platform
	browser =  request.user_agent.browser
	counter = 1

	# Platform , Browser vars
	
	browser_dict = {'firefox': 0 , 'chrome':0 , 'safari':0 , 'other':0}
	platform_dict = {'windows':0 , 'iphone':0 , 'android':0 , 'linux':0 , 'macos':0 , 'other':0}

	# Analytics
	if browser in browser_dict:
		browser_dict[browser] += 1
	else:								
		browser_dict['other'] += 1
	
	if platform in platform_dict.iterkeys():
		platform_dict[platform] += 1
	else:
		platform_dict['other'] += 1
			
	cursor.execute("SELECT URL FROM WEB_URL WHERE S_URL = %s;" ,(short_url,) )

	try:
		new_url = cursor.fetchone()[0]
		print new_url
		# Update Counters 
		
		counter_sql = "\
				UPDATE {tn} SET COUNTER = COUNTER + {og_counter} , CHROME = CHROME + {og_chrome} , FIREFOX = FIREFOX+{og_firefox} ,\
				SAFARI = SAFARI+{og_safari} , OTHER_BROWSER =OTHER_BROWSER+ {og_oth_brow} , ANDROID = ANDROID +{og_andr} , IOS = IOS +{og_ios},\
				WINDOWS = WINDOWS+{og_windows} , LINUX = LINUX+{og_linux}  , MAC =MAC+ {og_mac} , OTHER_PLATFORM =OTHER_PLATFORM+ {og_plat_other} WHERE S_URL = %s;".\
				format(tn = "WEB_URL" , og_counter = counter , og_chrome = browser_dict['chrome'] , og_firefox = browser_dict['firefox'],\
				og_safari = browser_dict['safari'] , og_oth_brow = browser_dict['other'] , og_andr = platform_dict['android'] , og_ios = platform_dict['iphone'] ,\
				og_windows = platform_dict['windows'] , og_linux = platform_dict['linux'] , og_mac = platform_dict['macos'] , og_plat_other = platform_dict['other'])
		res_update = cursor.execute(counter_sql, (short_url, ))
		conn.commit()
		conn.close()

		return redirect(new_url)

	except Exception as e:
		e = "Something went wrong.Please try again."
		return render_template('404.html') ,404
// reroute [python]
@app.route('/<short_url>')
def reroute(short_url):

	conn = MySQLdb.connect(host , user , passwrd, db)
	cursor = conn.cursor()
	platform = request.user_agent.platform
	browser =  request.user_agent.browser
	counter = 1

	# Platform , Browser vars
	
	browser_dict = {'firefox': 0 , 'chrome':0 , 'safari':0 , 'other':0}
	platform_dict = {'windows':0 , 'iphone':0 , 'android':0 , 'linux':0 , 'macos':0 , 'other':0}

	# Analytics
	if browser in browser_dict:
		browser_dict[browser] += 1
	else:								
		browser_dict['other'] += 1
	
	if platform in platform_dict.iterkeys():
		platform_dict[platform] += 1
	else:
		platform_dict['other'] += 1
			
	cursor.execute("SELECT URL FROM WEB_URL WHERE S_URL = %s;" ,(short_url,) )

	try:
		new_url = cursor.fetchone()[0]
		print new_url
		# Update Counters 
		
		counter_sql = "\
				UPDATE {tn} SET COUNTER = COUNTER + {og_counter} , CHROME = CHROME + {og_chrome} , FIREFOX = FIREFOX+{og_firefox} ,\
				SAFARI = SAFARI+{og_safari} , OTHER_BROWSER =OTHER_BROWSER+ {og_oth_brow} , ANDROID = ANDROID +{og_andr} , IOS = IOS +{og_ios},\
				WINDOWS = WINDOWS+{og_windows} , LINUX = LINUX+{og_linux}  , MAC =MAC+ {og_mac} , OTHER_PLATFORM =OTHER_PLATFORM+ {og_plat_other} WHERE S_URL = '{surl}';".\
				format(tn = "WEB_URL" , og_counter = counter , og_chrome = browser_dict['chrome'] , og_firefox = browser_dict['firefox'],\
				og_safari = browser_dict['safari'] , og_oth_brow = browser_dict['other'] , og_andr = platform_dict['android'] , og_ios = platform_dict['iphone'] ,\
				og_windows = platform_dict['windows'] , og_linux = platform_dict['linux'] , og_mac = platform_dict['macos'] , og_plat_other = platform_dict['other'] ,\
				surl = short_url)
		res_update = cursor.execute(counter_sql)
		conn.commit()
		conn.close()

		return redirect(new_url)

	except Exception as e:
		e = "Something went wrong.Please try again."
		return render_template('404.html') ,404
// autocomplete_phrases [python]
def autocomplete_phrases(query):
    query_statement = sql.text(ur"""
        with s as (SELECT id, lower(title) as lower_title FROM pub_2018 WHERE title iLIKE :p0)
        select match, count(*) as score from (
            SELECT regexp_matches(lower_title, :p1, 'g') as match FROM s
            union all
            SELECT regexp_matches(lower_title, :p2, 'g') as match FROM s
            union all
            SELECT regexp_matches(lower_title, :p3, 'g') as match FROM s
            union all
            SELECT regexp_matches(lower_title, :p4, 'g') as match FROM s
        ) s_all
        group by match
        order by score desc, length(match::text) asc
        LIMIT 50;""").bindparams(
            p0='%{}%'.format(query),
            p1=ur'({}\w*?\M)'.format(query),
            p2=ur'({}\w*?(?:\s+\w+){{1}})\M'.format(query),
            p3=ur'({}\w*?(?:\s+\w+){{2}})\M'.format(query),
            p4=ur'({}\w*?(?:\s+\w+){{3}}|)\M'.format(query)
        )

    rows = db.engine.execute(query_statement).fetchall()
    phrases = [{"phrase":row[0][0], "score":row[1]} for row in rows if row[0][0]]
    return phrases
// autocomplete_phrases [python]
def autocomplete_phrases(query):
    query_string = ur"""
        with s as (SELECT id, lower(title) as lower_title FROM pub_2018 WHERE title iLIKE '%{query}%')
        select match, count(*) as score from (
            SELECT regexp_matches(lower_title, '({query}\w*?\M)', 'g') as match FROM s
            union all
            SELECT regexp_matches(lower_title, '({query}\w*?(?:\s+\w+){{1}})\M', 'g') as match FROM s
            union all
            SELECT regexp_matches(lower_title, '({query}\w*?(?:\s+\w+){{2}})\M', 'g') as match FROM s
            union all
            SELECT regexp_matches(lower_title, '({query}\w*?(?:\s+\w+){{3}}|)\M', 'g') as match FROM s
        ) s_all
        group by match
        order by score desc, length(match::text) asc
        LIMIT 50;""".format(query=query)

    rows = db.engine.execute(sql.text(query_string)).fetchall()
    phrases = [{"phrase":row[0][0], "score":row[1]} for row in rows if row[0][0]]
    return phrases
// tile_by_id [python]
def tile_by_id(id,path):
    '''
    ''' 

    conn = mysql_connection()
    mysql = conn.cursor(cursor_class=MySQLCursorDict)
    
    tms_path = '.'.join(path.split('.')[:-1])
    bucket = aws_prefix+'stuff'
    opaque = False
    
    image = Image.new('RGBA', (256, 256), (0, 0, 0, 0))
    
    if request.endpoint == "tilemap": 
        mysql.execute("SELECT tiles FROM maps WHERE id = %s", (id, ))
    elif request.endpoint == "tileatlas":
        mysql.execute("SELECT tiles FROM maps WHERE atlas_id = %s AND image IS NOT NULL ORDER BY image DESC", (id, ))

    items = mysql.fetchdicts()

    conn.close()
    
    if items:
        for item in items:
            if 'tiles' in item and item['tiles'] != None:
                #s3_path = 'maps/%s/%s/%s.png' % (item.name, item['tiles'], tms_path)
                s3_path = '%s/%s.png' % ( item['tiles'], tms_path)
                url = 'http://%(bucket)s.s3.amazonaws.com/%(s3_path)s' % locals()
        
                try:
                    tile_img = Image.open(StringIO(urlopen(url).read()))
                except IOError: 
                    continue
        
                fresh_img = Image.new('RGBA', (256, 256), (0, 0, 0, 0))
                fresh_img.paste(tile_img, (0, 0), tile_img)
                fresh_img.paste(image, (0, 0), image)
                image = fresh_img
        
            if Stat(image).extrema[3][0] > 0:
                opaque = True         
                break  
    
    if not opaque:
        url = 'http://tile.stamen.com/toner-lite/%s.png' % tms_path
        tile_img = Image.open(StringIO(urlopen(url).read()))
        tile_img.paste(image, (0, 0), image)
        image = tile_img
        

    bytes = StringIO()
    image.save(bytes, 'JPEG')
    
    resp = make_response(bytes.getvalue(), 200)
    resp.headers['Content-Type'] = 'image/jpeg'

    return resp
// tile_by_id [python]
def tile_by_id(id,path):
    '''
    ''' 

    conn = mysql_connection()
    mysql = conn.cursor(cursor_class=MySQLCursorDict)
    
    tms_path = '.'.join(path.split('.')[:-1])
    bucket = aws_prefix+'stuff'
    opaque = False
    
    image = Image.new('RGBA', (256, 256), (0, 0, 0, 0))
    
    if request.endpoint == "tilemap": 
        mysql.execute("SELECT tiles FROM maps WHERE id = '%s'" % id)
    elif request.endpoint == "tileatlas":
        mysql.execute("SELECT tiles FROM maps WHERE atlas_id = '%s' AND image IS NOT NULL ORDER BY image DESC" % id)

    items = mysql.fetchdicts()

    conn.close()
    
    if items:
        for item in items:
            if 'tiles' in item and item['tiles'] != None:
                #s3_path = 'maps/%s/%s/%s.png' % (item.name, item['tiles'], tms_path)
                s3_path = '%s/%s.png' % ( item['tiles'], tms_path)
                url = 'http://%(bucket)s.s3.amazonaws.com/%(s3_path)s' % locals()
        
                try:
                    tile_img = Image.open(StringIO(urlopen(url).read()))
                except IOError: 
                    continue
        
                fresh_img = Image.new('RGBA', (256, 256), (0, 0, 0, 0))
                fresh_img.paste(tile_img, (0, 0), tile_img)
                fresh_img.paste(image, (0, 0), image)
                image = fresh_img
        
            if Stat(image).extrema[3][0] > 0:
                opaque = True         
                break  
    
    if not opaque:
        url = 'http://tile.stamen.com/toner-lite/%s.png' % tms_path
        tile_img = Image.open(StringIO(urlopen(url).read()))
        tile_img.paste(image, (0, 0), image)
        image = tile_img
        

    bytes = StringIO()
    image.save(bytes, 'JPEG')
    
    resp = make_response(bytes.getvalue(), 200)
    resp.headers['Content-Type'] = 'image/jpeg'

    return resp