diff --git a/newsroom/oauth_clients/clients_async.py b/newsroom/oauth_clients/clients_async.py index c191f7d53..fcaea1d1a 100644 --- a/newsroom/oauth_clients/clients_async.py +++ b/newsroom/oauth_clients/clients_async.py @@ -1,18 +1,24 @@ -from content_api import MONGO_PREFIX -from typing import Optional, Annotated, List, Dict, Union -from superdesk.core.resources import ResourceModel, ResourceConfig, MongoResourceConfig +import logging +from pydantic import Field +from datetime import datetime +from typing import Optional, Annotated, List, Dict + + +from superdesk.core.resources import ResourceModel, ResourceConfig, MongoResourceConfig, validators from superdesk.core.resources.service import AsyncResourceService from superdesk.core.web import EndpointGroup -from pydantic import Field -import logging -from bson import ObjectId + + +from content_api import MONGO_PREFIX class ClientResource(ResourceModel): - id: Annotated[Union[str, ObjectId], Field(alias="_id")] = None - name: str + name: Annotated[ + Optional[str], + validators.validate_iunique_value_async(resource_name="oauth_clients", field_name="name"), + ] password: str - last_active: Optional[str] = None + last_active: Optional[datetime] = None etag: Annotated[Optional[str], Field(alias="_etag")] = None @@ -21,7 +27,7 @@ class ClientService(AsyncResourceService[ClientResource]): resource_name = "oauth_clients" - async def get_all_client(self) -> List[Dict]: + async def get_all_clients(self) -> List[Dict]: try: # Collect all items asynchronously clients = [client async for client in self.get_all()] diff --git a/newsroom/oauth_clients/views.py b/newsroom/oauth_clients/views.py index 39d7421ae..b06c83e4a 100644 --- a/newsroom/oauth_clients/views.py +++ b/newsroom/oauth_clients/views.py @@ -16,7 +16,7 @@ async def get_settings_data(): - data = await ClientService().get_all_client() + data = await ClientService().get_all_clients() return { "oauth_clients": data, } @@ -54,6 +54,7 @@ async def create(request: Request) -> Response: password = gen_password() doc = { + "_id": ObjectId(), "name": client.get("name"), "password": bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode(), }