🚨 Python Black commit
This commit is contained in:
parent
ff8384d2c5
commit
b07df29c9c
3 changed files with 30 additions and 29 deletions
|
|
@ -64,9 +64,7 @@ class TokenType(str, Enum):
|
||||||
refresh = "refresh"
|
refresh = "refresh"
|
||||||
|
|
||||||
|
|
||||||
def claims(
|
def claims(token_type: TokenType, user: User, groups: List[Group] = []) -> Claims:
|
||||||
token_type: TokenType, user: User, groups: List[Group] = []
|
|
||||||
) -> Claims:
|
|
||||||
return Claims(
|
return Claims(
|
||||||
type=token_type,
|
type=token_type,
|
||||||
sub=str(user.id),
|
sub=str(user.id),
|
||||||
|
|
@ -83,7 +81,7 @@ def claims(
|
||||||
|
|
||||||
def create_token(claims: Claims, expires_in: int) -> str:
|
def create_token(claims: Claims, expires_in: int) -> str:
|
||||||
to_encode = claims.model_dump()
|
to_encode = claims.model_dump()
|
||||||
to_encode['exp'] = int(time.time()) + expires_in
|
to_encode["exp"] = int(time.time()) + expires_in
|
||||||
token = jwt.encode(to_encode, JWT_SECRET, algorithm=JWT_ALGORITHM)
|
token = jwt.encode(to_encode, JWT_SECRET, algorithm=JWT_ALGORITHM)
|
||||||
if isinstance(token, bytes):
|
if isinstance(token, bytes):
|
||||||
token = token.decode("utf-8")
|
token = token.decode("utf-8")
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,9 @@ class Team(Model):
|
||||||
id: UUID = field.data.UUIDField(primary_key=True)
|
id: UUID = field.data.UUIDField(primary_key=True)
|
||||||
name: str = field.TextField()
|
name: str = field.TextField()
|
||||||
members: field.ManyToManyRelation[User] = field.ManyToManyField(
|
members: field.ManyToManyRelation[User] = field.ManyToManyField(
|
||||||
'ohmyapi_auth.User',
|
"ohmyapi_auth.User",
|
||||||
related_name="tournament_teams",
|
related_name="tournament_teams",
|
||||||
through='user_tournament_teams',
|
through="user_tournament_teams",
|
||||||
)
|
)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|
@ -32,19 +32,19 @@ class Event(Model):
|
||||||
id: UUID = field.data.UUIDField(primary_key=True)
|
id: UUID = field.data.UUIDField(primary_key=True)
|
||||||
name: str = field.TextField()
|
name: str = field.TextField()
|
||||||
tournament: field.ForeignKeyRelation[Tournament] = field.ForeignKeyField(
|
tournament: field.ForeignKeyRelation[Tournament] = field.ForeignKeyField(
|
||||||
'ohmyapi_demo.Tournament',
|
"ohmyapi_demo.Tournament",
|
||||||
related_name='events',
|
related_name="events",
|
||||||
)
|
)
|
||||||
participants: field.ManyToManyRelation[Team] = field.ManyToManyField(
|
participants: field.ManyToManyRelation[Team] = field.ManyToManyField(
|
||||||
'ohmyapi_demo.Team',
|
"ohmyapi_demo.Team",
|
||||||
related_name='events',
|
related_name="events",
|
||||||
through='event_team',
|
through="event_team",
|
||||||
)
|
)
|
||||||
modified: datetime = field.DatetimeField(auto_now=True)
|
modified: datetime = field.DatetimeField(auto_now=True)
|
||||||
prize: Decimal = field.DecimalField(max_digits=10, decimal_places=2, null=True)
|
prize: Decimal = field.DecimalField(max_digits=10, decimal_places=2, null=True)
|
||||||
|
|
||||||
class Schema:
|
class Schema:
|
||||||
exclude = ['tournament_id']
|
exclude = ["tournament_id"]
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
|
||||||
|
|
@ -11,37 +11,41 @@ from typing import List
|
||||||
router = APIRouter(prefix="/tournemant")
|
router = APIRouter(prefix="/tournemant")
|
||||||
|
|
||||||
|
|
||||||
@router.get("/",
|
@router.get(
|
||||||
tags=["tournament"],
|
"/", tags=["tournament"], response_model=List[models.Tournament.Schema.model]
|
||||||
response_model=List[models.Tournament.Schema.model])
|
)
|
||||||
async def list():
|
async def list():
|
||||||
"""List all tournaments."""
|
"""List all tournaments."""
|
||||||
return await models.Tournament.Schema.model.from_queryset(Tournament.all())
|
return await models.Tournament.Schema.model.from_queryset(Tournament.all())
|
||||||
|
|
||||||
|
|
||||||
@router.post("/",
|
@router.post("/", tags=["tournament"], status_code=HTTPStatus.CREATED)
|
||||||
tags=["tournament"],
|
|
||||||
status_code=HTTPStatus.CREATED)
|
|
||||||
async def post(tournament: models.Tournament.Schema.readonly):
|
async def post(tournament: models.Tournament.Schema.readonly):
|
||||||
"""Create tournament."""
|
"""Create tournament."""
|
||||||
return await models.Tournament.Schema.model.from_queryset(models.Tournament.create(**tournament.model_dump()))
|
return await models.Tournament.Schema.model.from_queryset(
|
||||||
|
models.Tournament.create(**tournament.model_dump())
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{id}",
|
@router.get("/{id}", tags=["tournament"], response_model=models.Tournament.Schema.model)
|
||||||
tags=["tournament"],
|
|
||||||
response_model=models.Tournament.Schema.model)
|
|
||||||
async def get(id: str):
|
async def get(id: str):
|
||||||
"""Get tournament by id."""
|
"""Get tournament by id."""
|
||||||
return await models.Tournament.Schema.model.from_queryset(models.Tournament.get(id=id))
|
return await models.Tournament.Schema.model.from_queryset(
|
||||||
|
models.Tournament.get(id=id)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.put("/{id}",
|
@router.put(
|
||||||
tags=["tournament"],
|
"/{id}",
|
||||||
response_model=models.Tournament.Schema.model,
|
tags=["tournament"],
|
||||||
status_code=HTTPStatus.ACCEPTED)
|
response_model=models.Tournament.Schema.model,
|
||||||
|
status_code=HTTPStatus.ACCEPTED,
|
||||||
|
)
|
||||||
async def put(tournament: models.Tournament.Schema.model):
|
async def put(tournament: models.Tournament.Schema.model):
|
||||||
"""Update tournament."""
|
"""Update tournament."""
|
||||||
return await models.Tournament.Schema.model.from_queryset(models.Tournament.update(**tournament.model_dump()))
|
return await models.Tournament.Schema.model.from_queryset(
|
||||||
|
models.Tournament.update(**tournament.model_dump())
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/{id}", tags=["tournament"])
|
@router.delete("/{id}", tags=["tournament"])
|
||||||
|
|
@ -51,4 +55,3 @@ async def delete(id: str):
|
||||||
return await tournament.delete()
|
return await tournament.delete()
|
||||||
except DoesNotExist:
|
except DoesNotExist:
|
||||||
raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail="not found")
|
raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail="not found")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue