def configRoom(id): db = get_db() if request.method == 'POST': print(id) roomId = id description = request.form['description'] width = request.form['width'] deepth = request.form['deepth'] db.execute( "UPDATE room SET description = ?, width = ?, deepth = ? WHERE id = ?;", ( description, width, deepth, roomId, )) db.commit() thisRoom = db.execute("SELECT * FROM room WHERE id = ?;", (id, )).fetchone() if not thisRoom: abort(404, description="404 - Room not found") rooms = db.execute("SELECT * FROM room;").fetchall() microbits = get_db().execute('SELECT * FROM microbit;').fetchall() return render_template('room/config.html', microbits=microbits, thisRoom=thisRoom, rooms=rooms)
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 room(id): room = get_db().execute('SELECT * FROM room WHERE id=?;', (id, )).fetchall() rooms = get_db().execute('SELECT * FROM room;').fetchall() microbits = get_db().execute('SELECT * FROM microbit;').fetchall() if not room: abort(404, description="404 - Room not found") return render_template('monitor/room.html', thisRoom=room, rooms=rooms, microbits=microbits, colors=colors)
def deleteMicroBit(id): db = get_db() oldRoom = db.execute("SELECT room FROM microbit WHERE id = ?;", (id, )).fetchone() db.execute("UPDATE microbit SET room = 'NULL' WHERE id = ?;", (id, )) db.commit() thisRoom = db.execute("SELECT * FROM room WHERE id = ?;", (oldRoom['room'], )).fetchone() rooms = db.execute("SELECT * FROM room;").fetchall() microbits = get_db().execute('SELECT * FROM microbit;').fetchall() return render_template('room/config.html', microbits=microbits, thisRoom=thisRoom, rooms=rooms)
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 newRoom(): db = get_db() if request.method == 'POST': error = None description = request.form['description'] if description is None: error = "Room needs a description" elif description in db.execute( 'SELECT description FROM room;').fetchall(): error = "Description is used for another room" else: width = request.form['width'] deepth = request.form['deepth'] db.execute( "INSERT INTO room (description, width, deepth) VALUES (?,?,?);", ( description, width, deepth, )) db.commit() newroom = db.execute('SELECT id FROM room WHERE description = ?;', (description, )).fetchone() return redirect(url_for('monitor.room', id=newroom['id'])) flash(error) rooms = db.execute("SELECT * FROM room;").fetchall() return render_template('room/new.html', rooms=rooms)
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()
def addMicroBit(id): db = get_db() microbit = request.form.get('microbit') db.execute("UPDATE microbit SET room = ? WHERE id = ?", ( id, microbit, )) db.commit() thisRoom = db.execute("SELECT * FROM room WHERE id = ?;", (id, )).fetchone() rooms = db.execute("SELECT * FROM room;").fetchall() microbits = get_db().execute('SELECT * FROM microbit;').fetchall() return render_template('room/config.html', microbits=microbits, thisRoom=thisRoom, rooms=rooms)
def configMicroBit(id): db = get_db() if request.method == 'POST': name = request.form['description'] x = request.form['xpos'] y = request.form['ypos'] minTemp = request.form['min-temp'] maxTemp = request.form['max-temp'] minLight = request.form['min-light'] maxLight = request.form['max-light'] mail = request.form['email'] db.execute( '''UPDATE microbit SET name = ?, position_x = ?, position_y = ?, min_temp = ?, max_temp = ?, min_light = ?, max_light = ?, mail = ? WHERE id = ?''', ( name, x, y, minTemp, maxTemp, minLight, maxLight, mail, id, )) db.commit() thisMb = db.execute("SELECT * FROM microbit WHERE id = ?;", (id, )).fetchone() if not thisMb: abort(404, description="404 - Micro:Bit not found") rooms = db.execute("SELECT * FROM room;").fetchall() microbits = get_db().execute('SELECT * FROM microbit;').fetchall() return render_template('microbit/config.html', microbits=microbits, thisMb=thisMb, rooms=rooms)
def test_create(client, auth, app): with app.app_context(): db = get_db() precount = db.execute('SELECT COUNT(id) FROM room').fetchone()[0] auth.login() assert client.get('/room/new').status_code == 200 client.post('/room/new', data={ 'description': 'created', 'width': '1', 'deepth': '1' }) with app.app_context(): db = get_db() count = db.execute('SELECT COUNT(id) FROM room').fetchone()[0] assert (precount + 1) == count
def index(): db = get_db() microbits = db.execute('SELECT * FROM microbit;').fetchall() rooms = db.execute('SELECT * FROM room;').fetchall() print() return render_template('monitor/index.html', microbits=microbits, rooms=rooms, colors=colors)
def test_delete(client, auth, app): auth.login() response = client.post('/3/delete') assert response.headers['Location'] == 'http://localhost/' with app.app_context(): db = get_db() post = db.execute('SELECT * FROM room WHERE id = 3').fetchone() assert post is None
def test_register(client, app, auth): auth.login() assert client.get('/auth/register').status_code == 200 response = client.post('/auth/register', data={ 'username': '******', 'password': '******' }) assert 'http://localhost/auth/users' == response.headers['Location'] with app.app_context(): assert get_db().execute( "SELECT * FROM user WHERE username = '******'", ).fetchone() is not None
def test_update(client, auth, app): auth.login() assert client.get('/room/2').status_code == 200 client.post('/room/2', data={ 'description': 'updated', 'width': '5', 'deepth': '6' }) with app.app_context(): db = get_db() post = db.execute('SELECT * FROM room WHERE id = 3').fetchone() assert post['description'] == 'updated' assert post['width'] == '5' assert post['deepth'] == '6'
def delete(id): db = get_db() error = None if request.method == 'POST': if not (db.execute("SELECT id FROM user WHERE id = ?;", (id, )).fetchone()): error = "No user with this id" elif (len(db.execute("SELECT id FROM user;").fetchall()) <= 1): error = "You can not remove the last user" else: db.execute('DELETE FROM user WHERE id = ?', (id, )) db.commit() return redirect(url_for('auth.users')) flash(error) users = db.execute("SELECT id, username FROM user;").fetchall() return render_template('auth/users.html', users=users)
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')
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.users')) flash(error) return render_template('auth/register.html')
def edit(id): db = get_db() user = db.execute("SELECT id, username FROM user WHERE id = ?;", (id, )).fetchone() if not user: abort(404, description="404 - User not found") if request.method == 'POST': password = request.form['password'] error = None if not password: error = 'Password is required.' if error is None: db.execute('UPDATE user set password = ? WHERE id = ?', ( generate_password_hash(password), id, )) db.commit() return redirect(url_for('auth.users')) flash(error) return render_template('auth/edit.html', user=user)
def events(): db = get_db() events = db.execute("SELECT * FROM warning_event ORDER BY date DESC;") return render_template('monitor/events.html', events=events)
def history(): db = get_db() history = db.execute("SELECT * FROM history ORDER BY date DESC;") return render_template('monitor/history.html', history=history)
def users(): db = get_db() users = db.execute("SELECT id, username FROM user;").fetchall() return render_template('auth/users.html', users=users)
def deleteRoom(id): db = get_db() db.execute("DELETE FROM room WHERE id = ?;", (id, )) db.commit() return redirect(url_for('monitor.index'))