Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def load_token(self, server, pan_user):
# type: (str, str) -> Optional[str]
"""Load a sync token for a pan user.
Returns the sync token if one is found.
"""
server = Servers.get(name=server)
user = ServerUsers.get(server=server, user_id=pan_user)
token = PanSyncTokens.get_or_none(user=user)
if token:
return token.token
return None
@attr.s
class ClientInfo:
user_id = attr.ib(type=str)
access_token = attr.ib(type=str)
@attr.s
class PanStore:
store_path = attr.ib(type=str)
database_name = attr.ib(type=str, default="pan.db")
database = attr.ib(type=SqliteDatabase, init=False)
database_path = attr.ib(type=str, init=False)
models = [
Accounts,
AccessTokens,
Servers,
ServerUsers,
DeviceKeys,
DeviceTrustState,
PanSyncTokens,
PanFetcherTasks,
]
def __attrs_post_init__(self):
self.database_path = os.path.join(
os.path.abspath(self.store_path), self.database_name
)
self.database = self._create_database()
self.database.connect()
with self.database.bind_ctx(self.models):
def save_server_user(self, server_name, user_id):
# type: (str, str) -> None
server, _ = Servers.get_or_create(name=server_name)
ServerUsers.insert(
user_id=user_id, server=server
).on_conflict_ignore().execute()
def load_users(self, server_name):
# type: (str) -> List[Tuple[str, str]]
users = []
server = Servers.get_or_none(Servers.name == server_name)
if not server:
return []
server_users = []
for u in server.users:
server_users.append(u.user_id)
query = Accounts.select(Accounts.user_id, Accounts.device_id).where(
Accounts.user_id.in_(server_users)
)
for account in query:
users.append((account.user_id, account.device_id))
account = ForeignKeyField(
model=Accounts, primary_key=True, backref="access_token", on_delete="CASCADE"
)
class Servers(Model):
name = TextField()
class Meta:
constraints = [SQL("UNIQUE(name)")]
class ServerUsers(Model):
user_id = TextField()
server = ForeignKeyField(
model=Servers, column_name="server_id", backref="users", on_delete="CASCADE"
)
class Meta:
constraints = [SQL("UNIQUE(user_id,server_id)")]
class PanSyncTokens(Model):
token = TextField()
user = ForeignKeyField(model=ServerUsers, column_name="user_id")
class Meta:
constraints = [SQL("UNIQUE(user_id)")]
class PanFetcherTasks(Model):
user = ForeignKeyField(