async def update_db(self, ctx): with open("merlin-latest", "r") as f: data = f.readlines() new_people = [] new_logins = [] for line in data: line = line.split(":") login = line[0] name = line[4].split(",", 1)[0] try: year = line[4].split(",")[1] except IndexError: continue new_people.append(Valid_person(login=login, year=year, name=name)) new_logins.append(login) for person in new_people: session.merge(person) for person in session.query(Valid_person): if person.login not in new_logins: try: # check for muni int(person.login) person.year = "MUNI" except ValueError: person.year = "dropout" session.commit() await ctx.send("Update databaze probehl uspesne")
async def update_db(self, ctx, convert_0xit: bool = False): with open("merlin-latest", "r") as f: data = f.readlines() found_people = [] found_logins = [] new_logins = [] login_results = session.query(Valid_person.login).all() # Use unpacking on results old_logins = [value for value, in login_results] for line in data: line = line.split(":") login = line[0] name = line[4].split(",", 1)[0] try: year = line[4].split(",")[1] except IndexError: continue if convert_0xit and year.endswith("1r"): person = session.query(Valid_person).\ filter(Valid_person.login == login).\ one_or_none() if person is not None and person.year.endswith("0r"): year = year.replace("1r", "0r") found_people.append(Valid_person(login=login, year=year, name=name)) found_logins.append(login) for login in found_logins: if login not in old_logins: new_logins.append(login) await ctx.send(f"Našel jsem {len(new_logins)} nových loginů.") for person in found_people: session.merge(person) cnt_new = 0 for person in session.query(Valid_person): if person.login not in found_logins: try: # check for muni int(person.login) person.year = "MUNI" except ValueError: person.year = "dropout" elif convert_0xit and person.login in new_logins: if person.year.endswith("1r"): person.year = person.year.replace("1r", "0r") cnt_new += 1 session.commit() await ctx.send("Aktualizace databáze proběhla úspěšně.") if convert_0xit: await ctx.send(f"Debug: Našel jsem {cnt_new} nových prvaků.")
async def update_db(self, ctx): if ctx.author.id != config.admin_id: await ctx.send( messages.insufficient_rights.format( user=utils.generate_mention(ctx.author.id))) return with open("merlin-latest", "r") as f: data = f.readlines() new_people = [] new_logins = [] for line in data: line = line.split(":") login = line[0] name = line[4].split(",", 1)[0] try: year = line[4].split(",")[1] except IndexError: continue new_people.append(Valid_person(login=login, year=year, name=name)) new_logins.append(login) for person in new_people: session.merge(person) for person in session.query(Valid_person): if person.login not in new_logins: try: # check for muni int(person.login) print("Muni pls") person.year = "MUNI" except ValueError: person.year = "dropout" session.commit() await ctx.send("Update databaze probehl uspesne")
def add_user(self, login: str, year: str, status: int = 1): """Add user to database""" session.add(Valid_person(login=login, year=year, status=status)) session.commit()
def load_dump(filename: str): init_db(False) session.query(Karma).delete() session.query(Karma_emoji).delete() session.query(Permit).delete() session.query(Valid_person).delete() session.commit() print(f'Loading dump from {filename}') data = database.base.metadata.tables.keys() for row in data: print(row) with open(filename, "r", encoding='utf-8') as backup_file: data = backup_file.readlines() inserts = [line for line in data if line.startswith("INSERT")] karma_values = [] for insert in inserts: values = insert.split("VALUES", 1)[1] if insert.startswith("INSERT INTO `bot_karma`"): values = values[1:-2].replace('\'', '') values = values.replace('(', '').replace(')', '') values = values.split(',') for i in range(0, len(values), 3): karma_values.append( Karma(member_ID=values[i], karma=values[i + 1])) elif insert.startswith("INSERT INTO `bot_karma_giving`"): values = values[1:-2].replace('\'', '') values = values.replace('(', '').replace(')', '') values = values.split(',') for i in range(0, len(values), 4): karma_values.append( Karma(member_ID=values[i], positive=values[i + 1], negative=values[i + 2])) elif insert.startswith("INSERT INTO `bot_karma_emoji`"): values = values[1:-2].replace('\'', '') values = values.replace('(', '').replace(')', '') values = values.split(',') for i in range(0, len(values), 2): session.add( Karma_emoji(emoji_ID=values[i], value=values[i + 1])) elif insert.startswith("INSERT INTO `bot_permit`"): values = values[1:-2] values = values.replace('(', '').replace(')', '') values = re.split(r',(?=\')', values) values = [value.replace('\'', '') for value in values] for i in range(0, len(values), 3): session.add(Permit(login=values[i], discord_ID=values[i + 2])) elif insert.startswith("INSERT INTO `bot_valid_persons`"): values = values[1:-2].replace('\'', '') values = values.replace('(', '').replace(')', '') values = values.split(',') for i in range(0, len(values), 5): session.add( Valid_person( login=values[i], name=values[i + 1], year=values[i + 2], code=values[i + 3] if values[i + 3] != "NULL" else None, status=values[i + 4])) for karma in karma_values: session.merge(karma) session.commit()
def add_user(self, login: str, year: str, status: int = 1) -> Valid_person: """Add user to database""" person = Valid_person(login=login, year=year, status=status) session.add(person) session.commit() return person