1- from django .db import models
21import base64
32import json
43import logging
54import uuid
6-
75from enum import Enum
6+
87from cryptography .fernet import Fernet
98from cryptography .hazmat .backends import default_backend
109from cryptography .hazmat .primitives import hashes
1110from cryptography .hazmat .primitives .kdf .pbkdf2 import PBKDF2HMAC
1211from django .conf import settings
1312from django .contrib .auth .models import User
1413from django .db import models
15- from rest_framework .authtoken .models import Token
1614from django .utils .module_loading import import_string
1715from rest_framework .authtoken .models import Token
1816
@@ -152,7 +150,7 @@ def use_custom_embedding(self):
152150
153151 @property
154152 def connections (self ):
155- return {k : json .loads (self .decrypt_value (v )) for k , v in self ._connections .items ()}
153+ return {k : json .loads (self .decrypt_value (v )) for k , v in self ._connections .items ()} if self . _connections else {}
156154
157155 def get_connection (self , id ):
158156 return json .loads (self .decrypt_value (self ._connections .get (id ))) if self ._connections and self ._connections .get (id ) else None
@@ -161,18 +159,17 @@ def add_connection(self, connection):
161159 connection_id = connection ['id' ] if 'id' in connection else str (
162160 uuid .uuid4 ())
163161 connection_json = json .dumps (connection )
162+ if not self ._connections :
163+ self ._connections = {}
164164 self ._connections [connection_id ] = self .encrypt_value (
165165 connection_json ).decode ('utf-8' )
166166 self .save (update_fields = ['_connections' ])
167167
168168 def delete_connection (self , id ):
169- if id in self ._connections :
169+ if self . _connections and id in self ._connections :
170170 del self ._connections [id ]
171171 self .save (update_fields = ['_connections' ])
172-
173- def get_all_connections (self ):
174- return [json .loads (self .decrypt_value (v )) for v in self ._connections .values ()]
175-
172+
176173 def get_connection_by_type (self , connection_type_slug ):
177174 return [json .loads (self .decrypt_value (v )) for v in self ._connections .values () if json .loads (self .decrypt_value (v ))['connection_type_slug' ] == connection_type_slug ]
178175
0 commit comments