class CreateDatabase: """ This class manages the creation of the database. """ def __init__(self): self.db_registered_product = RegisteredProductDatabase() self.db_product = ProductDatabase() self.db_category = CategoryDatabase() self.db_update = LogDatabase() self.db_injection = InjectData() self.interface = ConsoleCreateView() def prepare(self): """ This method creates all the necessary databases for the application """ # Databases are created self.interface.start_db_creation() self.db_registered_product.create_db() self.db_product.create_db() self.db_category.create_db() self.db_update.create_db() # Foreign keys are created self.db_product.create_keys() self.db_registered_product.create_keys() # Databases are feed self.interface.start_get_categories() self.db_injection.feed_categories() self.db_injection.feed_products() # Update date is registered self.db_update.inject_update_date() self.interface.end_creation()
class UpdateDatabase: """ This class manages all the actions linked to the update of the database """ def __init__(self): self.daily_date = datetime.date.today() self.db_registered_product = RegisteredProductDatabase() self.db_update = LogDatabase() self.db_category = CategoryDatabase() self.db_product = ProductDatabase() self.db_injection = InjectData() self.interface = ConsoleUpdateView() self.update = self.__update_decision() def update_database(self, force=False): """ This method updates the database with modification identified in API response """ if self.update or force: self.interface.start_update() saved_products_ref_list = self.db_registered_product.get_products_ref() #We delete registered_product and product tables and recreate them empty self.db_registered_product.create_db() self.db_product.create_db() self.db_product.create_keys() self.db_registered_product.create_keys() #We feed the products for each get_categories self.interface.start_feed_product() self.db_injection.feed_products() #for each ref from saved_products_ref_tuple: for product_ref in saved_products_ref_list: product_id = self.db_product.get_product_from_ref(product_ref) print(product_ref) if product_id: print('injection produit disponible') self.db_registered_product.inject_product(product_ref, 'disponible') else: print('injection produit indisponible') self.db_registered_product.inject_product(product_ref, 'indisponible') #Don't forget to update the update date self.db_update.inject_update_date() self.interface.end_update() def __update_decision(self): """ This method decides if an update of the database has to be done """ last_update_date = self.db_update.get_last_update_date() #type: datetime.date duration = self.daily_date - last_update_date #type: datetime.timedelta if duration.days >= 7: return True