convenience methods

This commit is contained in:
mcinj 2022-05-09 12:19:36 -04:00
parent 07b44f51f8
commit 6241264147

View file

@ -25,9 +25,23 @@ class TableNotification(Base):
@classmethod @classmethod
def get_won_notifications_today(cls): def get_won_notifications_today(cls):
with Session(engine) as session: with Session(engine) as session:
return session.query(TableNotification)\ return session.query(TableNotification) \
.filter(func.DATE(TableNotification.created_at) == datetime.utcnow().date())\ .filter(func.DATE(TableNotification.created_at) == datetime.utcnow().date()) \
.filter_by(type='won')\ .filter_by(type='won') \
.all()
@classmethod
def get_won_notifications(cls):
with Session(engine) as session:
return session.query(TableNotification) \
.filter_by(type='won') \
.all()
@classmethod
def get_error_notifications(cls):
with Session(engine) as session:
return session.query(TableNotification) \
.filter_by(type='error') \
.all() .all()
@ -68,10 +82,15 @@ class TableGiveaway(Base):
return datetime.utcfromtimestamp(timestamp) return datetime.utcfromtimestamp(timestamp)
@classmethod @classmethod
def upsert_giveaway(cls, giveaway, entered): def get_by_ids(cls, giveaway):
with Session(engine) as session: with Session(engine) as session:
result = session.query(TableGiveaway).filter_by(giveaway_id=giveaway.giveaway_game_id, return session.query(TableGiveaway).filter_by(giveaway_id=giveaway.giveaway_game_id,
steam_id=giveaway.steam_app_id).all() steam_id=giveaway.steam_app_id).all()
@classmethod
def insert(cls, giveaway, entered):
with Session(engine) as session:
result = session.query(TableSteamItem).filter_by(steam_id=giveaway.steam_app_id).all()
if result: if result:
steam_id = result[0].steam_id steam_id = result[0].steam_id
else: else:
@ -79,10 +98,9 @@ class TableGiveaway(Base):
steam_id=giveaway.steam_app_id, steam_id=giveaway.steam_app_id,
steam_url=giveaway.steam_url, steam_url=giveaway.steam_url,
game_name=giveaway.game_name) game_name=giveaway.game_name)
session.merge(item) session.add(item)
session.flush() session.flush()
steam_id = item.steam_id steam_id = item.steam_id
g = TableGiveaway( g = TableGiveaway(
giveaway_id=giveaway.giveaway_game_id, giveaway_id=giveaway.giveaway_game_id,
steam_id=steam_id, steam_id=steam_id,
@ -96,10 +114,32 @@ class TableGiveaway(Base):
entered=entered, entered=entered,
won=False, won=False,
game_entries=giveaway.game_entries) game_entries=giveaway.game_entries)
session.add(g)
session.merge(g)
session.commit() session.commit()
@classmethod
def upsert_giveaway(cls, giveaway, entered):
result = TableGiveaway.get_by_ids(giveaway)
if not result:
TableGiveaway.insert(giveaway, entered)
else:
with Session(engine) as session:
g = TableGiveaway(
giveaway_id=giveaway.giveaway_game_id,
steam_id=result[0].steam_id,
giveaway_uri=giveaway.giveaway_uri,
user=giveaway.user,
giveaway_created_at=TableGiveaway.unix_timestamp_to_utc_datetime(giveaway.time_created_timestamp),
giveaway_ended_at=TableGiveaway.unix_timestamp_to_utc_datetime(giveaway.time_remaining_timestamp),
cost=giveaway.cost,
copies=giveaway.copies,
contributor_level=giveaway.contributor_level,
entered=entered,
won=False,
game_entries=giveaway.game_entries)
session.merge(g)
session.commit()
if not database_exists(engine.url): if not database_exists(engine.url):
create_database(engine.url) create_database(engine.url)