WIP: full refactor

This commit is contained in:
norohind committed 2021-12-05 23:21:07 +03:00
1 parent bbeec2e3fe
commit 226382f28a
6 files changed
+90 -6

No files matched your search

+10 -2
View File
@@ -5,6 +5,7 @@ from . import exceptions
import base64
import os
import time
from typing import Union
import config
from EDMCLogging import get_main_logger
@@ -92,9 +93,13 @@ class CAPIAuthorizer:
return msg
def get_token_by_state(self, state: str) -> dict:
def get_token_by_state(self, state: str) -> Union[dict, None]:
self.refresh_by_state(state)
row = self.model.get_token_for_user(state)
if row is None:
return None
row['expires_over'] = int(row['expires_on']) - int(time.time())
return row
@@ -113,7 +118,7 @@ class CAPIAuthorizer:
if row is None:
# No such state in DB
msg['status'] = 'error'
msg['message'] = 'No such state in DB'
msg['description'] = 'No such state in DB'
raise exceptions.RefreshFail(msg['description'], msg['status'], state)
msg['state'] = state
@@ -163,5 +168,8 @@ class CAPIAuthorizer:
def list_all_users(self) -> list[dict]:
return self.model.list_all_records()
def cleanup_orphans(self) -> None:
self.model.cleanup_orphans_records()
capi_authorizer = CAPIAuthorizer(model.Model())
+1 -1
View File
@@ -3,4 +3,4 @@ class RefreshFail(Exception):
self.message = message
self.status = status
self.state = state
super().__init__(self.message + ' for ' + self.state)
super().__init__(str(self.message) + ' for ' + str(self.state))
+4
View File
@@ -115,3 +115,7 @@ class Model:
def list_all_records(self) -> list:
return self.db.execute(sqlite_requests.select_nickname_state_all).fetchall()
def cleanup_orphans_records(self) -> None:
with self.db:
self.db.execute(sqlite_requests.del_orphans)
+2
View File
@@ -46,3 +46,5 @@ get_token_for_user = """select
from authorizations where state = :state;"""
select_nickname_state_all = """select nickname, state from authorizations where nickname is not null;"""
del_orphans = """delete from authorizations where nickname is null;"""