1616
1717
1818class CognitoInconsistencyError (Exception ):
19- """ An exception indicating that the state in the database and state in cognito have
19+ """An exception indicating that the state in the database and state in cognito have
2020 diverged.
2121 """
2222
@@ -46,7 +46,9 @@ def __str__(self) -> str:
4646 return str (self .username )
4747
4848 username = CustomSlugField (_ (_context , "User name" ), unique = True , max_length = 100 )
49- user_id = models .CharField (_ (_context , "User ID" ), unique = True , default = generate_short_id )
49+ user_id = models .CharField (
50+ _ (_context , "User ID" ), unique = True , default = generate_short_id
51+ )
5052 created = models .DateTimeField (_ (_context , "Created" ), auto_now_add = True )
5153 updated = models .DateTimeField (_ (_context , "Updated" ), auto_now = True )
5254 first_name = models .CharField (_ (_context , "First name" ))
@@ -70,36 +72,46 @@ def save(
7072 force_insert : bool | tuple [ModelBase , ...] = False ,
7173 force_update : bool = False ,
7274 using : str | None = None ,
73- update_fields : Iterable [str ] | None = None
75+ update_fields : Iterable [str ] | None = None ,
7476 ) -> None :
7577 """Validates the model before writing it to the database and syncs with cognito."""
7678
7779 self .full_clean ()
7880 client = Client ()
7981 with transaction .atomic ():
8082 if self ._state .adding :
81- super ().save (force_insert = True , using = using , update_fields = update_fields )
83+ super ().save (
84+ force_insert = True , using = using , update_fields = update_fields
85+ )
8286 if not client .create_user (self .user_id , self .username , self .email ):
83- logger .critical ("User %s already exists in cognito, not created" , self .user_id )
87+ logger .critical (
88+ "User %s already exists in cognito, not created" , self .user_id
89+ )
8490 raise CognitoInconsistencyError ()
8591 else :
8692 User .all_objects .select_for_update ().filter (pk = self .pk ).get ()
87- super ().save (force_update = True , using = using , update_fields = update_fields )
93+ super ().save (
94+ force_update = True , using = using , update_fields = update_fields
95+ )
8896 if not client .update_user (self .user_id , self .username , self .email ):
89- logger .critical ("User %s does not exist in cognito, not updated" , self .user_id )
97+ logger .critical (
98+ "User %s does not exist in cognito, not updated" , self .user_id
99+ )
90100 raise CognitoInconsistencyError ()
91101
92- def delete (self ,
93- using : str | None = None ,
94- keep_parents : bool = False ) -> tuple [int , dict [str , int ]]:
102+ def delete (
103+ self , using : str | None = None , keep_parents : bool = False
104+ ) -> tuple [int , dict [str , int ]]:
95105 """Deletes the user from the database and cognito."""
96106
97107 client = Client ()
98108 with transaction .atomic ():
99109 User .all_objects .select_for_update ().filter (pk = self .pk ).get ()
100110 result = super ().delete (using = using , keep_parents = keep_parents )
101111 if not client .delete_user (self .user_id ):
102- logger .critical ("User %s does not exist in cognito, not deleted" , self .user_id )
112+ logger .critical (
113+ "User %s does not exist in cognito, not deleted" , self .user_id
114+ )
103115 raise CognitoInconsistencyError ()
104116 return result
105117
@@ -113,5 +125,7 @@ def disable(self) -> None:
113125 self .deleted_at = timezone .now ()
114126 super ().save (force_update = True )
115127 if not client .disable_user (self .user_id ):
116- logger .critical ("User %s does not exist in cognito, not disabled" , self .user_id )
128+ logger .critical (
129+ "User %s does not exist in cognito, not disabled" , self .user_id
130+ )
117131 raise CognitoInconsistencyError ()
0 commit comments