def get_all_owners(badge: str, cur: MySQLdb.cursors.DictCursor) -> Set[int]: '''Returns a set of ids of users who should receive the badge''' with open(os.path.join(BADGES_PATH, badge, 'query.sql')) as fd: query = fd.read() cur.execute(query) results = set() for row in cur: results.add(row['user_id']) return results
def get_all_owners(badge: str, current_timestamp: Optional[datetime.datetime], cur: MySQLdb.cursors.DictCursor) -> Set[int]: '''Returns a set of ids of users who should receive the badge''' with open(os.path.join(BADGES_PATH, badge, 'query.sql')) as fd: query = fd.read() if current_timestamp is not None: query = query.replace( 'NOW()', f"'{current_timestamp.strftime('%Y-%m-%d %H:%M:%S')}'") cur.execute(query) results = set() for row in cur: results.add(row['user_id']) return results
def get_current_owners(badge: str, cur: MySQLdb.cursors.DictCursor) -> Set[int]: '''Returns a set of ids of current badge owners''' cur.execute(''' SELECT ub.user_id FROM Users_Badges ub WHERE ub.badge_alias = '%s';''' % badge) results = set() for row in cur: results.add(row['user_id']) return results
def save_new_owners(badge: str, users: Set[int], cur: MySQLdb.cursors.DictCursor) -> None: '''Adds new badge owners entries to Users_Badges table''' badges_tuples = [] notifications_tuples = [] for user in users: badges_tuples.append((user, badge)) notifications_tuples.append(( user, json.dumps({'type': 'badge', 'badge': badge}))) cur.executemany(''' INSERT INTO Users_Badges (user_id, badge_alias) VALUES (%s, %s);''', badges_tuples) cur.executemany(''' INSERT INTO Notifications (user_id, contents) VALUES (%s, %s)''', notifications_tuples)