from aiohttp import web from typing import Any, Dict, Optional from marshmallow import Schema, post_dump, fields, validate from server.core.exceptions import BaseSchemaErrorRequest from server.artist.constants import COUNTRIES_LIST_SLUG_NAME, COUNTRIES_LIST_NAME from server.track.constants import GENRES_LIST_SLUG_NAME, GENRES # Response SChemas ===================================================================================================== class GenreSchema(Schema): code = fields.String(validate=validate.ContainsOnly(GENRES_LIST_SLUG_NAME)) name = fields.String(validate=validate.ContainsOnly(GENRES)) class CountrySchema(Schema): code = fields.String(validate=validate.ContainsOnly(COUNTRIES_LIST_SLUG_NAME)) name = fields.String(validate=validate.ContainsOnly(COUNTRIES_LIST_NAME)) # Request Schemas ====================================================================================================== class BaseSchema(Schema): class Meta: strict = True def handle_error(self, exc, data, **kwargs): raise BaseSchemaErrorRequest(error_message=exc.messages, status_code=web.HTTPUnprocessableEntity.status_code) class BaseSchemaV4(Schema): class Meta: strict = True def handle_error(self, exc, data, **kwargs): raise BaseSchemaErrorRequest(error_message=exc.messages, status_code=web.HTTPUnprocessableEntity.status_code) @post_dump def check_results(self, data: Optional[Dict[Any, Any]], **kwargs) -> Optional[Dict[Any, Any]]: if not data: data = None return data class IdSchema(BaseSchema): id = fields.String(validate=validate.Length(min=1, max=15)) entity_id = fields.String(validate=validate.Length(min=1, max=15))