def index(): if request.method == 'POST': message = request.form['message'] chatbot_proj_id = 1 _, response_message = wit_ai_understand(message) if update_classroom_db(message, response_message): return redirect(url_for('classroom.index')) db = get_db() posts = db.execute( 'SELECT p.id, message, p.created AS created, author_id, username, r.body AS response, r.voice_filename' ' FROM post p' ' JOIN user u ON p.author_id = u.id' ' JOIN response r ON p.response_id = r.id' ' ORDER BY created DESC').fetchall() proj_name = g.chatbot_proj['proj_name'] proj_asset_url = 'projs' + '/' + proj_name video_folder = os.path.join(current_app.static_folder, 'projs', proj_name, 'responses_videos') has_videos = dict() for post in posts: print(post) print(str(post['voice_filename'])) has_videos[str(post['voice_filename'])] = os.path.isfile( os.path.join(video_folder, str(post['voice_filename']) + '.mp4')) return render_template('classroom/index.html', proj_asset_dir=proj_asset_url, posts=posts, has_videos=has_videos)
def register(role='student'): 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, role) VALUES (?, ?, ?)', (username, generate_password_hash(password), role)) db.commit() return redirect(url_for('auth.login')) flash(error) return render_template('auth/register.html')
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() load_logged_in_user_chatbot_projs()
def get_post(id, check_author=True): post = get_db().execute( 'SELECT p.id, message, 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, "Message id {0} doesn't exist.".format(id)) if check_author and post['author_id'] != g.user['id']: abort(403) return post
def update(id): post = get_post(id) if request.method == 'POST': message = request.form['message'] error = None if not message: error = 'Message is required.' if error is not None: flash(error) else: db = get_db() db.execute('UPDATE post SET message = ?' ' WHERE id = ?', (message, id)) db.commit() return redirect(url_for('classroom.index')) return render_template('classroom/update.html', post=post)
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 update_classroom_db(message, response_message): chatbot_proj_id = 1 response_id = 1 error = None if not message: error = 'The message is required.' if error is not None: flash(error) return False else: m = hashlib.sha224() m.update(response_message.strip().encode("utf-8")) response_message_filename = m.hexdigest() + '.mp3' speech = gTTS(text=response_message, lang='en', slow=False) proj_name = g.chatbot_proj['proj_name'] mp3_fpath = os.path.join(current_app.static_folder, 'projs', proj_name, 'responses_voices', response_message_filename) if not os.path.isfile(mp3_fpath): speech.save(mp3_fpath) # If two people are inserting at the same time, # as long as they are using different cursors, cursor.lastrowid # will return the id for the last row that cursor inserted. db = get_db() cursor = db.cursor() cursor.execute( 'INSERT INTO response (chatbot_proj_id, body, voice_filename)' ' VALUES (?, ?, ?)', (chatbot_proj_id, response_message, response_message_filename)) response_id = cursor.lastrowid cursor.execute( 'INSERT INTO post (chatbot_proj_id, message, author_id, response_id)' ' VALUES (?, ?, ?, ?)', (chatbot_proj_id, message, g.user['id'], response_id)) db.commit() return True
def simple_index(): db = get_db() posts = db.execute( 'SELECT p.id, message, p.created AS created, author_id, username, r.body AS response, r.voice_filename' ' FROM post p' ' JOIN user u ON p.author_id = u.id' ' JOIN response r ON p.response_id = r.id' ' ORDER BY created DESC').fetchall() proj_name = g.chatbot_proj['proj_name'] proj_asset_url = 'projs' + '/' + proj_name video_folder = os.path.join(current_app.static_folder, 'projs', proj_name, 'responses_videos') has_videos = dict() for post in posts: print(post) print(str(post['voice_filename'])) has_videos[str(post['voice_filename'])] = os.path.isfile( os.path.join(video_folder, str(post['voice_filename']) + '.mp4')) return render_template('classroom/simple_index.html', proj_asset_dir=proj_asset_url, posts=posts, has_videos=has_videos)
def load_logged_in_user_chatbot_projs(): # TODO: support switch across chatbot projects for each student g.chatbot_proj = get_db().execute('SELECT * FROM chatbot_proj').fetchone()
def delete(id): get_post(id) db = get_db() db.execute('DELETE FROM post WHERE id = ?', (id, )) db.commit() return redirect(url_for('classform.index'))