|
|
@ -1,10 +1,61 @@ |
|
|
|
"""Service to handle user_token operations.""" |
|
|
|
import uuid |
|
|
|
from datetime import datetime |
|
|
|
from typing import Optional |
|
|
|
from typing import Optional, Dict, Callable, Any |
|
|
|
|
|
|
|
from atheneum.db import db |
|
|
|
from atheneum.model import User, UserToken |
|
|
|
from atheneum.service.serialization_service import ( |
|
|
|
BaseSerializer, |
|
|
|
register_serializer |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
class UserTokenSerializer(BaseSerializer): |
|
|
|
"""Serialize User model.""" |
|
|
|
|
|
|
|
def _serializers(self) -> Dict[str, Callable[[], Any]]: |
|
|
|
"""Define the fields and the accompanying serializer factory.""" |
|
|
|
return { |
|
|
|
'token': self.serialize_token, |
|
|
|
'note': self.serialize_note, |
|
|
|
'enabled': self.serialize_enabled, |
|
|
|
'expirationTime': self.serialize_expiration_time, |
|
|
|
'creationTime': self.serialize_creation_time, |
|
|
|
'lastUsageTime': self.serialize_last_usage_time, |
|
|
|
'version': self.serialize_version |
|
|
|
} |
|
|
|
|
|
|
|
def serialize_token(self) -> str: |
|
|
|
"""User token.""" |
|
|
|
return self.model.token |
|
|
|
|
|
|
|
def serialize_note(self) -> str: |
|
|
|
"""User token note.""" |
|
|
|
return self.model.note |
|
|
|
|
|
|
|
def serialize_enabled(self) -> bool: |
|
|
|
"""User token enabled.""" |
|
|
|
return self.model.enabled |
|
|
|
|
|
|
|
def serialize_expiration_time(self) -> datetime: |
|
|
|
"""User token expiration time.""" |
|
|
|
return self.model.expiration_time |
|
|
|
|
|
|
|
def serialize_creation_time(self) -> datetime: |
|
|
|
"""User token creation time.""" |
|
|
|
return self.model.creation_time |
|
|
|
|
|
|
|
def serialize_last_usage_time(self) -> datetime: |
|
|
|
"""User token last usage time.""" |
|
|
|
return self.model.last_usage_time |
|
|
|
|
|
|
|
def serialize_version(self) -> int: |
|
|
|
"""User token version.""" |
|
|
|
return self.model.version |
|
|
|
|
|
|
|
|
|
|
|
register_serializer(UserToken.__name__, UserTokenSerializer) |
|
|
|
|
|
|
|
|
|
|
|
def generate_token() -> uuid.UUID: |
|
|
|