def check_login(token): if token == 'none': return False session = Session() check = session.query(exists().where(TokenTable.token==token)) session.close() return check
def verify_user(token): session = Session() user_id = session.query(TokenTable).filter_by(token=token).one_or_none() session.close() if user_id is None: return False else: return int(user_id.user_id)
def music_list(): session = Session() songs = session.query(Musics).all() session.close() result = [] for song in songs: result.append([song.id, song.song_title, song.singer]) return result
def create_eventnames(): session = Session() names = session.query(Eventnames).all() for func in functions: if func not in names: session.add(Eventnames(event_name=func)) session.commit() session.close()
def isExist(name, singer): session = Session() song = session.query(Musics).filter_by(song_title=name, singer=singer).all() session.close() if len(song): return song[0].id else: return 0
def ws_lag(): session = Session() event_id_ws_sing = session.query(Eventnames).filter_by( event_name='ws_sing').one().id - 1 lag_list = session.query(Eventlogs).filter_by( event_id=event_id_ws_sing).filter_by(user_id=0).all() session.close() lag = statistics.median([int(l.log_message) for l in lag_list]) return lag
def new_token(user_id): session = Session() token = secrets.token_hex() session.add(TokenTable( token=token, user_id=user_id )) session.commit() session.close() return token
def add_music(name, singer): session = Session() song_id = session.query(Musics).count() + 1 session.add( Musics( song_title=name, singer=singer, created_at=datetime.now().isoformat(' ', 'seconds'), )) session.commit() session.close() return song_id
def logged_in(user_id): if user_id < 2: return 0 session = Session() result = session.query(Eventlogs).filter_by(user_id=user_id).all() event_id_logout = session.query(Eventnames).filter_by( event_name='logout').one().id - 1 session.close() if len(result) == 0 or result[-1].event_id == event_id_logout: return 0 else: return 1
def logged_in(user_id): # TODO: write here for auto login if user_id == -1: return 0 session = Session() result = session.query(Eventlogs).filter_by(user_id=user_id).all() event_name_logout = session.query(Eventnames).filter_by( event_name='logout').one().id session.close() if len(result) == 0 or result[-1].event_id == event_name_logout: return 0 else: return 1
def login(data): session = Session() user = session.query(Users).filter_by(user_name=data['user_name']).all() session.close() user_id = -1 password = hashlib.sha256(data['user_password'].encode()).hexdigest() if len(user) == 1: if user[0].user_password == password: msg = 'success' user_id = user[0].id else: msg = 'wrong password' else: msg = 'wrong username' return {'isFound': len(user), 'user_id': user_id, 'msg': msg}
def music_list(): session = Session() songs = session.query(Musics).all() event_id_ws_sing = session.query(Eventnames).filter_by( event_name='ws_sing').one().id - 1 count = session.query(Eventlogs).filter_by(event_id=event_id_ws_sing).all() session.close() song_dict = collections.defaultdict(int) for i in range(len(count)): song_dict[int(count[i].push)] += 1 result = sorted( [[song.id, song.song_title, song.singer, song_dict[int(song.id)]] for song in songs], key=lambda x: x[3], reverse=True) return result
def init_db(): session = Session() session.add( Users( user_name='master', user_password='******', created_at=datetime.now().isoformat(' ', 'seconds'), )) session.add( Musics( song_title='wonder stella', singer='fhana', created_at=datetime.now().isoformat(' ', 'seconds'), )) session.commit() session.close()
def signup(data): name = data['user_name'] user_id = -1 session = Session() user = session.query(Users).filter_by(user_name=name).all() if len(user) == 0: user_id = session.query(Users).count() + 1 session.add( Users(user_name=name, user_password=hashlib.sha256( data['user_password'].encode()).hexdigest(), created_at=datetime.now().isoformat(' ', 'seconds'))) session.commit() msg = 'succeeded to create an user account' else: msg = 'already exists' session.close() return {'user_id': user_id, 'msg': msg}
def init_db(): session = Session() create_eventnames() session.add(Users( user_name='master', user_password='******', created_at=datetime.now().isoformat(' ', 'seconds'), )) session.add(Musics( song_title='wonder stella', singer='fhana', created_at=datetime.now().isoformat(' ', 'seconds'), )) logger.info('{0}@_@{1} {2} {3} {4}'.format( 0, 9, 0, 1, 0 )) session.commit() session.close()
def isExist(name, singer): session = Session() song = session.query(Musics).filter_by(song_title=name, singer=singer).all() session.close() return len(song) != 0
def finish_upload(song_id): session = Session() res = session.query(Hsh).filter_by(song_id=song_id).all() session.close() return len(res)
def username(user_id): session = Session() name = session.query(Users).filter_by(id=user_id).first() session.close() return name.user_name
def upload_hash(song_id, h, t, n): # TODO: format table? session = Session() session.add(Hsh(song_id=song_id, hsh_data=h, ptime=t, noise=n)) session.commit() session.close()
def hashtable(song_id): session = Session() hsh = session.query(Hsh).filter_by(song_id=song_id).one() session.close() return hsh.hsh_data, hsh.ptime, hsh.noise