def test_get_close_db(app): with app.app_context(): db = get_db() assert db is get_db() with pytest.raises(sqlite3.ProgrammingError) as e: db.execute('SELECT 1') assert 'closed' in str(e.value)
def update(id): title = request.form.get('title') body = request.form.get('body') error = None if not title: error = 'Title is required' if not body: error = 'Body is required' if error is not None: return {"success": False, "error": error} db = get_db() db.execute('UPDATE post SET title = ?, body = ?' ' WHERE id = ?', (title, body, id)) db.commit() return { "success": True, "data": { "user_id": g.user[0], "title": title, "body": body } }
def add(): title = request.form.get('title') body = request.form.get('body') error = None if not title: error = 'Title is required' if not body: error = 'Body is required' if error is not None: return {"success": False, "error": error} db = get_db() db.execute('INSERT INTO post (title, body, author_id)' ' VALUES(?, ?, ?)', (title, body, g.user[0])) db.commit() return { "success": True, "data": { "user_id": g.user[0], "title": title, "body": body } }
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 {'success': True, 'posts': posts}
def app(): db_fd, db_path = tempfile.mkstemp() app = create_app({ 'TESTING': True, 'DATABASE': db_path, }) with app.app_context(): init_db() get_db().executescript(_data_sql) yield app os.close(db_fd) os.unlink(db_path)
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[2], password): error = 'Incorrect password.' if error is None: session.clear() session['user_id'] = user[0] return { 'success': True, 'data': { 'user_id': user[0], 'username': user[1] } } return { 'success': False, 'error': error }
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 ({ 'success': True, 'data': { 'username': username } }) return ({ 'success': False, 'error': error }, 400)
def load_logged_in_user(): user_id = session.get('user_id') if user_id is None: g.user = None g.user = get_db().execute( 'SELECT * FROM user WHERE id = ?', (user_id,) ).fetchone()
def get_post(id): 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)) return {"success": True, "data": post}
def delete(id): db = get_db() db.execute('DELETE FROM post WHERE id = ? ', (id, )) db.commit() return {"success": True, "data": {"id": id}}