This repository has been archived on 2022-03-31. You can view files and clone it, but cannot push or open issues or pull requests.
GesMag/stock.py

123 lines
4.2 KiB
Python
Raw Normal View History

2021-11-24 17:53:17 +01:00
from random import randint, uniform
from db import BaseDeDonnees
class Stock(BaseDeDonnees):
"""Gère une table "stock" pour une base de donnée donné."""
def __init__(self):
super().__init__(r"db.sqlite3")
def creationTable(self) -> None:
"""Créer la table qui stocker les stocks."""
requete = """
CREATE TABLE IF NOT EXISTS stocks (
id INTEGER PRIMARY KEY,
type TEXT,
nom TEXT,
quantite INTEGER,
prix REAL,
image_url TEXT
);
"""
self.requete(requete)
# Ajout d'un stock par défaut si aucun stock n'existe dans la base de donnée
if len(self.listeStocks()) == 0:
2021-11-24 17:53:17 +01:00
defaut = {
"fruits legumes": [
"banane",
"orange",
"betterave",
"carottes",
"tomates",
"citron",
"kiwi",
"clementine",
"pomme",
"avocat"
],
"boulangerie": [
"brownie",
"baguette",
"pain au chocolat",
"croissant",
"macaron",
"millefeuille",
"paris-brest",
"opera",
"fraisier",
"eclair"
],
"boucherie poissnerie": [
"saucisson",
"côte de boeuf",
"langue de boeuf",
"collier de boeuf",
"entrecote",
"cabillaud",
"saumon",
"colin",
"bar",
"dorade"
],
"entretien": [
"nettoyant air comprimé",
"nettoyage anti-bactérien",
"nettoyant pour écran",
"nettoyant pour lunettes",
"pioche",
"pelle",
"lampe torche",
"gants",
"éponge",
"essuie-tout"
]
}
for type in defaut:
for element in defaut[type]:
self.ajoutStock(type, element, randint(0, 10), round(uniform(2., 30.), 2), "noimage")
def ajoutStock(self, type: str, nom: str, quantite: int, prix: float, imageURL: str) -> list:
"""Ajoute un élément dans le stock et retourne l'ID de ce dernier."""
requete = """
INSERT INTO stocks (
type, nom, quantite, prix, image_url
) VALUES (
?, ?, ?, ?, ?
);
"""
self.requete(requete, [type.lower(), nom.lower(), quantite, prix, imageURL])
return self.affichageResultat(self.requete("SELECT last_insert_rowid();"))
def suppressionStocks(self, id: int) -> None:
"""Supprime un stock."""
requete = """
DELETE FROM stocks
WHERE id = ?
"""
self.requete(requete, id)
def listeStocks(self) -> list:
"""Retourne la liste des éléments en stock sous forme de dictionnaire."""
2021-11-24 17:53:17 +01:00
recuperation = [
"id",
"type",
"nom",
"quantite",
"prix",
"image_url"
]
requete = f"""
SELECT {", ".join(recuperation)} FROM stocks
"""
return [dict(zip(recuperation, element)) for element in self.affichageResultat(self.requete(requete))]
2021-11-24 17:53:17 +01:00
def stockExistant(self, stock: str) -> bool:
"""Vérifie si l"stock donnée existe déjà dans la base de donnée."""
requete = """
SELECT EXISTS (
SELECT 1 FROM stocks
WHERE name = ?
)
"""
return True if self.affichageResultat(self.requete(requete, stock.lower()))[0][0] == 1 else False