From 65b4b28d89ceabb61288a4420f4f18c39cce2906 Mon Sep 17 00:00:00 2001 From: Luna Date: Mon, 11 Mar 2019 01:43:11 -0300 Subject: [PATCH] schemas: use coerce to Feature instead of guild_features type cleaner to write code for. - admin_api.features: support that --- litecord/blueprints/admin_api/features.py | 15 +++++++++------ litecord/schemas.py | 7 ++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/litecord/blueprints/admin_api/features.py b/litecord/blueprints/admin_api/features.py index b786692..40a4bf1 100644 --- a/litecord/blueprints/admin_api/features.py +++ b/litecord/blueprints/admin_api/features.py @@ -16,6 +16,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . """ +from typing import List from quart import Blueprint, current_app as app, jsonify, request @@ -27,6 +28,11 @@ from litecord.enums import Feature bp = Blueprint('features_admin', __name__) +async def _features_from_req() -> List[str]: + j = validate(await request.get_json(), FEATURES) + return [feature.value for feature in j['features']] + + async def _features(guild_id: int): return jsonify({ 'features': await app.storage.guild_features(guild_id) @@ -45,8 +51,7 @@ async def _update_features(guild_id: int, features: list): async def replace_features(guild_id: int): """Replace the feature list in a guild""" await admin_check() - j = validate(await request.get_json(), FEATURES) - features = j['features'] + features = await _features_from_req() # yes, we need to pass it to a set and then to a list before # doing anything, since the api client might just @@ -59,9 +64,8 @@ async def replace_features(guild_id: int): async def insert_features(guild_id: int): """Insert a feature on a guild.""" await admin_check() - j = validate(await request.get_json(), FEATURES) + to_add = await _features_from_req() - to_add = j['features'] features = await app.storage.guild_features(guild_id) features = set(features) @@ -77,8 +81,7 @@ async def insert_features(guild_id: int): async def remove_feature(guild_id: int): """Remove a feature from a guild""" await admin_check() - j = validate(await request.get_json(), FEATURES) - to_remove = j['features'] + to_remove = await _features_from_req() features = await app.storage.guild_features(guild_id) for feature in to_remove: diff --git a/litecord/schemas.py b/litecord/schemas.py index 54bed7c..a746bfe 100644 --- a/litecord/schemas.py +++ b/litecord/schemas.py @@ -28,7 +28,7 @@ from .permissions import Permissions from .types import Color from .enums import ( ActivityType, StatusType, ExplicitFilter, RelationshipType, - MessageNotifications, ChannelType, VerificationLevel, Features + MessageNotifications, ChannelType, VerificationLevel, Feature ) from litecord.embed.schemas import EMBED_OBJECT @@ -145,9 +145,6 @@ class LitecordValidator(Validator): def _validate_type_nickname(self, value: str) -> bool: return isinstance(value, str) and (len(value) < 32) - def _validate_type_guild_feature(self, value: str) -> bool: - return value in Features.values() - def validate(reqjson: Union[Dict, List], schema: Dict, raise_err: bool = True) -> Dict: @@ -662,6 +659,6 @@ GET_MENTIONS = { FEATURES = { 'features': { 'type': 'list', 'required': True, - 'schema': {'type': 'guild_feature'} + 'schema': {'coerce': Feature} } }