EGYADMIN commited on
Commit
032fab8
·
verified ·
1 Parent(s): 5b246c4

Delete database

Browse files
Files changed (2) hide show
  1. database/db_connector.py +0 -61
  2. database/models.py +0 -279
database/db_connector.py DELETED
@@ -1,61 +0,0 @@
1
- #!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
-
4
- """
5
- وحدة الاتصال بقاعدة البيانات
6
- """
7
-
8
- import os
9
- import sys
10
- import psycopg2
11
- from dotenv import load_dotenv
12
-
13
- # إضافة مسار النظام للوصول للملفات المشتركة
14
- sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
15
-
16
- # تحميل متغيرات البيئة
17
- load_dotenv()
18
-
19
- def get_connection():
20
- """
21
- إنشاء اتصال بقاعدة البيانات
22
-
23
- الإرجاع:
24
- اتصال بقاعدة البيانات
25
- """
26
- try:
27
- # محاولة الاتصال بقاعدة البيانات
28
- conn = psycopg2.connect(
29
- dbname=os.getenv("PGDATABASE"),
30
- user=os.getenv("PGUSER"),
31
- password=os.getenv("PGPASSWORD"),
32
- host=os.getenv("PGHOST"),
33
- port=os.getenv("PGPORT")
34
- )
35
- return conn
36
- except Exception as e:
37
- print(f"خطأ في الاتصال بقاعدة البيانات: {e}")
38
-
39
- # إذا فشل الاتصال، استخدم اتصال قاعدة بيانات SQLite محلية
40
- import os
41
- import sqlite3
42
-
43
- # إنشاء مجلد البيانات إذا لم يكن موجوداً
44
- data_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'data')
45
- os.makedirs(data_dir, exist_ok=True)
46
-
47
- # إنشاء اتصال قاعدة بيانات SQLite محلية
48
- db_path = os.path.join(data_dir, 'local_db.sqlite')
49
- conn = sqlite3.connect(db_path)
50
-
51
- # إعادة محاكاة سلوك اتصال PostgreSQL
52
- conn.execute = conn.cursor().execute
53
-
54
- # إضافة وظيفة وهمية للاقتطاع (commit) والإغلاق
55
- original_close = conn.close
56
- def enhanced_close():
57
- conn.commit()
58
- original_close()
59
- conn.close = enhanced_close
60
-
61
- return conn
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
database/models.py DELETED
@@ -1,279 +0,0 @@
1
- """
2
- نماذج بيانات النظام
3
- """
4
-
5
- from sqlalchemy import Column, Integer, String, Float, DateTime, ForeignKey, Boolean, Text, Table, Enum
6
- from sqlalchemy.orm import relationship
7
- from datetime import datetime
8
- import enum
9
-
10
- from database.db_connector import Base
11
-
12
-
13
- # جدول العلاقة متعددة القيم بين المشاريع والملفات
14
- project_files = Table(
15
- 'project_files',
16
- Base.metadata,
17
- Column('project_id', Integer, ForeignKey('projects.id'), primary_key=True),
18
- Column('file_id', Integer, ForeignKey('files.id'), primary_key=True)
19
- )
20
-
21
- # تعريف الأنواع المدرجة
22
- class ProjectStatus(enum.Enum):
23
- """حالة المشروع"""
24
- NEW = "جديد"
25
- PRICING = "قيد التسعير"
26
- SUBMITTED = "تم التقديم"
27
- AWARDED = "تمت الترسية"
28
- EXECUTION = "قيد التنفيذ"
29
- COMPLETED = "منتهي"
30
- CANCELLED = "ملغي"
31
-
32
- class TenderType(enum.Enum):
33
- """نوع المناقصة"""
34
- PUBLIC = "عامة"
35
- PRIVATE = "خاصة"
36
- DIRECT = "أمر مباشر"
37
-
38
- class PricingMethod(enum.Enum):
39
- """طريقة التسعير"""
40
- STANDARD = "قياسي"
41
- UNBALANCED = "غير متزن"
42
- COMPETITIVE = "تنافسي"
43
- PROFITABILITY = "موجه بالربحية"
44
-
45
- # نموذج المستخدم
46
- class User(Base):
47
- """نموذج بيانات المستخدم"""
48
- __tablename__ = 'users'
49
-
50
- id = Column(Integer, primary_key=True)
51
- username = Column(String(50), unique=True, nullable=False)
52
- password_hash = Column(String(128), nullable=False)
53
- full_name = Column(String(100), nullable=False)
54
- email = Column(String(100), unique=True, nullable=False)
55
- phone = Column(String(20))
56
- role = Column(String(20), nullable=False)
57
- department = Column(String(50))
58
- is_active = Column(Boolean, default=True)
59
- created_at = Column(DateTime, default=datetime.now)
60
- last_login = Column(DateTime)
61
-
62
- # العلاقات
63
- projects = relationship("Project", back_populates="created_by")
64
- pricing_items = relationship("PricingItem", back_populates="created_by")
65
-
66
- def __repr__(self):
67
- return f"<User {self.username}>"
68
-
69
- # نموذج المشروع
70
- class Project(Base):
71
- """نموذج بيانات المشروع"""
72
- __tablename__ = 'projects'
73
-
74
- id = Column(Integer, primary_key=True)
75
- name = Column(String(100), nullable=False)
76
- tender_number = Column(String(50))
77
- client = Column(String(100), nullable=False)
78
- location = Column(String(100))
79
- description = Column(Text)
80
- status = Column(Enum(ProjectStatus), default=ProjectStatus.NEW)
81
- tender_type = Column(Enum(TenderType), default=TenderType.PUBLIC)
82
- pricing_method = Column(Enum(PricingMethod), default=PricingMethod.STANDARD)
83
- submission_date = Column(DateTime)
84
- created_at = Column(DateTime, default=datetime.now)
85
- updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
86
- created_by_id = Column(Integer, ForeignKey('users.id'))
87
-
88
- # العلاقات
89
- created_by = relationship("User", back_populates="projects")
90
- pricing_sections = relationship("PricingSection", back_populates="project", cascade="all, delete-orphan")
91
- pricing_items = relationship("PricingItem", back_populates="project", cascade="all, delete-orphan")
92
- local_content_items = relationship("LocalContentItem", back_populates="project", cascade="all, delete-orphan")
93
- risk_items = relationship("RiskItem", back_populates="project", cascade="all, delete-orphan")
94
- files = relationship("File", secondary=project_files, back_populates="projects")
95
-
96
- def __repr__(self):
97
- return f"<Project {self.name}>"
98
-
99
- # نموذج قسم التسعير
100
- class PricingSection(Base):
101
- """نموذج بيانات قسم التسعير"""
102
- __tablename__ = 'pricing_sections'
103
-
104
- id = Column(Integer, primary_key=True)
105
- project_id = Column(Integer, ForeignKey('projects.id'), nullable=False)
106
- name = Column(String(100), nullable=False)
107
- description = Column(Text)
108
- section_order = Column(Integer, default=0)
109
- created_at = Column(DateTime, default=datetime.now)
110
- updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
111
-
112
- # العلاقات
113
- project = relationship("Project", back_populates="pricing_sections")
114
- pricing_items = relationship("PricingItem", back_populates="section", cascade="all, delete-orphan")
115
-
116
- def __repr__(self):
117
- return f"<PricingSection {self.name}>"
118
-
119
- # نموذج بند التسعير
120
- class PricingItem(Base):
121
- """نموذج بيانات بند التسعير"""
122
- __tablename__ = 'pricing_items'
123
-
124
- id = Column(Integer, primary_key=True)
125
- project_id = Column(Integer, ForeignKey('projects.id'), nullable=False)
126
- section_id = Column(Integer, ForeignKey('pricing_sections.id'))
127
- item_code = Column(String(20))
128
- description = Column(Text, nullable=False)
129
- unit = Column(String(20), nullable=False)
130
- quantity = Column(Float, nullable=False)
131
- unit_price = Column(Float, default=0)
132
- unbalanced_price = Column(Float)
133
- final_price = Column(Float)
134
- pricing_strategy = Column(String(20), default="متوازن")
135
- notes = Column(Text)
136
- created_by_id = Column(Integer, ForeignKey('users.id'))
137
- created_at = Column(DateTime, default=datetime.now)
138
- updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
139
-
140
- # العلاقات
141
- project = relationship("Project", back_populates="pricing_items")
142
- section = relationship("PricingSection", back_populates="pricing_items")
143
- created_by = relationship("User", back_populates="pricing_items")
144
- resource_usages = relationship("ResourceUsage", back_populates="pricing_item")
145
-
146
- def __repr__(self):
147
- return f"<PricingItem {self.item_code}: {self.description[:30]}>"
148
-
149
- # نموذج بند المحتوى المحلي
150
- class LocalContentItem(Base):
151
- """نموذج بيانات بند المحتوى المحلي"""
152
- __tablename__ = 'local_content_items'
153
-
154
- id = Column(Integer, primary_key=True)
155
- project_id = Column(Integer, ForeignKey('projects.id'), nullable=False)
156
- category = Column(String(50), nullable=False)
157
- item_name = Column(String(100), nullable=False)
158
- supplier_id = Column(Integer, ForeignKey('suppliers.id'))
159
- total_cost = Column(Float, default=0)
160
- local_percentage = Column(Float, default=0)
161
- notes = Column(Text)
162
- created_at = Column(DateTime, default=datetime.now)
163
- updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
164
-
165
- # العلاقات
166
- project = relationship("Project", back_populates="local_content_items")
167
- supplier = relationship("Supplier", back_populates="local_content_items")
168
-
169
- def __repr__(self):
170
- return f"<LocalContentItem {self.item_name}>"
171
-
172
- # نموذج المورد
173
- class Supplier(Base):
174
- """نموذج بيانات المورد"""
175
- __tablename__ = 'suppliers'
176
-
177
- id = Column(Integer, primary_key=True)
178
- name = Column(String(100), nullable=False)
179
- contact_person = Column(String(100))
180
- phone = Column(String(20))
181
- email = Column(String(100))
182
- address = Column(String(200))
183
- category = Column(String(50))
184
- is_local = Column(Boolean, default=False)
185
- local_content_percentage = Column(Float, default=0)
186
- created_at = Column(DateTime, default=datetime.now)
187
- updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
188
-
189
- # العلاقات
190
- local_content_items = relationship("LocalContentItem", back_populates="supplier")
191
- resources = relationship("Resource", back_populates="supplier")
192
-
193
- def __repr__(self):
194
- return f"<Supplier {self.name}>"
195
-
196
- # نموذج المخاطرة
197
- class RiskItem(Base):
198
- """نموذج بيانات المخاطرة"""
199
- __tablename__ = 'risk_items'
200
-
201
- id = Column(Integer, primary_key=True)
202
- project_id = Column(Integer, ForeignKey('projects.id'), nullable=False)
203
- risk_code = Column(String(20))
204
- description = Column(Text, nullable=False)
205
- category = Column(String(50), nullable=False)
206
- impact = Column(String(20), nullable=False)
207
- probability = Column(String(20), nullable=False)
208
- mitigation_strategy = Column(Text)
209
- created_at = Column(DateTime, default=datetime.now)
210
- updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
211
-
212
- # العلاقات
213
- project = relationship("Project", back_populates="risk_items")
214
-
215
- def __repr__(self):
216
- return f"<RiskItem {self.risk_code}: {self.description[:30]}>"
217
-
218
- # نموذج المورد
219
- class Resource(Base):
220
- """نموذج بيانات المورد"""
221
- __tablename__ = 'resources'
222
-
223
- id = Column(Integer, primary_key=True)
224
- code = Column(String(20), unique=True)
225
- name = Column(String(100), nullable=False)
226
- description = Column(Text)
227
- category = Column(String(50), nullable=False)
228
- unit = Column(String(20), nullable=False)
229
- unit_price = Column(Float, default=0)
230
- supplier_id = Column(Integer, ForeignKey('suppliers.id'))
231
- is_local = Column(Boolean, default=False)
232
- local_content_percentage = Column(Float, default=0)
233
- created_at = Column(DateTime, default=datetime.now)
234
- updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
235
-
236
- # العلاقات
237
- supplier = relationship("Supplier", back_populates="resources")
238
- resource_usages = relationship("ResourceUsage", back_populates="resource")
239
-
240
- def __repr__(self):
241
- return f"<Resource {self.code}: {self.name}>"
242
-
243
- # نموذج استخدام المورد
244
- class ResourceUsage(Base):
245
- """نموذج بيانات استخدام المورد"""
246
- __tablename__ = 'resource_usages'
247
-
248
- id = Column(Integer, primary_key=True)
249
- pricing_item_id = Column(Integer, ForeignKey('pricing_items.id'), nullable=False)
250
- resource_id = Column(Integer, ForeignKey('resources.id'), nullable=False)
251
- quantity = Column(Float, nullable=False)
252
- created_at = Column(DateTime, default=datetime.now)
253
- updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
254
-
255
- # العلاقات
256
- pricing_item = relationship("PricingItem", back_populates="resource_usages")
257
- resource = relationship("Resource", back_populates="resource_usages")
258
-
259
- def __repr__(self):
260
- return f"<ResourceUsage {self.pricing_item_id} - {self.resource_id}>"
261
-
262
- # نموذج الملف
263
- class File(Base):
264
- """نموذج بيانات الملف"""
265
- __tablename__ = 'files'
266
-
267
- id = Column(Integer, primary_key=True)
268
- filename = Column(String(100), nullable=False)
269
- original_filename = Column(String(100), nullable=False)
270
- file_type = Column(String(20), nullable=False)
271
- file_size = Column(Integer, nullable=False)
272
- file_path = Column(String(255), nullable=False)
273
- upload_date = Column(DateTime, default=datetime.now)
274
-
275
- # العلاقات
276
- projects = relationship("Project", secondary=project_files, back_populates="files")
277
-
278
- def __repr__(self):
279
- return f"<File {self.original_filename}>"