Example #1
0
def index():
    db = get_db()
    posts = db.execute(
        'SELECT p.id, title, body, created, author_id, username'
        ' FROM post p JOIN user u ON p.author_id = u.id'
        ' ORDER BY created DESC'
    ).fetchall()
    return render_template('blog/index.html', posts=posts)
Example #2
0
def get_post(id, check_author=True):
    post = get_db().execute(
        'SELECT p.id, title, body, created, author_id, username'
        ' FROM post p JOIN user u ON p.author_id = u.id'
        ' WHERE p.id = ?',
        (id,)
    ).fetchone()

    if post is None:
        abort(404, "Post id {0} doesn't exist.".format(id))

    if check_author and post['author_id'] != g.user['id']:
        abort(403)

    return post    
Example #3
0
def get_status(job_name, check_author=True):
    status_sql = """
    select joid, job_name, run_machine, status, status_text, 
    exit_code, status_time, replace(status_timestr, '/', '-') status_timestr,
    last_start, last_end from est_jobst where job_name = ?
    """

    db = get_db()
    cur_job = job_name.upper()

    updated_sql = status_sql.replace("?", "'" + cur_job + "'")
    # jobs = db.execute(
    #     status_sql, (cur_job,)
    # ).fetchall()

    status_df = pd.read_sql(updated_sql, db)

    col_list = status_df.columns

    # got through all column names and create a
    # camelCase version of those column names
    json_col_list = []
    for cur_col in col_list:
        # if we have _, we have tokens to split and capitalize
        if "_" in cur_col:
            cur_tokens = cur_col.split("_")
            final_col = cur_tokens[0].lower()
            for cur_token in cur_tokens[1:]:
                final_col += cur_token.capitalize()
        else:
            final_col = cur_col.lower()

        json_col_list.append(final_col)

    # apply new column names to data fram
    status_df.columns = json_col_list

    # convert results to list of dicts ( so it can be easily made to json )
    results = status_df.to_dict(orient='records')

    return results
Example #4
0
def create():
    if request.method == 'POST':
        title = request.form['title']
        body = request.form['body']
        error = None

        if not title:
            error = 'Title is required.'

        if error is not None:
            flash(error)
        else:
            db = get_db()
            db.execute(
                'INSERT INTO post (title, body, author_id)'
                ' VALUES (?, ?, ?)',
                (title, body, g.user['id'])
            )
            db.commit()
            return redirect(url_for('blog.index'))

    return render_template('blog/create.html')
Example #5
0
def update(id):
    post = get_post(id)

    if request.method == 'POST':
        title = request.form['title']
        body = request.form['body']
        error = None

        if not title:
            error = 'Title is required.'

        if error is not None:
            flash(error)
        else:
            db = get_db()
            db.execute(
                'UPDATE post SET title = ?, body = ?'
                ' WHERE id = ?',
                (title, body, id)
            )
            db.commit()
            return redirect(url_for('blog.index'))

    return render_template('blog/update.html', post=post)   
Example #6
0
def delete(id):
    get_post(id)
    db = get_db()
    db.execute('DELETE FROM post WHERE id = ?', (id,))
    db.commit()
    return redirect(url_for('blog.index'))