-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabasestorage.py
38 lines (32 loc) · 1.31 KB
/
databasestorage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import json
from main import FILENAME
from article import Article
from bd import BD
def sauvegarder_article():
data = {"bd": []}
for bd in Article.articles["bd"]:
bd_data = {
"title": bd.title,
"auteur_name": bd.auteur_name,
"isbn": bd.isbn,
"first_publish_year": bd.first_publish_year,
"language": bd.language,
"code_barre": bd.code_barre(),
"prix": bd.prix(),
"stock": bd.stock()
}
data["bd"].append(bd_data)
# Écrire les nouvelles données dans le fichier
with open(FILENAME, "w") as file:
json.dump(data, file, indent=4)
def charger_articles():
with open(FILENAME, "r") as file:
data = json.load(file) # Charger les données depuis le fichier JSON
# Si des données sont présentes
if data:
# Créer des objets BD à partir des données et les ajouter à la liste
for bd_data in data.get("bd", []):
nouvelle_bd = BD(bd_data["title"], bd_data["auteur_name"], bd_data["isbn"],
bd_data["first_publish_year"], bd_data["language"], bd_data["code_barre"],
bd_data["prix"], bd_data["stock"])
Article.articles["bd"].append(nouvelle_bd)