3
3
4
4
5
5
class PostgresUserRole (models .Model ):
6
- """
7
- Postgres model for user roles.
8
- """
9
-
10
- # MongoDB ObjectId as string for reference
11
6
mongo_id = models .CharField (max_length = 24 , unique = True , null = True , blank = True )
12
7
13
- # User role fields
14
- user_mongo_id = models .CharField (max_length = 24 ) # MongoDB ObjectId as string
15
- role_mongo_id = models .CharField (max_length = 24 ) # MongoDB ObjectId as string
16
- team_mongo_id = models .CharField (max_length = 24 , null = True , blank = True ) # MongoDB ObjectId as string
17
-
18
- # Timestamps
8
+ user_id = models .CharField (max_length = 24 )
9
+ role_name = models .CharField (max_length = 50 )
10
+ scope = models .CharField (max_length = 20 )
11
+ team_id = models .CharField (max_length = 24 , null = True , blank = True )
12
+ is_active = models .BooleanField (default = True )
19
13
created_at = models .DateTimeField (default = timezone .now )
20
- updated_at = models .DateTimeField ( null = True , blank = True )
14
+ created_by = models .CharField ( max_length = 24 , default = "system" )
21
15
22
- # References
23
- created_by = models .CharField (max_length = 24 ) # MongoDB ObjectId as string
24
- updated_by = models .CharField (max_length = 24 , null = True , blank = True ) # MongoDB ObjectId as string
25
-
26
- # Sync metadata
27
16
last_sync_at = models .DateTimeField (auto_now = True )
28
17
sync_status = models .CharField (
29
18
max_length = 20 ,
@@ -38,20 +27,16 @@ class PostgresUserRole(models.Model):
38
27
39
28
class Meta :
40
29
db_table = "postgres_user_roles"
41
- unique_together = ["user_mongo_id " , "role_mongo_id " , "team_mongo_id " ]
30
+ unique_together = ["user_id " , "role_name " , "scope" , "team_id " ]
42
31
indexes = [
43
32
models .Index (fields = ["mongo_id" ]),
44
- models .Index (fields = ["user_mongo_id" ]),
45
- models .Index (fields = ["role_mongo_id" ]),
46
- models .Index (fields = ["team_mongo_id" ]),
33
+ models .Index (fields = ["user_id" ]),
34
+ models .Index (fields = ["role_name" ]),
35
+ models .Index (fields = ["scope" ]),
36
+ models .Index (fields = ["team_id" ]),
37
+ models .Index (fields = ["is_active" ]),
47
38
models .Index (fields = ["sync_status" ]),
48
39
]
49
40
50
41
def __str__ (self ):
51
- return f"User { self .user_mongo_id } has Role { self .role_mongo_id } "
52
-
53
- def save (self , * args , ** kwargs ):
54
- if not self .pk : # New instance
55
- self .created_at = timezone .now ()
56
- self .updated_at = timezone .now ()
57
- super ().save (* args , ** kwargs )
42
+ return f"User { self .user_id } has Role { self .role_name } ({ self .scope } )"
0 commit comments