fix: add session closing

main
Khiem Ton 2 years ago
parent a64cd12e9f
commit 0d7b4a55d2
Signed by: th4tkh13m
GPG Key ID: 4D9CF147DCADD05D

@ -24,6 +24,7 @@ class PlayerHistory(Base):
if result: if result:
time, location = result.time, result.location time, location = result.time, result.location
session.commit() session.commit()
session.close()
return time, location return time, location
@ -33,4 +34,5 @@ class PlayerHistory(Base):
self.location = location self.location = location
session.add(self) session.add(self)
session.commit() session.commit()
session.close()
return True return True

@ -25,6 +25,7 @@ class PokemonList:
result = session.query(Pokemon).where(Pokemon.end_time > datetime.now() + self.timedelta)\ result = session.query(Pokemon).where(Pokemon.end_time > datetime.now() + self.timedelta)\
.order_by(Pokemon.name) .order_by(Pokemon.name)
session.commit() session.commit()
session.close()
return result.all() return result.all()
def sort_by_cp(self, reverse=False): def sort_by_cp(self, reverse=False):
@ -36,6 +37,7 @@ class PokemonList:
result = session.query(Pokemon).where(Pokemon.end_time > datetime.now() + self.timedelta) \ result = session.query(Pokemon).where(Pokemon.end_time > datetime.now() + self.timedelta) \
.order_by(Pokemon.cp) .order_by(Pokemon.cp)
session.commit() session.commit()
session.close()
return result.all() return result.all()
def sort_by_level(self, reverse=False): def sort_by_level(self, reverse=False):
@ -47,18 +49,21 @@ class PokemonList:
result = session.query(Pokemon).where(Pokemon.end_time > datetime.now() + self.timedelta) \ result = session.query(Pokemon).where(Pokemon.end_time > datetime.now() + self.timedelta) \
.order_by(Pokemon.level) .order_by(Pokemon.level)
session.commit() session.commit()
session.close()
return result.all() return result.all()
def sort_by_distance(self, location: Location, reverse=False): def sort_by_distance(self, location: Location, reverse=False):
with Session(self.engine) as session: with Session(self.engine) as session:
result = session.query(Pokemon).where(Pokemon.end_time > datetime.now() + self.timedelta) result = session.query(Pokemon).where(Pokemon.end_time > datetime.now() + self.timedelta)
session.commit() session.commit()
session.close()
return sorted(result.all(), key=lambda pokemon: location.distance(pokemon.location), reverse=reverse) return sorted(result.all(), key=lambda pokemon: location.distance(pokemon.location), reverse=reverse)
def search_by_name(self, query: str): def search_by_name(self, query: str):
with Session(self.engine) as session: with Session(self.engine) as session:
result = session.query(Pokemon).where(Pokemon.end_time > datetime.now() + self.timedelta) result = session.query(Pokemon).where(Pokemon.end_time > datetime.now() + self.timedelta)
session.commit() session.commit()
session.close()
return list(filter(lambda pokemon: partial_ratio(query.lower(), pokemon.name) >= 80, result.all())) return list(filter(lambda pokemon: partial_ratio(query.lower(), pokemon.name) >= 80, result.all()))
def insert_to_database(self, pokemons: List[Pokemon]): def insert_to_database(self, pokemons: List[Pokemon]):
@ -66,4 +71,5 @@ class PokemonList:
with Session(self.engine) as session: with Session(self.engine) as session:
session.add_all(pokemons) session.add_all(pokemons)
session.commit() session.commit()
session.close()

@ -25,6 +25,7 @@ class RaidList:
result = session.query(Raid).where(Raid.end_time > datetime.now() + self.timedelta) \ result = session.query(Raid).where(Raid.end_time > datetime.now() + self.timedelta) \
.order_by(Raid.name) .order_by(Raid.name)
session.commit() session.commit()
session.close()
return result.all() return result.all()
def sort_by_level(self, reverse=False): def sort_by_level(self, reverse=False):
@ -36,18 +37,21 @@ class RaidList:
result = session.query(Raid).where(Raid.end_time > datetime.now() + self.timedelta) \ result = session.query(Raid).where(Raid.end_time > datetime.now() + self.timedelta) \
.order_by(Raid.level) .order_by(Raid.level)
session.commit() session.commit()
session.close()
return result.all() return result.all()
def sort_by_distance(self, location: Location, reverse=False): def sort_by_distance(self, location: Location, reverse=False):
with Session(self.engine) as session: with Session(self.engine) as session:
result = session.query(Raid).where(Raid.end_time > datetime.now() + self.timedelta) result = session.query(Raid).where(Raid.end_time > datetime.now() + self.timedelta)
session.commit() session.commit()
session.close()
return sorted(result.all(), key=lambda raid: location.distance(raid.location), reverse=reverse) return sorted(result.all(), key=lambda raid: location.distance(raid.location), reverse=reverse)
def search_by_name(self, query: str): def search_by_name(self, query: str):
with Session(self.engine) as session: with Session(self.engine) as session:
result = session.query(Raid).where(Raid.end_time > datetime.now() + self.timedelta) result = session.query(Raid).where(Raid.end_time > datetime.now() + self.timedelta)
session.commit() session.commit()
session.close()
return list(filter(lambda raid: partial_ratio(query.lower(), raid.name) >= 80, result.all())) return list(filter(lambda raid: partial_ratio(query.lower(), raid.name) >= 80, result.all()))
def insert_to_database(self, raids: List[Raid]): def insert_to_database(self, raids: List[Raid]):
@ -55,4 +59,5 @@ class RaidList:
with Session(self.engine) as session: with Session(self.engine) as session:
session.add_all(raids) session.add_all(raids)
session.commit() session.commit()
session.close()

Loading…
Cancel
Save