def update_users_object(): subscribers = database.child( "groups/{}/mensalistas".format(STACK_OVERGOL_ID)).get().val() users = database.child("users").get().val().values() new_users_format = {} for user in users: try: rating = user["groups"][STACK_OVERGOL_ID]["stars"] except: rating = 3.0 new_user = { "first_name": user["first_name"], "id": user["id"], "is_admin": False, "is_subscriber": str(user["id"]) in subscribers, "last_name": user["last_name"], "rating": rating, "strikes": 0 } new_users_format[new_user["id"]] = new_user database.child("refactor/users").set(new_users_format)
def update(key, payload): user = self.get_user(payload) if user: update_user(user, payload) update_listitem(user) database.child("updates").child(key).remove()
def update_user(user, payload): if "rating" in payload: user.rating = payload["rating"] if "is_admin" in payload: user.is_admin = payload["is_admin"] if "is_subscriber" in payload: user.is_subscriber = payload["is_subscriber"] database.child("users").child(user.uid).set(user.serialize())
def update_listitem(user): listitem = self.get_user_list_item(user) if listitem: listitem.user = user self.list.sort() database.child("list").set( {item.user.uid: item.serialize() for item in self.list})
def updates_handler(bot, job): updates = database.child("updates").get().val() if not updates: return for key, changes in updates.items(): update(key, changes["payload"])
def save(self): database.child("teams").set(self.teams.serialize()) database.child("check_in_open").set(self.check_in_open) database.child("list").set( {item.user.uid: item.serialize() for item in self.list}) database.child("users").set( {user.uid: user.serialize() for user in self.all_users}) self.should_sync = False
def get_missing_ratings_from_current_list(group_id, show_id=False): lista = database.child( "groups/{}/lista".format(group_id)).get().val().values() lista = sorted(lista, key=itemgetter("first_name", "last_name")) for user in lista: id = user["id"] first_name = user["first_name"].upper() last_name = user["last_name"].upper() rating = database.child("users/{}/groups/{}/stars".format( id, group_id)).get().val() if (rating is None): if show_id: output = "{:10} - ".format(id) else: output = "" output += ",{} {},,,{}".format(first_name, last_name, user["id"]) print(output)
def load(self): self.check_in_open = database.child("check_in_open").get().val() self.list = [] try: for item in database.child("list").get().each(): values = item.val() user_values = values.pop("user") uid = user_values.pop("uid") first_name = user_values.pop("first_name") last_name = user_values.pop("last_name") user = User(uid, first_name, last_name, **user_values) listitem = ListItem( user, hide_guest_label=Config.racha_hide_guest_label(), hide_subscriber_label=Config.racha_hide_subscriber_label(), **values) self.list.append(listitem) self.list.sort() except Exception as error: logger.error(error) # TODO: load teams try: self.all_users = [ User(**user.val()) for user in database.child("users").get().each() ] except Exception as error: logger.error(error)
def main(): with open(RATINGS_FILE_NAME, newline='') as ratingsfile: ratingsreader = csv.reader(ratingsfile, delimiter=',', quotechar='|') for i, row in enumerate(ratingsreader): if not i or not row[0]: continue try: value = int(row[5]) except: value = 0 user = database.child("users/{}".format(row[0])).get().val() if not user: print('creating user: {} {}'.format(row[1], row[2])) database.child("users/{}".format(row[0])).set({ "first_name": row[1], "last_name": row[2], "rating": float(row[3]) or 3, "is_admin": row[4] == '1', "is_subscriber": row[5] == '1', "uid": int(row[0]) }) else: print('updating user: {} {}'.format(row[1], row[2])) database.child("users/{}".format(row[0])).update({ "rating": float(row[3]) or 3, "is_admin": row[5] == '1', "is_subscriber": row[5] == '1' }) for i, prop in enumerate([ "uid", "first_name", "last_name", "rating", "is_admin", "is_subscriber" ]): if user[prop] != row[i]: print('chaing {} from {} to {}'.format( prop, user[prop], row[i])) user = database.child("list/{}".format(row[0])).get().val() if user: print('updating list item: {} {}'.format(row[1], row[2])) database.child("list/{}/user".format(row[0])).update({ "rating": float(row[3]) or 3, "is_admin": row[4] == '1', "is_subscriber": row[5] == '1' })