class ModelClima: def __init__(self): self.database = Database() self.cacheTime = 15 # RESPONSÁVEL POR BUSCAR UM CLIMA NO BANCO def select(self, cidade): tempoLimite = dt.datetime.now() - dt.timedelta(minutes=self.cacheTime) tempoLimite = tempoLimite.strftime("%Y-%m-%d %H:%M:%S") cidade = cidade.lower() + '%%' result = self.database.execute( 'SELECT response FROM clima_historico WHERE lower(cidade) ILIKE \'' + cidade + '\' AND data_consulta >= \'' + tempoLimite + '\' ORDER BY id DESC LIMIT 1;') retorno = [] while True: rows = result.fetchmany(10000) if not rows: break for row in rows: retorno.append(row[0]) pass if retorno == []: return [] return json.loads(retorno[0]) # RESPONSÁVEL POR INSERIR UM RETORNO DE CLIMA def insert(self, weather): city = weather['results']['city'] jsonResponse = json.dumps(weather) return self.database.execute( "INSERT INTO clima_historico (id, cidade, response, data_consulta) VALUES ( nextval('clima_sequence'), '" + city + "', '" + jsonResponse + "', current_timestamp);")
def create_thermostat_database(read_only): db_file = 'Thermostat.db' sql_create = """CREATE TABLE IF NOT EXISTS stats ( time integer PRIMARY KEY, sensor_temperature real NOT NULL, target_temperature real NOT NULL, outside_temperature real, boiler_on integer NOT NULL );""" sql_write = ''' INSERT INTO stats(time,sensor_temperature,target_temperature,boiler_on,outside_temperature) VALUES(?,?,?,?,?) ''' sql_read = "SELECT time,sensor_temperature,target_temperature,boiler_on,outside_temperature FROM stats WHERE time>=? AND time<?" return Database(read_only, db_file, sql_create, sql_read, sql_write)
def create_house_database(read_only): db_file = 'House.db' sql_create = """CREATE TABLE IF NOT EXISTS stats ( time integer PRIMARY KEY, k1_best_fit real, k2_best_fit real, k real, is_k1_latest_fit integer );""" sql_write = ''' INSERT INTO stats(time,k1_best_fit,k2_best_fit,k,is_k1_latest_fit) VALUES(?,?,?,?,?) ''' sql_read = "SELECT time,k1_best_fit,k2_best_fit,k,is_k1_latest_fit FROM stats WHERE time>=? AND time<?" return Database(read_only, db_file, sql_create, sql_read, sql_write)
def __init__(self): self.api = APIRequest() self.db = Database(self.api) self.ctrl = KeyboardController() self.running = False self.answer = 0
class LaunchApp: '''Define what happens when someone is using the app''' def __init__(self): self.api = APIRequest() self.db = Database(self.api) self.ctrl = KeyboardController() self.running = False self.answer = 0 def regular_start(self): '''Start and close the app when the database is already created''' print('====PUR BEURRE, l\'application====') print() self.db.database_connexion() #Checking if there's a matching database if self.db.database_selection() == False: self.db.database_creation() #If not we create it self.db.database_selection() if self.db.database_check_in( ) is None: #Checking if the database is filled with products self.first_start() # If not we fill it with the API datas self.running = True while self.running: self.menu() menu_choice = self.ctrl.binary_choice() if menu_choice == 1: #All the process from choosing a categorie to save or not a substitute into the database self.app_cat_query() cat_choice = self.ctrl.cat_choice(self.cat_id) self.db.select_products(cat_choice) self.app_prod_query() prod_choice = self.ctrl.prod_choice(self.prod_id) self.db.select_substitutes(cat_choice, prod_choice) try: self.app_sub_query() sub = self.ctrl.sub_choice(self.sub_id) self.db.show_substitute(sub) self.subs_details() self.sub_saving() save_choice = self.ctrl.binary_choice() if save_choice == 1: #if the user choses to save his query self.db.save_favorites(sub, prod_choice) print('========ENREGISTREMENT EFFECTUÉ========') self.app_closing() end_choice = self.ctrl.binary_choice() if end_choice == 1: self.running = True else: #if the user choses to not save his query, he can end the app or start over self.app_closing() end_choice = self.ctrl.binary_choice() if end_choice == 1: self.running = True else: self.db.database_closing() self.running = False except ValueError: print( 'Désolé, il n\'existe pas de produits de meilleure qualité dans la base de données' ) self.app_closing() end_choice = self.ctrl.binary_choice() if end_choice == 1: self.running = True else: self.db.database_closing() self.running = False else: #Only deals with the action of consulting the saved favorites self.app_fav_query() fav = self.ctrl.fav_choice(self.fav_id) if fav == 0: pass else: self.db.show_favorite(fav) self.favorite_details() self.app_closing() end_choice = self.ctrl.binary_choice() if end_choice == 1: #the user goes back to the starting menu self.running = True else: #disconnect the database and close the app self.db.database_closing() self.running = False def first_start(self): '''When datas are missing or first use of the app''' print('=====Les stocks sont au plus bas !====') print('=====Téléchargement des données====') self.api.data_loading() print('=====Reconstitution des stocks en cours====') self.db.products_recording(self.api) print('====Votre magasin est désormais opérationnel !====') def app_closing(self): '''To close properly the app''' print('========RECHERCHE TERMINÉE========') print() print('Que souhaitez-vous faire ?\ \nPour revenir à l\'accueil -> Tapez 1\ \nPour quitter le programme -> Tapez 2') def menu(self): """First interaction with the user, who has to chose between the favorites or the categories menu""" print() print('Que souhaitez-vous faire ?\ \nPour consulter les catégories d\'aliments disponibles -> Tapez 1\ \nPour consulter vos aliments favoris -> Tapez 2') def app_cat_query(self): """Call the database to show all the available category""" self.db.select_categories() self.text = '''========CATEGORIES========\ \nChoisissez un type d'aliment en tapant son numéro :''' self.cat_id = [] for category in self.db.selected_cat: self.cat_choices = '\n{} -> {}'.format(category.id, category.name) self.text = self.text + self.cat_choices self.cat_id.append(category.id) print(self.text) def app_prod_query(self): """Call the database to show an certain amount of products regarding its category""" self.text = '''=======ALIMENTS=======\ \nChoisissez un produit à substituer en tapant son numéro :''' self.products = sample(self.db.selected_products, 5) self.prod_id = [] for product in self.products: self.prod_choices = '\n{} -> {} / NOVA Groupe : {}'.format( product.id, product.name, product.nova_group) self.text = self.text + self.prod_choices self.prod_id.append(product.id) print(self.text) def app_sub_query(self): """Call the database to show an certain amount of substitutes regarding its grade""" print('''=======PRODUIT À REMPLACER=======''') self.text = '''=======BETTER, HEALTHIER, TASTIER=======''' self.substitutes = sample(self.db.substitute, 1) self.sub_id = [] for original, substitute in zip(self.db.original_prod, self.substitutes): self.recall = '\nVoici un substitut pour {}, Nova GROUPE : {}'.format( original.name, original.nova_group) self.sub_choices = '\n{} -> {}, {}, Groupe NOVA : {}'.format( substitute.id, substitute.name, substitute.description, substitute.nova_group) self.text = self.text + self.sub_choices self.sub_id.append(substitute.id) print(self.recall) print() print(self.text) def subs_details(self): """Show to the user the details of the selected substitute""" self.text = '''=======SUBSTITUT SÉLECTIONNÉ=======''' for product in self.db.selected_substitute: self.product_card = """\nNom : {} \nDescription : {} \nGroupe Nova : {} \nDisponible chez : {} \nCode-barre : {} \nEn savoir plus : {}""".format(product.name, product.description, product.nova_group, product.stores, product.code, product.url) self.text = self.text + self.product_card print(self.text) def sub_saving(self): '''Give the user the choice to save or not the printed substitute''' print() print('Que souhaitez-vous faire ?\ \nPour conserver ce substitut dans votre base de données -> Tapez 1\ \nPour clôturer la recherche -> Tapez 2') def app_fav_query(self): '''If the user wants to take a look at his previous queries''' self.db.select_favorites() self.text = ('=======HALL OF FAME=======') self.fav_id = [] for id, favorite, original in zip(self.db.id, self.db.favorite, self.db.original): self.saves = '\n{} -> {}, comme substitut à {}'.format( id.id, favorite.name, original.name) self.text = self.text + self.saves self.fav_id.append(id.id) print(self.text) def favorite_details(self): """Show to the user the details of the selected substitute""" self.text = '''=======SUBSTITUT SÉLECTIONNÉ=======''' for favorite in self.db.selected_favorite: self.favorite_card = """\nNom : {} \nDescription : {} \nGroupe Nova : {} \nDisponible chez : {} \nCode-barre : {} \nEn savoir plus : {}""".format(favorite.name, favorite.description, favorite.nova_group, favorite.stores, favorite.code, favorite.url) self.text = self.text + self.favorite_card print(self.text)
from Models.Database import Database Database = Database() Database.addCountries()
def __init__(self): self.database = Database() self.cacheTime = 15
def countrycompare(): data = Database.viewCountry() print(data) return render_template("compare_countries.html", countries=data)
def countrystat(): data = Database.viewCountry() print(data) return render_template("country.html", countries=data)
def ownCountry(theName): x = theName data = Database.searchCountry(x) print(data) return render_template("singleCountry.html", countries=data)
def hello(): Database.updateAll() data = Database.viewCountry() print(data) return render_template("home.html", countries=data)
# Import Flask Library from flask import Flask, render_template, request, session, url_for, redirect import pymysql.cursors import datetime import hashlib from Models.Database import Database from Models.CountryList import CountryList # Initialize the app from Flask app = Flask(__name__) Database = Database() CountryList = CountryList() # Define a route to hello function @app.route("/") def hello(): Database.updateAll() data = Database.viewCountry() print(data) return render_template("home.html", countries=data) @app.route("/<string:theName>") def ownCountry(theName): x = theName data = Database.searchCountry(x) print(data) return render_template("singleCountry.html", countries=data)