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
Mylloon 78c91d1b7e Modifications:
- Fix typo
- Remove useless function
- Return a list of dict instead of a list of tuples in `listeStocks` for better clarity
2021-11-25 16:21:24 +01:00

122 lines
4.2 KiB
Python

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:
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."""
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))]
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