コード例 #1
0
def register():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        db = get_db()
        error = None

        if not username:
            error = 'Username is required'
        elif not password:
            error = 'Password is required'

        #check to make sure there is not already a user in the database
        #before we create a new user with this username
        elif db.execute('SELECT id FROM user WHERE username = ?',
                        (username, )).fetchone() is not None:
            error = 'User {} is already registered.'.format(username)

        #if there isn't a user of this same name, add the username and password
        #hash to our database
        if error is None:
            db.execute('INSERT INTO user (username, password) VALUES (?, ?)',
                       (username, generate_password_hash(password)))
            #always call commit after data is modified
            db.commit()

            #url_for generates the URL for the login view based on the name
            return redirect(url_for('auth.login'))

        #flash stores messages that can be retrieved when rendering the template
        flash(error)

    #if the method is not a POST request (i.e. it is a GET request) render the register template
    return render_template('auth/register.html')
コード例 #2
0
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        db = get_db()
        error = None
        user = db.execute('SELECT * FROM user WHERE username = ?',
                          (username, )).fetchone()

        if user is None:
            error = 'Incorrect username'
        elif not check_password_hash(user['password'], password):
            error = 'Incorrect password'

        #session is a dictionary that stores data across requests
        #when validation succeeds, the users ID is stored in the session
        #the data is stored in a cookie that is sent to the browser, and the
        #browser then sends it back with subsequent requets. Flask securely signs
        #the data so that it can't be tampered with (i.e. fake a cookie)
        if error is None:
            session.clear()
            session['user_id'] = user['id']
            return redirect(url_for('index'))

        flash(error)

    return render_template('auth/login.html')
コード例 #3
0
def load_logged_in_user():
    user_id = session.get('user_id')

    if user_id is None:
        g.user = None
    else:
        g.user = get_db().execute('SELECT * FROM user WHERE id = ?',
                                  (user_id, )).fetchone()
コード例 #4
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)
コード例 #5
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))

    #404 not found, 403 forbidden, 401 unauthorized
    if check_author and post['author_id'] != g.user['id']:
        abort(403)

    return post
コード例 #6
0
def search_coch():
    if request.method == "POST":
        article = request.form["article"]

        if len(article) > 0:
            database = db.get_db()

            sql = "SELECT coch, accuracy FROM wiki_coch " +\
                  "WHERE wiki='" + article + "'" +\
                  "ORDER BY accuracy DESC;"
            result = database.execute(sql).fetchall()

            if len(result) > 0:
                review = [row[0] for row in result]
                similarity = [row[1] for row in result]
                similarity = ["Missing" if item == None else item \
                              for item in similarity]
                return render_template("wiki_to_coch2.html",
                                       article = article,
                                       result = zip(review, similarity))
            else:
                sql = "SELECT DISTINCT wiki FROM wiki_coch " +\
                      "WHERE wiki LIKE '%" + article + "%';"
                result = database.execute(sql).fetchall()

                if len(result) > 0:
                    article_list = [row[0] for row in result]
                    return render_template("wiki_to_coch3.html",
                                           article_list = article_list)
                else:
                    instruction = ["No Similar Wikipedia article found.",
                                   "Please input a new one."]
                    return render_template("wiki_to_coch.html",
                                           instruction = instruction)
        else:
            instruction = ["No input found.",
                           "Please input a Wikipedia article."]
            return render_template("wiki_to_coch.html",
                                   instruction = instruction)
コード例 #7
0
def search_wiki():
    if request.method == "POST":
        review = request.form["review"]

        if len(review) > 0:
            database = db.get_db()

            sql = "SELECT wiki, accuracy FROM coch_wiki " +\
                  "WHERE coch ='" + review + "'" +\
                  "ORDER BY accuracy DESC;"
            result = database.execute(sql).fetchall()

            if len(result) > 0: # At least one row is returned
                article = [row[0] for row in result]
                similarity = [row[1] for row in result]
                similarity = ["missing" if item == None else item \
                              for item in similarity]
                return render_template("coch_to_wiki2.html",
                                       review = review,
                                       result = zip(article, similarity))
            else:
                sql = "SELECT DISTINCT coch FROM coch_wiki " +\
                      "WHERE coch LIKE '%" + review + "%';"
                result = database.execute(sql).fetchall()

                if len(result) > 0:
                    review_list = [row[0] for row in result]
                    return render_template("coch_to_wiki3.html",
                                           review_list = review_list)
                else:
                    instruction = ["No Similar Cochrane Reviews found.",
                                   "Please input a new one."]
                    return render_template("coch_to_wiki.html",
                                           instruction = instruction)
        else:
            instruction = ["No input found.",
                           "Please input a Cochrane review."]
            return render_template("coch_to_wiki.html",
                                   instruction = instruction)
コード例 #8
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')
コード例 #9
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)
コード例 #10
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'))