forked from Github-Mirrors/canaille
refactor: add some typing to models
This commit is contained in:
parent
1fbb074cc5
commit
b6c59b3eda
3 changed files with 25 additions and 15 deletions
|
@ -2,6 +2,8 @@ import copy
|
|||
import datetime
|
||||
import typing
|
||||
import uuid
|
||||
from typing import ClassVar
|
||||
from typing import Dict
|
||||
|
||||
import canaille.core.models
|
||||
import canaille.oidc.models
|
||||
|
@ -239,8 +241,8 @@ class MemoryModel(BackendModel):
|
|||
|
||||
|
||||
class User(canaille.core.models.User, MemoryModel):
|
||||
identifier_attribute = "user_name"
|
||||
model_attributes = {
|
||||
identifier_attribute: ClassVar[str] = "user_name"
|
||||
model_attributes: ClassVar[Dict[str, str]] = {
|
||||
"groups": ("Group", "members"),
|
||||
}
|
||||
|
||||
|
@ -266,30 +268,30 @@ class User(canaille.core.models.User, MemoryModel):
|
|||
|
||||
|
||||
class Group(canaille.core.models.Group, MemoryModel):
|
||||
model_attributes = {
|
||||
model_attributes: ClassVar[Dict[str, str]] = {
|
||||
"members": ("User", "groups"),
|
||||
}
|
||||
identifier_attribute = "display_name"
|
||||
identifier_attribute: ClassVar[str] = "display_name"
|
||||
|
||||
|
||||
class Client(canaille.oidc.models.Client, MemoryModel):
|
||||
identifier_attribute = "client_id"
|
||||
model_attributes = {
|
||||
identifier_attribute: ClassVar[str] = "client_id"
|
||||
model_attributes: ClassVar[Dict[str, str]] = {
|
||||
"audience": ("Client", None),
|
||||
}
|
||||
|
||||
|
||||
class AuthorizationCode(canaille.oidc.models.AuthorizationCode, MemoryModel):
|
||||
identifier_attribute = "authorization_code_id"
|
||||
model_attributes = {
|
||||
identifier_attribute: ClassVar[str] = "authorization_code_id"
|
||||
model_attributes: ClassVar[Dict[str, str]] = {
|
||||
"client": ("Client", None),
|
||||
"subject": ("User", None),
|
||||
}
|
||||
|
||||
|
||||
class Token(canaille.oidc.models.Token, MemoryModel):
|
||||
identifier_attribute = "token_id"
|
||||
model_attributes = {
|
||||
identifier_attribute: ClassVar[str] = "token_id"
|
||||
model_attributes: ClassVar[Dict[str, str]] = {
|
||||
"client": ("Client", None),
|
||||
"subject": ("User", None),
|
||||
"audience": ("Client", None),
|
||||
|
@ -297,8 +299,8 @@ class Token(canaille.oidc.models.Token, MemoryModel):
|
|||
|
||||
|
||||
class Consent(canaille.oidc.models.Consent, MemoryModel):
|
||||
identifier_attribute = "consent_id"
|
||||
model_attributes = {
|
||||
identifier_attribute: ClassVar[str] = "consent_id"
|
||||
model_attributes: ClassVar[Dict[str, str]] = {
|
||||
"client": ("Client", None),
|
||||
"subject": ("User", None),
|
||||
}
|
||||
|
|
|
@ -46,13 +46,19 @@ class Model:
|
|||
|
||||
@classproperty
|
||||
def attributes(cls):
|
||||
return ChainMap(
|
||||
annotations = ChainMap(
|
||||
*(
|
||||
typing.get_type_hints(klass)
|
||||
for klass in reversed(cls.__mro__)
|
||||
if issubclass(klass, Model)
|
||||
)
|
||||
)
|
||||
# only keep types that are not typing.ClassVar
|
||||
return {
|
||||
key: value
|
||||
for key, value in annotations.items()
|
||||
if typing.get_origin(value) is not typing.ClassVar
|
||||
}
|
||||
|
||||
|
||||
class BackendModel:
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
import datetime
|
||||
from typing import ClassVar
|
||||
from typing import List
|
||||
|
||||
from authlib.oauth2.rfc6749 import AuthorizationCodeMixin
|
||||
from authlib.oauth2.rfc6749 import ClientMixin
|
||||
|
@ -14,14 +16,14 @@ from .basemodels import Token as BaseToken
|
|||
|
||||
|
||||
class Client(BaseClient, ClientMixin):
|
||||
client_info_attributes = [
|
||||
client_info_attributes: ClassVar[List[str]] = [
|
||||
"client_id",
|
||||
"client_secret",
|
||||
"client_id_issued_at",
|
||||
"client_secret_expires_at",
|
||||
]
|
||||
|
||||
client_metadata_attributes = [
|
||||
client_metadata_attributes: ClassVar[List[str]] = [
|
||||
"client_name",
|
||||
"contacts",
|
||||
"client_uri",
|
||||
|
|
Loading…
Reference in a new issue