def send_last_word(): userId = request.json["userId"] lastWord = request.json["lastWord"] contact = request.json["contact"] end_time = filter.get_pair_end_time(userId) pair = filter.get_pair(userId) if status.is_send_last_message(userId) == False: try: if pair.playerA == userId: pair.playerA_lastedAt = datetime.now() else: pair.playerB_lastedAt = datetime.now() except BaseException as err: db_session.rollback() logging.error("SQL is rollback. error is: {}", err) raise err else: db_session.commit() reply.last_message(userId, end_time.hour, end_time.minute, lastWord, contact) return response(msg="Send palyer's last word.", payload={"status": "success"}, code=200)
def pair_user(placeId, userId): pool = filter.get_active_pool(userId) pair = filter.get_active_pair(userId) if status.is_place(placeId) == False: return response(msg="Wrong place ID.", payload={"status": "noPair"}, code=200) if status.is_noPair(userId): try: db_session.add(Pool(placeId=placeId, userId=userId)) except BaseException as err: db_session.rollback() logging.error("SQL is rollback. error is: {}", err) raise err else: db_session.commit() reply.pairing(userId) message.push_customer_menu(userId, Context.menu_waiting_cancel) return response(msg="User start to pair.", payload={"status": "pairing"}, code=200) elif status.is_pairing(userId): reply.pairing(userId) return response(msg="User is exist and pairing.", payload={"status": "pairing"}, code=200) else: return response(msg="User is chatting.", payload={"status": "paired"}, code=200) return "success"
def leave(userId): message.delete_menu(userId) try: if status.is_pairing(userId): data = filter.get_active_pool(userId) placeId = data.placeId words = Context.waiting_leave elif status.is_paired(userId): data = filter.get_active_pair(userId) placeId = data.placeId data.status = status_Enum(1) recipient_id = filter.get_recipient_id(userId) message.delete_menu(recipient_id) reply.quick_pair(recipient_id, placeId, Context.partner_leave_message) words = Context.leave_message else: return reply.general_pair(userId, Context.no_pair_message) data.deletedAt = datetime.now() except BaseException as err: db_session.rollback() logging.error("SQL is rollback. error is: {}", err) raise err else: db_session.commit() return reply.quick_pair(userId, placeId, words)
def seed(): init_db() place_count = Place.query.filter().count() if place_count == 0: db_session.add( Place(id="1111", name="木木卡的黑店", longitude="25.066765", latitude="121.526336")) db_session.commit()
def register(): """ View function to register new user """ try: user = user_schema.load(request.get_json()) db_session.add(user) db_session.commit() return (json.dumps(user_schema.dump(user)), 200, { 'content-type': 'application/json' }) except ValidationError as err: return make_response(jsonify({'message': err.messages}), 422)
def timeout(userId): pair = filter.get_active_pair(userId) pool = filter.get_active_pool(userId) if status.is_pairing(userId): time_diff = expired_time(Config.EXPIRED_TIME) if time_diff >= pool.createdAt: try: pool.deletedAt = datetime.now() except BaseException as err: db_session.rollback() logging.error("SQL is rollback. error is: {}", err) raise err else: db_session.commit() message.delete_menu(userId) reply.quick_pair(userId, pool.placeId, Context.wait_expired) else: return response(msg="User is pairing", payload={"status": "pairing"}, code=200) else: time_diff = expired_time(Config.END_TIME) if time_diff >= pair.createdAt: try: pair.deletedAt = datetime.now() pair.status = status_Enum(2) except BaseException as err: db_session.rollback() logging.error("SQL is rollback. error is: {}", err) raise err else: db_session.commit() recipient_id = filter.get_recipient_id(userId) reply.timeout_message(userId) reply.timeout_message(recipient_id) else: return response(msg="User is paired", payload={"status": "paired"}, code=200) return response(msg="Timeout to breaked pair", payload={"status": "Broken"}, code=200)
def user_colors(user_id): """ View function to retrieve colors by user. """ if request.method == 'GET': colors = db_session.query(Color).filter_by(user_id=user_id).all() return (json.dumps(colors_schema.dump(colors)), 200, {'content_type': 'application/json'}) try: data = request.get_json() data['user_id'] = user_id color = color_schema.load(data) db_session.add(color) db_session.commit() return (json.dumps(color_schema.dump(color)), 200, {'content-type': 'application/json'}) except ValidationError as err: return make_response(jsonify({'message': err.messages}), 422)
def user_moods(user_id): """ View function to retrieve moods by user. """ if request.method == 'GET': moods = db_session.query(Mood).filter_by(user_id=user_id).all() return (json.dumps(moods_schema.dump(moods)), 200, { 'content_type': 'application/json' }) try: data = request.get_json() color = db_session.query(Color).get(data['color_id']) data['user_id'] = user_id data['color_hex'] = color.hex_code mood = mood_schema.load(data) db_session.add(mood) db_session.commit() return (json.dumps(mood_schema.dump(mood)), 200, { 'content-type': 'application/json' }) except ValidationError as err: return make_response(jsonify({'message': err.messages}), 422)