def test_create_karma_user(mock_empty_db_session, mock_slack_api_call): karma = Karma("ABC123", "XYZ123", "CHANNEL42") assert karma.giver.username == "pybob" assert karma.receiver.username == "clamytoe" first = db_session.create_session().query(KarmaUser).get("ABC123") second = db_session.create_session().query(KarmaUser).get("XYZ123") assert first.username == "pybob" assert second.username == "clamytoe"
def update_username(**kwargs): """Changes the Username""" user_id = kwargs.get("user_id").strip("<>@") session = create_session() karma_user: KarmaUser = session.query(KarmaUser).get(user_id) if not karma_user: return "User not found" old_username = karma_user.username user_info = bot.SLACK_CLIENT.api_call("users.info", user=user_id) new_username = bot.slack.get_available_username(user_info) if old_username == new_username: return ( f"Sorry, you have not updated your username: {old_username}. \n" "Please update your real-name or display-name in your Slack " "profile and retry." ) karma_user.username = new_username session.commit() session.close() return ( f"Sucessfully updated your KarmaUser name " f"from '{old_username}' to '{new_username}'!" )
def test_change_karma(mock_filled_db_session, test_changes, mock_slack_api_call): session = db_session.create_session() pre_change_karma = session.query(KarmaUser).get(test_changes[1]).karma_points karma = Karma(test_changes[0], test_changes[1], test_changes[2]) karma.change_karma(test_changes[3]) post_change = session.query(KarmaUser).get(test_changes[1]).karma_points assert post_change == (pre_change_karma + test_changes[3]) session.close()
def test_change_karma(mock_filled_db_session, test_changes): session = db_session.create_session() pre_change_karma = session.query(KarmaUser).get( test_changes[1]).karma_points karma = Karma(test_changes[0], test_changes[1]) karma.change_karma(test_changes[2]) session.commit() post_change = session.query(KarmaUser).get(test_changes[1]).karma_points assert post_change == (pre_change_karma + test_changes[2])
def get_user_name(**kwargs): """Shows the current Username""" user_id = kwargs.get("user_id").strip("<>@") session = create_session() karma_user: KarmaUser = session.query(KarmaUser).get(user_id) if not karma_user: return "Sorry, you are not yet known to karmabot. Try to give some Karma! :)" username = karma_user.username session.close() return f"Your current username for karmabot is '{username}'"
def top_karma(**kwargs): """Get the PyBites members with most karma""" output = ["PyBites members with most karma:"] session = db_session.create_session() top_users = (session.query(KarmaUser).order_by( KarmaUser.karma_points.desc()).limit(TOP_NUMBER)) try: for top_user in top_users: output.append("{:<20} -> {}".format(top_user.username, top_user.karma_points)) ret = "\n".join(output) return "```{}```".format(ret) finally: session.close()
def upload_karma_to_db(new_karma): session = db_session.create_session() session.execute("""TRUNCATE TABLE karma_user""") session.commit() new_users = [ KarmaUser( user_id=user["id"], username=user["username"], karma_points=user["karma_points"], ) for user in new_karma.values() ] print(f"Inserting {len(new_users)} users in DB") session.bulk_save_objects(new_users) session.commit() session.close()
def get_karma(**kwargs): """Get your current karma score""" user_id = kwargs.get("user_id").strip("<>@") session = db_session.create_session() kama_user = session.query(KarmaUser).get(user_id) try: if not kama_user: return "User not found" if kama_user.karma_points == 0: return "Sorry, you don't have any karma yet" return ( f"Hey {kama_user.username}, your current karma is {kama_user.karma_points}" ) finally: session.close()