26 lines
689 B
Python
26 lines
689 B
Python
|
import secrets
|
||
|
from database import db
|
||
|
from models import Session, User, Organization
|
||
|
|
||
|
|
||
|
class SessionService:
|
||
|
@staticmethod
|
||
|
def create_session(user: User, org: Organization) -> Session:
|
||
|
session = Session(
|
||
|
user_id=user.id,
|
||
|
org_id=org.id,
|
||
|
token=secrets.token_hex(128)
|
||
|
)
|
||
|
db.add(session)
|
||
|
db.commit()
|
||
|
db.refresh(session)
|
||
|
return session
|
||
|
|
||
|
@staticmethod
|
||
|
def get_session_by_token(token: str) -> Session | None:
|
||
|
return db.query(Session).filter(Session.token == token).first()
|
||
|
|
||
|
@staticmethod
|
||
|
def delete_session(session: Session) -> None:
|
||
|
db.delete(session)
|
||
|
db.commit()
|