mirror of https://gitlab.com/litecord/litecord.git
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:
parent
c325543242
commit
65b4b28d89
|
|
@ -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/>.
|
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
|
from quart import Blueprint, current_app as app, jsonify, request
|
||||||
|
|
||||||
|
|
@ -27,6 +28,11 @@ from litecord.enums import Feature
|
||||||
bp = Blueprint('features_admin', __name__)
|
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):
|
async def _features(guild_id: int):
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'features': await app.storage.guild_features(guild_id)
|
'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):
|
async def replace_features(guild_id: int):
|
||||||
"""Replace the feature list in a guild"""
|
"""Replace the feature list in a guild"""
|
||||||
await admin_check()
|
await admin_check()
|
||||||
j = validate(await request.get_json(), FEATURES)
|
features = await _features_from_req()
|
||||||
features = j['features']
|
|
||||||
|
|
||||||
# yes, we need to pass it to a set and then to a list before
|
# yes, we need to pass it to a set and then to a list before
|
||||||
# doing anything, since the api client might just
|
# 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):
|
async def insert_features(guild_id: int):
|
||||||
"""Insert a feature on a guild."""
|
"""Insert a feature on a guild."""
|
||||||
await admin_check()
|
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 = await app.storage.guild_features(guild_id)
|
||||||
features = set(features)
|
features = set(features)
|
||||||
|
|
||||||
|
|
@ -77,8 +81,7 @@ async def insert_features(guild_id: int):
|
||||||
async def remove_feature(guild_id: int):
|
async def remove_feature(guild_id: int):
|
||||||
"""Remove a feature from a guild"""
|
"""Remove a feature from a guild"""
|
||||||
await admin_check()
|
await admin_check()
|
||||||
j = validate(await request.get_json(), FEATURES)
|
to_remove = await _features_from_req()
|
||||||
to_remove = j['features']
|
|
||||||
features = await app.storage.guild_features(guild_id)
|
features = await app.storage.guild_features(guild_id)
|
||||||
|
|
||||||
for feature in to_remove:
|
for feature in to_remove:
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ from .permissions import Permissions
|
||||||
from .types import Color
|
from .types import Color
|
||||||
from .enums import (
|
from .enums import (
|
||||||
ActivityType, StatusType, ExplicitFilter, RelationshipType,
|
ActivityType, StatusType, ExplicitFilter, RelationshipType,
|
||||||
MessageNotifications, ChannelType, VerificationLevel, Features
|
MessageNotifications, ChannelType, VerificationLevel, Feature
|
||||||
)
|
)
|
||||||
|
|
||||||
from litecord.embed.schemas import EMBED_OBJECT
|
from litecord.embed.schemas import EMBED_OBJECT
|
||||||
|
|
@ -145,9 +145,6 @@ class LitecordValidator(Validator):
|
||||||
def _validate_type_nickname(self, value: str) -> bool:
|
def _validate_type_nickname(self, value: str) -> bool:
|
||||||
return isinstance(value, str) and (len(value) < 32)
|
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,
|
def validate(reqjson: Union[Dict, List], schema: Dict,
|
||||||
raise_err: bool = True) -> Dict:
|
raise_err: bool = True) -> Dict:
|
||||||
|
|
@ -662,6 +659,6 @@ GET_MENTIONS = {
|
||||||
FEATURES = {
|
FEATURES = {
|
||||||
'features': {
|
'features': {
|
||||||
'type': 'list', 'required': True,
|
'type': 'list', 'required': True,
|
||||||
'schema': {'type': 'guild_feature'}
|
'schema': {'coerce': Feature}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue