Skip to content

Commit c34499c

Browse files
committed
update schema design
1 parent 3d3f85f commit c34499c

File tree

1 file changed

+29
-66
lines changed

1 file changed

+29
-66
lines changed

encapsulation/data_model/orm_models.py

Lines changed: 29 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ class Base(DeclarativeBase):
2323

2424
# ==================== ENUMS ====================
2525

26+
# Enum to indicate permission target type (user, department, or all)
27+
class PermissionReceiverType(enum.Enum):
28+
USER = "user"
29+
DEPARTMENT = "department"
30+
ALL = "all"
31+
32+
class PermissionType(enum.Enum):
33+
VIEW = "view"
34+
EDIT = "edit"
35+
2636
class UserStatus(enum.Enum):
2737
"""User account status"""
2838
ACTIVE = "ACTIVE"
@@ -42,19 +52,8 @@ class PermissionAction(enum.Enum):
4252
"""Available permission actions"""
4353
VIEW = "VIEW"
4454
EDIT = "EDIT"
45-
DELETE = "DELETE"
46-
SHARE = "SHARE"
4755
MANAGE = "MANAGE" # Includes permission management
4856

49-
50-
class FileVisibility(enum.Enum):
51-
"""File visibility levels"""
52-
PRIVATE = "PRIVATE" # Only owner
53-
SHARED = "SHARED" # Explicitly shared users/departments
54-
DEPARTMENT = "DEPARTMENT" # All department members
55-
PUBLIC = "PUBLIC" # Everyone (including guests)
56-
57-
5857
class AuditAction(enum.Enum):
5958
"""Audit log action types"""
6059
FILE_UPLOAD = "FILE_UPLOAD"
@@ -103,7 +102,7 @@ class Department(Base):
103102
back_populates="parent_department", cascade="all, delete-orphan"
104103
)
105104
members: Mapped[List["User"]] = relationship(back_populates="department")
106-
file_permissions: Mapped[List["FileDepartmentPermission"]] = relationship(
105+
file_permissions: Mapped[List["FilePermission"]] = relationship(
107106
back_populates="department"
108107
)
109108

@@ -145,7 +144,7 @@ class User(Base):
145144
role: Mapped[Optional["Role"]] = relationship(back_populates="users")
146145
files: Mapped[List["FileMetadata"]] = relationship(back_populates="owner")
147146
chat_sessions: Mapped[List["ChatSession"]] = relationship(back_populates="user")
148-
file_permissions: Mapped[List["FileUserPermission"]] = relationship(
147+
file_permissions: Mapped[List["FilePermission"]] = relationship(
149148
back_populates="user"
150149
)
151150
audit_logs: Mapped[List["AuditLog"]] = relationship(back_populates="user")
@@ -270,11 +269,6 @@ class FileMetadata(Base):
270269
blob_key: Mapped[str] = mapped_column(String(500), nullable=False)
271270
filename: Mapped[str] = mapped_column(String(255), nullable=False)
272271

273-
# Visibility and access control
274-
visibility: Mapped[FileVisibility] = mapped_column(
275-
SQLEnum(FileVisibility), default=FileVisibility.PRIVATE, nullable=False
276-
)
277-
278272
# Processing status
279273
status: Mapped[FileStatus] = mapped_column(SQLEnum(FileStatus), nullable=False)
280274

@@ -291,37 +285,41 @@ class FileMetadata(Base):
291285
parsed_contents: Mapped[List["ParsedContentMetadata"]] = relationship(
292286
back_populates="source_file", cascade="all, delete-orphan"
293287
)
294-
user_permissions: Mapped[List["FileUserPermission"]] = relationship(
295-
back_populates="file", cascade="all, delete-orphan"
296-
)
297-
department_permissions: Mapped[List["FileDepartmentPermission"]] = relationship(
288+
permissions: Mapped[List["FilePermission"]] = relationship(
298289
back_populates="file", cascade="all, delete-orphan"
299290
)
300291
audit_logs: Mapped[List["AuditLog"]] = relationship(back_populates="file")
301292

302293

303-
class FileUserPermission(Base):
294+
class FilePermission(Base):
304295
"""
305-
Explicit file permissions for individual users.
306-
Used when files are shared with specific users.
296+
Explicit file permissions for users/departments/all.
297+
Used when files are shared with specific users/departments/all.
307298
"""
308-
__tablename__ = 'file_user_permission'
299+
__tablename__ = 'file_permission'
309300

310301
id: Mapped[uuid.UUID] = mapped_column(
311302
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
312303
)
313304
file_id: Mapped[str] = mapped_column(
314305
String(255), ForeignKey("file_metadata.file_id"), nullable=False, index=True
315306
)
307+
308+
permission_receiver_type: Mapped["PermissionReceiverType"] = mapped_column(
309+
SQLEnum(PermissionReceiverType), nullable=False, default=PermissionReceiverType.USER
310+
)
311+
316312
user_id: Mapped[uuid.UUID] = mapped_column(
317313
UUID(as_uuid=True), ForeignKey("user.id"), nullable=False, index=True
318314
)
315+
department_id: Mapped[uuid.UUID] = mapped_column(
316+
UUID(as_uuid=True), ForeignKey("department.id"), nullable=False, index=True
317+
)
319318

320-
# Permissions
321-
can_view: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
322-
can_edit: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
323-
can_delete: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
324-
can_share: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
319+
# Permission type (view, edit)
320+
permission_type: Mapped["PermissionType"] = mapped_column(
321+
SQLEnum(PermissionType), nullable=False, default=PermissionType.VIEW
322+
)
325323

326324
# Grant information
327325
granted_by: Mapped[uuid.UUID] = mapped_column(
@@ -336,41 +334,6 @@ class FileUserPermission(Base):
336334
foreign_keys=[user_id], back_populates="file_permissions"
337335
)
338336

339-
340-
class FileDepartmentPermission(Base):
341-
"""
342-
File permissions for entire departments.
343-
Enables department-wide sharing.
344-
"""
345-
__tablename__ = 'file_department_permission'
346-
347-
id: Mapped[uuid.UUID] = mapped_column(
348-
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
349-
)
350-
file_id: Mapped[str] = mapped_column(
351-
String(255), ForeignKey("file_metadata.file_id"), nullable=False, index=True
352-
)
353-
department_id: Mapped[uuid.UUID] = mapped_column(
354-
UUID(as_uuid=True), ForeignKey("department.id"), nullable=False, index=True
355-
)
356-
357-
# Permissions
358-
can_view: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
359-
can_edit: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
360-
can_delete: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
361-
can_share: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
362-
363-
# Grant information
364-
granted_by: Mapped[uuid.UUID] = mapped_column(
365-
UUID(as_uuid=True), ForeignKey("user.id"), nullable=False
366-
)
367-
granted_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.now, nullable=False)
368-
369-
# Relationships
370-
file: Mapped["FileMetadata"] = relationship(back_populates="department_permissions")
371-
department: Mapped["Department"] = relationship(back_populates="file_permissions")
372-
373-
374337
# ==================== AUDIT LOG ====================
375338

376339
class AuditLog(Base):

0 commit comments

Comments
 (0)