from peewee import * from LoggerHelper import getLogger, getExceptionLogger from constants import CONST_DatabaseProvider, CONST_GetPostgresDatabaseHost, CONST_GetPostgresDatabasePort, CONST_GetPostgresDatabaseUserName, CONST_GetPostgresDatabaseDatabaseName, CONST_GetPostgresDatabaseUserPassword databaseProvider = CONST_DatabaseProvider() if(databaseProvider == "postgres"): db = PostgresqlDatabase(CONST_GetPostgresDatabaseDatabaseName(), host=CONST_GetPostgresDatabaseHost(), port=int(CONST_GetPostgresDatabasePort()), user=CONST_GetPostgresDatabaseUserName(), password=CONST_GetPostgresDatabaseUserPassword()) else: db = SqliteDatabase('./database/gamesCollection.db') class GameDbObject(Model): fs_id = CharField(unique=True) url = CharField() discount_end_date = DateField() thumbnail = CharField() thumbnail_banner = CharField() price_regular = FloatField() price_discounted = FloatField() price_discount_percentage = IntegerField() price_has_discount = BooleanField() publisher = CharField() title = CharField() nsuid = CharField() catalog_description = TextField() media_type = CharField() class Meta: database = db def addANewGame(game): logger = getLogger() exceptionsLogger = getExceptionLogger() try: GameDbObject.create(fs_id=game.fs_id, url=game.url, discount_end_date=game.discount_end_date, thumbnail=game.thumbnail, thumbnail_banner=game.thumbnail_banner, price_regular=game.price_regular, price_discounted=game.price_discounted, price_discount_percentage=game.price_discount_percentage, price_has_discount=game.price_has_discount, publisher=game.publisher, title=game.title, nsuid=game.nsuid, catalog_description=game.catalog_description, media_type=game.media_type) logMessage = "The game " + game.title + " was added in the Database" logger.info(logMessage) except Exception as err: logMessage = "The game " + game.title + " was not added to the database" logger.error(logMessage) exceptionsLogger.exception(str(err)) def updateGameDatas(game): logger = getLogger() exceptionsLogger = getExceptionLogger() try: existingGame = GameDbObject.get(GameDbObject.nsuid == game.nsuid) query = GameDbObject.update(price_regular=game.price_regular, price_discounted=game.price_discounted, price_discount_percentage=game.price_discount_percentage,price_has_discount=game.price_has_discount).where(GameDbObject.nsuid == game.nsuid) query.execute() # Returns the number of rows that were updated. logMessage = "The game " + game.title + " was updated in the Database" logger.info(logMessage) except DoesNotExist as notCreated: logMessage = "The game with the NSUID " + game.nsuid + " does not exist in the database" logger.warning(logMessage) addANewGame(game) except Exception as err: logMessage = "Unable to query for the game " + game.nsuid + " from the database" logger.error(logMessage) exceptionsLogger.exception(str(err)) db.create_tables([GameDbObject])