Spaces:
Runtime error
Runtime error
| """ | |
| وحدة إدارة حالة الجلسة | |
| """ | |
| import streamlit as st | |
| from datetime import datetime | |
| import pandas as pd | |
| import config | |
| def initialize_session_state(): | |
| """ | |
| تهيئة متغيرات حالة الجلسة | |
| """ | |
| # المتغيرات الرئيسية لحالة المستخدم | |
| if 'is_authenticated' not in st.session_state: | |
| st.session_state.is_authenticated = False | |
| if 'user_info' not in st.session_state: | |
| st.session_state.user_info = None | |
| # المتغيرات المتعلقة بالمناقصات والمشاريع | |
| if 'current_project' not in st.session_state: | |
| st.session_state.current_project = None | |
| if 'current_pricing' not in st.session_state: | |
| st.session_state.current_pricing = None | |
| if 'pricing_history' not in st.session_state: | |
| st.session_state.pricing_history = [] | |
| # المتغيرات المتعلقة بالمحتوى المحلي | |
| if 'local_content_products' not in st.session_state: | |
| st.session_state.local_content_products = pd.DataFrame({ | |
| 'المنتج': [], | |
| 'الكمية': [], | |
| 'سعر_الوحدة': [], | |
| 'التكلفة_الإجمالية': [], | |
| 'نسبة_المحتوى_المحلي': [] | |
| }) | |
| if 'local_content_services' not in st.session_state: | |
| st.session_state.local_content_services = pd.DataFrame({ | |
| 'الخدمة': [], | |
| 'التكلفة': [], | |
| 'نسبة_المحتوى_المحلي': [] | |
| }) | |
| if 'local_content_labor' not in st.session_state: | |
| st.session_state.local_content_labor = pd.DataFrame({ | |
| 'فئة_العمالة': [], | |
| 'العدد': [], | |
| 'الراتب_الشهري': [], | |
| 'المدة_بالأشهر': [], | |
| 'نسبة_المحتوى_المحلي': [] | |
| }) | |
| # المتغيرات المتعلقة بتحليل المستندات | |
| if 'current_document' not in st.session_state: | |
| st.session_state.current_document = None | |
| if 'analyzed_documents' not in st.session_state: | |
| st.session_state.analyzed_documents = [] | |
| # المتغيرات المتعلقة بالتسعير | |
| if 'manual_items' not in st.session_state: | |
| st.session_state.manual_items = pd.DataFrame({ | |
| 'رقم البند': [], | |
| 'وصف البند': [], | |
| 'الوحدة': [], | |
| 'الكمية': [], | |
| 'سعر الوحدة': [], | |
| 'الإجمالي': [] | |
| }) | |
| # المتغيرات المتعلقة بالمصادر | |
| if 'resources' not in st.session_state: | |
| st.session_state.resources = [] | |
| # المتغيرات المتعلقة بالإعدادات | |
| if 'settings' not in st.session_state: | |
| st.session_state.settings = { | |
| 'ui_theme': config.UI_THEME, | |
| 'locale': config.LOCALE, | |
| 'enable_animations': config.ENABLE_ANIMATIONS | |
| } | |
| def save_current_pricing(): | |
| """ | |
| حفظ التسعير الحالي في سجل التسعير | |
| الإرجاع: | |
| True في حالة النجاح، False في حالة الفشل | |
| """ | |
| try: | |
| if st.session_state.current_pricing: | |
| # إضافة معلومات إضافية | |
| pricing_entry = st.session_state.current_pricing.copy() | |
| pricing_entry['timestamp'] = datetime.now() | |
| # إضافة إلى سجل التسعير | |
| st.session_state.pricing_history.append(pricing_entry) | |
| return True | |
| return False | |
| except Exception as e: | |
| print(f"خطأ في حفظ التسعير الحالي: {str(e)}") | |
| return False | |
| def set_current_project(project_data): | |
| """ | |
| تعيين المشروع الحالي | |
| المعلمات: | |
| project_data: بيانات المشروع | |
| """ | |
| st.session_state.current_project = project_data | |
| def set_current_pricing(pricing_data): | |
| """ | |
| تعيين التسعير الحالي | |
| المعلمات: | |
| pricing_data: بيانات التسعير | |
| """ | |
| st.session_state.current_pricing = pricing_data | |
| def set_current_document(document_data): | |
| """ | |
| تعيين المستند الحالي | |
| المعلمات: | |
| document_data: بيانات المستند | |
| """ | |
| st.session_state.current_document = document_data | |
| def clear_session(): | |
| """ | |
| مسح بيانات الجلسة الحالية | |
| """ | |
| # الاحتفاظ بحالة المصادقة والمستخدم | |
| is_authenticated = st.session_state.is_authenticated | |
| user_info = st.session_state.user_info | |
| settings = st.session_state.settings | |
| # مسح المتغيرات | |
| st.session_state.clear() | |
| # إعادة تعيين حالة المصادقة والمستخدم | |
| st.session_state.is_authenticated = is_authenticated | |
| st.session_state.user_info = user_info | |
| st.session_state.settings = settings | |
| # إعادة تهيئة متغيرات الجلسة | |
| initialize_session_state() | |
| def export_session_state(): | |
| """ | |
| تصدير حالة الجلسة الحالية | |
| الإرجاع: | |
| قاموس يحتوي على حالة الجلسة | |
| """ | |
| # إنشاء نسخة من حالة الجلسة | |
| session_data = {} | |
| # تخزين البيانات الرئيسية | |
| if st.session_state.current_project: | |
| session_data['current_project'] = st.session_state.current_project | |
| if st.session_state.current_pricing: | |
| session_data['current_pricing'] = st.session_state.current_pricing | |
| if st.session_state.pricing_history: | |
| session_data['pricing_history'] = st.session_state.pricing_history | |
| # تحويل DataFrames إلى قوائم من القواميس | |
| if 'local_content_products' in st.session_state and not st.session_state.local_content_products.empty: | |
| session_data['local_content_products'] = st.session_state.local_content_products.to_dict('records') | |
| if 'local_content_services' in st.session_state and not st.session_state.local_content_services.empty: | |
| session_data['local_content_services'] = st.session_state.local_content_services.to_dict('records') | |
| if 'local_content_labor' in st.session_state and not st.session_state.local_content_labor.empty: | |
| session_data['local_content_labor'] = st.session_state.local_content_labor.to_dict('records') | |
| # تخزين البيانات الأخرى | |
| if 'manual_items' in st.session_state and not st.session_state.manual_items.empty: | |
| session_data['manual_items'] = st.session_state.manual_items.to_dict('records') | |
| if st.session_state.resources: | |
| session_data['resources'] = st.session_state.resources | |
| # إضافة بيانات الوقت | |
| session_data['exported_at'] = datetime.now().isoformat() | |
| return session_data | |
| def import_session_state(session_data): | |
| """ | |
| استيراد حالة الجلسة | |
| المعلمات: | |
| session_data: قاموس يحتوي على حالة الجلسة | |
| الإرجاع: | |
| True في حالة النجاح، False في حالة الفشل | |
| """ | |
| try: | |
| # استيراد البيانات الرئيسية | |
| if 'current_project' in session_data: | |
| st.session_state.current_project = session_data['current_project'] | |
| if 'current_pricing' in session_data: | |
| st.session_state.current_pricing = session_data['current_pricing'] | |
| if 'pricing_history' in session_data: | |
| st.session_state.pricing_history = session_data['pricing_history'] | |
| # استيراد DataFrames | |
| if 'local_content_products' in session_data: | |
| st.session_state.local_content_products = pd.DataFrame(session_data['local_content_products']) | |
| if 'local_content_services' in session_data: | |
| st.session_state.local_content_services = pd.DataFrame(session_data['local_content_services']) | |
| if 'local_content_labor' in session_data: | |
| st.session_state.local_content_labor = pd.DataFrame(session_data['local_content_labor']) | |
| # استيراد البيانات الأخرى | |
| if 'manual_items' in session_data: | |
| st.session_state.manual_items = pd.DataFrame(session_data['manual_items']) | |
| if 'resources' in session_data: | |
| st.session_state.resources = session_data['resources'] | |
| return True | |
| except Exception as e: | |
| print(f"خطأ في استيراد حالة الجلسة: {str(e)}") | |
| return False |