schemas: use coerce to Feature instead of guild_features type

cleaner to write code for.

 - admin_api.features: support that
This commit is contained in:
Luna 2019-03-11 01:43:11 -03:00
parent c325543242
commit 65b4b28d89
2 changed files with 11 additions and 11 deletions

View File

@ -16,6 +16,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
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:

View File

@ -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}
}
}