CoolFace
Apppublic

pcb-defect-detector-project/new-version

sourceHugging Faceupdated 26d agoView on Hugging Face
0likes
database.py166 linesDownload Raw Back to root
1# database.py - نسخة معدلة للتخزين الدائم2from datasetstorage import DatasetStorage3from datetime import datetime4import json5import hashlib6 7class User:8    def __init__(self, id, username, email, hashed_password, created_at=None):9        self.id = id10        self.username = username11        self.email = email12        self.hashed_password = hashed_password13        self.created_at = created_at or datetime.now().isoformat()14    15    @staticmethod16    def create(username, email, hashed_password):17        """إنشاء مستخدم جديد"""18        # التحقق من عدم وجود المستخدم19        if DatasetStorage.user_exists(username):20            return None21        22        # إنشاء ID بسيط (استخدم العدد الحقيقي في الإنتاج)23        import hashlib24        user_id = hashlib.md5(username.encode()).hexdigest()[:8]25        26        user_data = {27            "id": user_id,28            "username": username,29            "email": email,30            "hashed_password": hashed_password,31            "created_at": datetime.now().isoformat()32        }33        34        if DatasetStorage.save_user(username, user_data):35            return User(user_id, username, email, hashed_password, user_data["created_at"])36        return None37    38    @staticmethod39    def get_by_username(username):40        """جلب مستخدم حسب اسم المستخدم"""41        user_data = DatasetStorage.get_user(username)42        if user_data:43            return User(44                user_data["id"],45                user_data["username"],46                user_data["email"],47                user_data["hashed_password"],48                user_data["created_at"]49            )50        return None51    52    @staticmethod53    def get_by_id(user_id):54        """جلب مستخدم حسب ID"""55        # بحث في جميع المستخدمين (في الإنتاج، استخدم فهرساً)56        files = DatasetStorage._list_files("users/")57        for f in files:58            user_data = DatasetStorage._download_json(f)59            if user_data and user_data.get("id") == user_id:60                return User(61                    user_data["id"],62                    user_data["username"],63                    user_data["email"],64                    user_data["hashed_password"],65                    user_data["created_at"]66                )67        return None68 69class Board:70    def __init__(self, id, title, description, user_id, image_path=None, 71                 annotated_image_path=None, defects_data=None, 72                 report_data=None, created_at=None, updated_at=None):73        self.id = id74        self.title = title75        self.description = description76        self.user_id = user_id77        self.image_path = image_path78        self.annotated_image_path = annotated_image_path79        self.defects_data = defects_data80        self.report_data = report_data81        self.created_at = created_at or datetime.now().isoformat()82        self.updated_at = updated_at or datetime.now().isoformat()83    84    @staticmethod85    def create(user_id, title, description=""):86        """إنشاء لوحة جديدة"""87        import time88        board_id = int(time.time() * 1000)  # استخدام timestamp كـ ID89        90        board_data = {91            "id": board_id,92            "title": title,93            "description": description,94            "user_id": user_id,95            "image_path": None,96            "annotated_image_path": None,97            "defects_data": None,98            "report_data": None,99            "created_at": datetime.now().isoformat(),100            "updated_at": datetime.now().isoformat()101        }102        103        if DatasetStorage.save_board(user_id, board_id, board_data):104            return Board(board_id, title, description, user_id)105        return None106    107    @staticmethod108    def get_all_by_user(user_id):109        """جلب جميع لوحات المستخدم"""110        boards_data = DatasetStorage.get_all_boards(user_id)111        boards = []112        for bd in boards_data:113            boards.append(Board(114                bd["id"], bd["title"], bd.get("description", ""),115                bd["user_id"], bd.get("image_path"), bd.get("annotated_image_path"),116                bd.get("defects_data"), bd.get("report_data"),117                bd.get("created_at"), bd.get("updated_at")118            ))119        return boards120    121    @staticmethod122    def get_by_id(user_id, board_id):123        """جلب لوحة محددة"""124        board_data = DatasetStorage.get_board(user_id, board_id)125        if board_data:126            return Board(127                board_data["id"], board_data["title"], board_data.get("description", ""),128                board_data["user_id"], board_data.get("image_path"), board_data.get("annotated_image_path"),129                board_data.get("defects_data"), board_data.get("report_data"),130                board_data.get("created_at"), board_data.get("updated_at")131            )132        return None133    134    def update(self, **kwargs):135        """تحديث بيانات اللوحة"""136        board_data = {137            "id": self.id,138            "title": kwargs.get("title", self.title),139            "description": kwargs.get("description", self.description),140            "user_id": self.user_id,141            "image_path": kwargs.get("image_path", self.image_path),142            "annotated_image_path": kwargs.get("annotated_image_path", self.annotated_image_path),143            "defects_data": kwargs.get("defects_data", self.defects_data),144            "report_data": kwargs.get("report_data", self.report_data),145            "created_at": self.created_at,146            "updated_at": datetime.now().isoformat()147        }148        if DatasetStorage.save_board(self.user_id, self.id, board_data):149            self.title = board_data["title"]150            self.description = board_data["description"]151            self.updated_at = board_data["updated_at"]152            return True153        return False154    155    def delete(self):156        """حذف اللوحة"""157        return DatasetStorage.delete_board(self.user_id, self.id)158 159# للحفاظ على التوافق مع الكود القديم160def get_db():161    """دالة وهمية للحفاظ على التوافق"""162    class DummySession:163        def __enter__(self): return self164        def __exit__(self, *args): pass165        def close(self): pass166    return DummySession()