🔀 Added delete tab expiry
This commit is contained in:
parent
4f12f6ae6d
commit
0c886205fe
|
@ -55,6 +55,7 @@ async def grant_tab(request: RequestSchema):
|
|||
|
||||
|
||||
async def get_profile(schema: RequestSchema) -> UserSchema:
|
||||
await database.delete_expired_tab_users()
|
||||
uuid = await database.get_uuid_by_username(schema)
|
||||
if not uuid:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from sqlalchemy import select
|
||||
from sqlalchemy import select, delete
|
||||
from sqlalchemy.ext.asyncio import create_async_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from db import LuckpermsPlayer, LitebansBan, LuckpermsUserPermission, TabUser
|
||||
|
@ -6,7 +6,7 @@ from db.schemas import MySQLConfig, UserSchema, RequestSchema, TabSchema
|
|||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from aiocache import cached, SimpleMemoryCache
|
||||
from aiocache.serializers import PickleSerializer
|
||||
|
||||
import datetime
|
||||
|
||||
class Database:
|
||||
def __init__(self, uri: MySQLConfig):
|
||||
|
@ -92,3 +92,14 @@ class Database:
|
|||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
async def delete_expired_tab_users(self):
|
||||
async with self.AsyncSessionLocal() as session:
|
||||
async with session.begin():
|
||||
current_timestamp = int((datetime.datetime.utcnow()).timestamp())
|
||||
await session.execute(
|
||||
delete(TabUser)
|
||||
.where(TabUser.expiry < current_timestamp)
|
||||
.where(TabUser.expiry != 0)
|
||||
)
|
||||
await session.commit()
|
|
@ -1,4 +1,4 @@
|
|||
from pydantic import BaseModel, PositiveInt, computed_field, field_validator
|
||||
from pydantic import BaseModel, computed_field, field_validator, NonNegativeInt
|
||||
from .TabSchema import TabSchema
|
||||
import datetime
|
||||
import re
|
||||
|
@ -6,7 +6,7 @@ import re
|
|||
|
||||
class RequestSchema(BaseModel):
|
||||
username: str
|
||||
expiry: PositiveInt = 1
|
||||
expiry: NonNegativeInt = 1
|
||||
tab: TabSchema = TabSchema()
|
||||
|
||||
@field_validator('username')
|
||||
|
@ -15,7 +15,7 @@ class RequestSchema(BaseModel):
|
|||
raise ValueError("Invalid username format")
|
||||
return value
|
||||
|
||||
@computed_field(return_type=PositiveInt)
|
||||
@computed_field(return_type=NonNegativeInt)
|
||||
def validated_expiry(self):
|
||||
if self.expiry > 10**5:
|
||||
try:
|
||||
|
|
Loading…
Reference in New Issue