Reza-galaxy21 commited on
Commit
02f791b
·
verified ·
1 Parent(s): 3df422e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -13
app.py CHANGED
@@ -5,37 +5,45 @@ from utils import parse_excel_file, save_to_json
5
 
6
  DATA_FILE = "material_db.json"
7
 
 
 
 
 
 
 
 
 
 
8
  def process_excel(file, description):
9
  if file is None:
10
  return "❌ لطفاً یک فایل اکسل بارگذاری کنید.", ""
11
-
12
  try:
13
- # پردازش فایل اکسل
14
  materials = parse_excel_file(file.name)
15
-
16
- if len(materials) == 0:
17
- return "❌ هیچ ماده‌ای برای ذخیره‌سازی یافت نشد.", ""
18
-
19
  record = {
20
  "project_description": description.strip(),
21
  "materials": materials
22
  }
23
 
24
- # بارگذاری داده‌های قبلی
25
  all_data = []
26
  if os.path.exists(DATA_FILE):
27
  with open(DATA_FILE, "r", encoding="utf-8") as f:
28
  try:
29
- all_data = json.load(f)
 
 
 
 
30
  except json.JSONDecodeError:
31
  pass # فایل خالی یا خراب
32
 
33
- # بررسی تکراری بودن توصیف پروژه
34
- for item in all_data:
35
- if item.get("project_description", "").strip() == description.strip():
36
- return "⚠️ پروژه‌ای با این توصیف قبلاً ثبت شده است.", json.dumps(item, indent=2, ensure_ascii=False)
37
 
38
- # افزودن پروژه جدید و ذخیره در فایل
39
  all_data.append(record)
40
  save_to_json(all_data, DATA_FILE)
41
 
 
5
 
6
  DATA_FILE = "material_db.json"
7
 
8
+ def is_duplicate(record, all_data):
9
+ for item in all_data:
10
+ if (
11
+ item.get("project_description") == record.get("project_description")
12
+ and item.get("materials") == record.get("materials")
13
+ ):
14
+ return True
15
+ return False
16
+
17
  def process_excel(file, description):
18
  if file is None:
19
  return "❌ لطفاً یک فایل اکسل بارگذاری کنید.", ""
20
+
21
  try:
22
+ # ۱. پردازش فایل اکسل
23
  materials = parse_excel_file(file.name)
 
 
 
 
24
  record = {
25
  "project_description": description.strip(),
26
  "materials": materials
27
  }
28
 
29
+ # ۲. بارگذاری داده‌های قبلی
30
  all_data = []
31
  if os.path.exists(DATA_FILE):
32
  with open(DATA_FILE, "r", encoding="utf-8") as f:
33
  try:
34
+ existing_data = json.load(f)
35
+ if isinstance(existing_data, list):
36
+ all_data = existing_data
37
+ else:
38
+ all_data = [existing_data]
39
  except json.JSONDecodeError:
40
  pass # فایل خالی یا خراب
41
 
42
+ # ۳. بررسی تکراری بودن
43
+ if is_duplicate(record, all_data):
44
+ return "⚠️ پروژه‌ای با همین توصیف و مواد قبلاً ثبت شده است.", ""
 
45
 
46
+ # ۴. ذخیره در فایل JSON
47
  all_data.append(record)
48
  save_to_json(all_data, DATA_FILE)
49