Exemple #1
0
def create():
    if request.method == 'POST':
        f = request.files['file']

        if f and f.filename.rsplit('.', 1)[1] == 'docx':
            filename = secure_filename(f.filename)

            fullpath = os.path.join(current_app.config['UPLOAD_FOLDER'],
                                    filename)
            f.save(fullpath)

            db = get_db()
            db.execute(
                'insert into entries(title, body, author_id) values(?, ?, ?)',
                [filename, filename, g.user['id']])
            docID = db.execute('select max(id) from entries where title=?',
                               [filename]).fetchone()['max(id)']
            db.commit()

            #print(docID, file=sys.stderr)
            word_to_elastic(fullpath, docID)
            os.remove(fullpath)

            flash('New entry was successfully posted')

        else:
            flash('Could not post file, only .docx allowed')

        return redirect(url_for('blog.index'))

    return render_template('blog/create.html')
Exemple #2
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()
Exemple #3
0
def delete(id):
    db = get_db()
    db.execute('DELETE FROM entries WHERE id = ?', (id, ))
    db.commit()

    delete_from_index(id)

    flash('Entry was successfully deleted')

    return redirect(url_for('blog.index'))
Exemple #4
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.'

        if error is None:
            session.clear()
            session['user_id'] = user['id']
            return redirect(url_for('index'))

        flash(error)

    return render_template('auth/login.html')
Exemple #5
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.'
        elif db.execute('SELECT id FROM user WHERE username = ?',
                        (username, )).fetchone() is not None:
            error = 'User {} is already registered.'.format(username)

        if error is None:
            db.execute('INSERT INTO user (username, password) VALUES (?, ?)',
                       (username, generate_password_hash(password)))
            db.commit()
            return redirect(url_for('auth.login'))

        flash(error)

    return render_template('auth/register.html')
Exemple #6
0
def index():
    db = get_db()
    entries = db.execute('select id, title, body'
                         ' FROM entries order by id').fetchall()
    return render_template('blog/index.html', entries=entries)