aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar D0rs4n <[email protected]>2021-12-05 16:09:56 +0100
committerGravatar D0rs4n <[email protected]>2021-12-18 18:02:12 +0100
commit8ab32d7820b57b9f3edb61d4bd93864b6037502b (patch)
tree58fbfb9d4d6f48b136b598658a7e22d69a704d14
parentFix linting (diff)
Adjust Filter JSON Schema
From now on the Serializer will have a different JSON representation than the table schema itself, conforming to the format needed on the bot-side.
-rw-r--r--pydis_site/apps/api/serializers.py30
-rw-r--r--pydis_site/apps/api/viewsets/bot/filters.py127
2 files changed, 127 insertions, 30 deletions
diff --git a/pydis_site/apps/api/serializers.py b/pydis_site/apps/api/serializers.py
index c82b0797..864ab52e 100644
--- a/pydis_site/apps/api/serializers.py
+++ b/pydis_site/apps/api/serializers.py
@@ -133,6 +133,17 @@ REQUIRED_FOR_FILTER_LIST_SETTINGS = (
'allowed_categories',
)
+# Required fields for custom JSON representation purposes
+BASE_FIELDS = ('id', 'content', 'description', 'additional_field')
+BASE_SETTINGS_FIELDS = ("ping_type", "dm_ping_type", "bypass_roles", "filter_dm")
+INFRACTION_FIELDS = ("infraction_type", "infraction_reason", "infraction_duration")
+CHANNEL_SCOPE_FIELDS = (
+ "allowed_channels",
+ "allowed_categories",
+ "disallowed_channels",
+ "disallowed_categories"
+)
+
SETTINGS_FIELDS = ALWAYS_OPTIONAL_SETTINGS + REQUIRED_FOR_FILTER_LIST_SETTINGS
@@ -181,6 +192,25 @@ class FilterSerializer(ModelSerializer):
'allowed_categories': {'allow_empty': True, 'allow_null': True, 'required': False},
}
+ def to_representation(self, instance: Filter) -> dict:
+ """
+ Provides a custom JSON representation to the Filter Serializers
+
+ That does not affect how the Serializer works in general.
+ """
+ item = Filter.objects.get(id=instance.id)
+ schema_settings = {
+ "settings":
+ {name: getattr(item, name) for name in BASE_SETTINGS_FIELDS}
+ | {"infraction": {name: getattr(item, name) for name in INFRACTION_FIELDS}}
+ | {"channel_scope": {name: getattr(item, name) for name in CHANNEL_SCOPE_FIELDS}}
+ }
+
+ schema_base = {name: getattr(item, name) for name in BASE_FIELDS} | \
+ {"filter_list": item.filter_list.id}
+
+ return schema_base | schema_settings
+
class FilterListSerializer(ModelSerializer):
"""A class providing (de-)serialization of `FilterList` instances."""
diff --git a/pydis_site/apps/api/viewsets/bot/filters.py b/pydis_site/apps/api/viewsets/bot/filters.py
index 5b21de26..64329ebe 100644
--- a/pydis_site/apps/api/viewsets/bot/filters.py
+++ b/pydis_site/apps/api/viewsets/bot/filters.py
@@ -27,11 +27,28 @@ class FilterListViewSet(ModelViewSet):
... "filters": [
... {
... "id": 1,
+ ... "filter_list": 1
... "content": "267624335836053506",
... "description": "Python Discord",
... "additional_field": None,
- ... "override": 1,
- ... "filter_list": 1
+ ... "settings": {
+ ... "ping_type": None,
+ ... "dm_ping_type": None
+ ... "bypass_roles": None
+ ... "filter_dm": None,
+ ... "infraction": {
+ ... "infraction_type": None,
+ ... "infraction_reason": "",
+ ... "infraction_duration": None
+ ... },
+ ... "channel_scope": {
+ ... "allowed_channels": None,
+ ... "allowed_categories": None,
+ ... "disallowed_channels": None,
+ ... "disallowed_categories": None
+ ... }
+ ... }
+ ...
... },
... ...
... ],
@@ -48,23 +65,40 @@ class FilterListViewSet(ModelViewSet):
Returns a specific FilterList item from the database.
#### Response format
- >>> {
- ... "id": 1,
- ... "name": "guild_invite",
- ... "list_type": 1,
- ... "filters": [
- ... {
- ... "id": 1,
- ... "content": "267624335836053506",
- ... "description": "Python Discord",
- ... "additional_field": None,
- ... "override": 1,
- ... "filter_list": 1
- ... },
- ... ...
- ... ],
- ... "default_settings": 1
- ... }
+ ... {
+ ... "id": 1,
+ ... "name": "guild_invite",
+ ... "list_type": 1,
+ ... "filters": [
+ ... {
+ ... "id": 1,
+ ... "filter_list": 1
+ ... "content": "267624335836053506",
+ ... "description": "Python Discord",
+ ... "additional_field": None,
+ ... "settings": {
+ ... "ping_type": None,
+ ... "dm_ping_type": None
+ ... "bypass_roles": None
+ ... "filter_dm": None,
+ ... "infraction": {
+ ... "infraction_type": None,
+ ... "infraction_reason": "",
+ ... "infraction_duration": None
+ ... },
+ ... "channel_scope": {
+ ... "allowed_channels": None,
+ ... "allowed_categories": None,
+ ... "disallowed_channels": None,
+ ... "disallowed_categories": None
+ ... }
+ ... }
+ ...
+ ... },
+ ... ...
+ ... ],
+ ... "default_settings": 1
+ ... }
#### Status codes
- 200: returned on success
@@ -93,12 +127,28 @@ class FilterViewSet(ModelViewSet):
#### Response format
>>> [
... {
- ... "id": 1,
- ... "content": "267624335836053506",
- ... "description": "Python Discord",
- ... "additional_field": None,
- ... "override": 1,
- ... "filter_list": 1
+ ... "id": 1,
+ ... "filter_list": 1
+ ... "content": "267624335836053506",
+ ... "description": "Python Discord",
+ ... "additional_field": None,
+ ... "settings": {
+ ... "ping_type": None,
+ ... "dm_ping_type": None
+ ... "bypass_roles": None
+ ... "filter_dm": None,
+ ... "infraction": {
+ ... "infraction_type": None,
+ ... "infraction_reason": "",
+ ... "infraction_duration": None
+ ... },
+ ... "channel_scope": {
+ ... "allowed_channels": None,
+ ... "allowed_categories": None,
+ ... "disallowed_channels": None,
+ ... "disallowed_categories": None
+ ... }
+ ... }
... },
... ...
... ]
@@ -112,11 +162,28 @@ class FilterViewSet(ModelViewSet):
#### Response format
>>> {
- ... "id": 1,
- ... "content": "267624335836053506",
- ... "description": "Python Discord",
- ... "additional_field": None,
- ... "override": 1
+ ... "id": 1,
+ ... "filter_list": 1
+ ... "content": "267624335836053506",
+ ... "description": "Python Discord",
+ ... "additional_field": None,
+ ... "settings": {
+ ... "ping_type": None,
+ ... "dm_ping_type": None
+ ... "bypass_roles": None
+ ... "filter_dm": None,
+ ... "infraction": {
+ ... "infraction_type": None,
+ ... "infraction_reason": "",
+ ... "infraction_duration": None
+ ... },
+ ... "channel_scope": {
+ ... "allowed_channels": None,
+ ... "allowed_categories": None,
+ ... "disallowed_channels": None,
+ ... "disallowed_categories": None
+ ... }
+ ... }
... }
#### Status codes