Skip to content

Commit

Permalink
api.models: collect all server-side models in a single file
Browse files Browse the repository at this point in the history
api/models.py now contains all the server-side pydantic model
definitions. The common server and client-facing models are in the
analogous models.py file in kernelci-core.

Signed-off-by: Ricardo Cañuelo <[email protected]>
  • Loading branch information
Ricardo Cañuelo committed Jan 4, 2024
1 parent 8254753 commit 15f560f
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 59 deletions.
2 changes: 1 addition & 1 deletion api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from .auth import Authentication
from .db import Database
from .user_models import User, UserGroup
from .models import User, UserGroup


async def setup_admin_group(db, admin_group):
Expand Down
2 changes: 1 addition & 1 deletion api/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from fastapi_pagination.ext.motor import paginate
from motor import motor_asyncio
from kernelci.api.models import Hierarchy, Node, Regression
from .user_models import User, UserGroup
from .models import User, UserGroup


class Database:
Expand Down
7 changes: 4 additions & 3 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@
PublishEvent,
get_model_from_kind
)
from .paginator_models import PageModel
from .pubsub import PubSub, Subscription
from .pubsub import PubSub
from .user_manager import get_user_manager, create_user_manager
from .user_models import (
from .models import (
PageModel,
Subscription,
User,
UserRead,
UserCreate,
Expand Down
60 changes: 56 additions & 4 deletions api/user_models.py → api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,47 @@
# at the moment
# pylint: disable=too-few-public-methods

"""User model definitions"""

from typing import Optional
from pydantic import conlist, Field
"""Server-side model definitions"""

from typing import Optional, TypeVar
from pydantic import (
BaseModel,
conlist,
Field,
BaseSettings,
EmailStr,
)
from fastapi import Query
from fastapi_pagination import LimitOffsetPage, LimitOffsetParams
from fastapi_users.db import BeanieBaseUser
from fastapi_users import schemas
from beanie import (
Indexed,
Document,
PydanticObjectId,
)
from bson import ObjectId
from kernelci.api.models_base import DatabaseModel, ModelId


# PubSub model definitions

class Subscription(BaseModel):
"""Pub/Sub subscription object model"""
id: int = Field(
description='Subscription ID'
)
channel: str = Field(
description='Subscription channel name'
)
user: str = Field(
description=("Username of the user that created the "
"subscription (owner)")
)


# User model definitions

class UserGroup(DatabaseModel):
"""API model to group associated user accounts"""
name: str = Field(
Expand Down Expand Up @@ -69,3 +96,28 @@ class UserUpdate(schemas.BaseUserUpdate):
"""Schema for updating a user"""
username: Optional[Indexed(str, unique=True)]
groups: Optional[conlist(str, unique_items=True)]


# Pagination models

class CustomLimitOffsetParams(LimitOffsetParams):
"""Model to set custom constraint on limit
The model is required to redefine limit parameter to remove the number
validation on maximum value"""

limit: int = Query(50, ge=1, description="Page size limit")


class PageModel(LimitOffsetPage[TypeVar("T")]):
"""Model for pagination
This model is required to serialize paginated model data response"""

__params_type__ = CustomLimitOffsetParams

class Config:
"""Configuration attributes for PageNode"""
json_encoders = {
ObjectId: str,
}
34 changes: 0 additions & 34 deletions api/paginator_models.py

This file was deleted.

16 changes: 1 addition & 15 deletions api/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,10 @@
import json
from redis import asyncio as aioredis
from cloudevents.http import CloudEvent, to_json
from pydantic import BaseModel, Field
from .models import Subscription
from .config import PubSubSettings


class Subscription(BaseModel):
"""Pub/Sub subscription object model"""
id: int = Field(
description='Subscription ID'
)
channel: str = Field(
description='Subscription channel name'
)
user: str = Field(
description=("Username of the user that created the "
"subscription (owner)")
)


class PubSub:
"""Pub/Sub implementation class
Expand Down
2 changes: 1 addition & 1 deletion api/user_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
)
from beanie import PydanticObjectId
import jinja2
from .user_models import User
from .models import User
from .config import AuthSettings
from .email_sender import EmailSender

Expand Down

0 comments on commit 15f560f

Please sign in to comment.