aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/api/viewsets/bot
diff options
context:
space:
mode:
authorGravatar Leon Sandøy <[email protected]>2022-01-23 16:38:03 +0100
committerGravatar Leon Sandøy <[email protected]>2022-01-23 16:38:03 +0100
commit605d9a0266a9a967f051fa244bf1c2d31776c119 (patch)
treea9f3218e355c8f6a106ba2604334ce8a34823715 /pydis_site/apps/api/viewsets/bot
parentLink icons belong close together. (diff)
parentMerge pull request #640 from Krish-bhardwaj/main (diff)
Merge branch 'main' into swfarnsworth/smarter-resources/merge-with-main
Diffstat (limited to 'pydis_site/apps/api/viewsets/bot')
-rw-r--r--pydis_site/apps/api/viewsets/bot/filter_list.py3
-rw-r--r--pydis_site/apps/api/viewsets/bot/infraction.py9
-rw-r--r--pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py32
-rw-r--r--pydis_site/apps/api/viewsets/bot/reminder.py13
-rw-r--r--pydis_site/apps/api/viewsets/bot/user.py12
5 files changed, 41 insertions, 28 deletions
diff --git a/pydis_site/apps/api/viewsets/bot/filter_list.py b/pydis_site/apps/api/viewsets/bot/filter_list.py
index 2cb21ab9..4b05acee 100644
--- a/pydis_site/apps/api/viewsets/bot/filter_list.py
+++ b/pydis_site/apps/api/viewsets/bot/filter_list.py
@@ -59,7 +59,8 @@ class FilterListViewSet(ModelViewSet):
... ["GUILD_INVITE","Guild Invite"],
... ["FILE_FORMAT","File Format"],
... ["DOMAIN_NAME","Domain Name"],
- ... ["FILTER_TOKEN","Filter Token"]
+ ... ["FILTER_TOKEN","Filter Token"],
+ ... ["REDIRECT", "Redirect"]
... ]
#### Status codes
diff --git a/pydis_site/apps/api/viewsets/bot/infraction.py b/pydis_site/apps/api/viewsets/bot/infraction.py
index f8b0cb9d..8a48ed1f 100644
--- a/pydis_site/apps/api/viewsets/bot/infraction.py
+++ b/pydis_site/apps/api/viewsets/bot/infraction.py
@@ -70,7 +70,8 @@ class InfractionViewSet(
... 'actor': 125435062127820800,
... 'type': 'ban',
... 'reason': 'He terk my jerb!',
- ... 'hidden': True
+ ... 'hidden': True,
+ ... 'dm_sent': True
... }
... ]
@@ -100,7 +101,8 @@ class InfractionViewSet(
... 'hidden': True,
... 'type': 'ban',
... 'reason': 'He terk my jerb!',
- ... 'user': 172395097705414656
+ ... 'user': 172395097705414656,
+ ... 'dm_sent': False
... }
#### Response format
@@ -118,7 +120,8 @@ class InfractionViewSet(
>>> {
... 'active': True,
... 'expires_at': '4143-02-15T21:04:31+00:00',
- ... 'reason': 'durka derr'
+ ... 'reason': 'durka derr',
+ ... 'dm_sent': True
... }
#### Response format
diff --git a/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py b/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py
index 826ad25e..78f8c340 100644
--- a/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py
+++ b/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py
@@ -1,18 +1,17 @@
from django.db.models import Case, Value, When
from django.db.models.query import QuerySet
-from django.http.request import HttpRequest
from django.shortcuts import get_object_or_404
from rest_framework.exceptions import ParseError
-from rest_framework.mixins import DestroyModelMixin
+from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.status import HTTP_201_CREATED
-from rest_framework.viewsets import ViewSet
+from rest_framework.viewsets import ModelViewSet
from pydis_site.apps.api.models.bot.off_topic_channel_name import OffTopicChannelName
from pydis_site.apps.api.serializers import OffTopicChannelNameSerializer
-class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet):
+class OffTopicChannelNameViewSet(ModelViewSet):
"""
View of off-topic channel names used by the bot to rotate our off-topic names on a daily basis.
@@ -20,7 +19,7 @@ class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet):
### GET /bot/off-topic-channel-names
Return all known off-topic channel names from the database.
If the `random_items` query parameter is given, for example using...
- $ curl api.pythondiscord.local:8000/bot/off-topic-channel-names?random_items=5
+ $ curl 127.0.0.1:8000/api/bot/off-topic-channel-names?random_items=5
... then the API will return `5` random items from the database
that is not used in current rotation.
When running out of names, API will mark all names to not used and start new rotation.
@@ -39,7 +38,7 @@ class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet):
### POST /bot/off-topic-channel-names
Create a new off-topic-channel name in the database.
The name must be given as a query parameter, for example:
- $ curl api.pythondiscord.local:8000/bot/off-topic-channel-names?name=lemons-lemonade-shop
+ $ curl 127.0.0.1:8000/api/bot/off-topic-channel-names?name=lemons-lemonade-shop
#### Status codes
- 201: returned on success
@@ -58,6 +57,7 @@ class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet):
lookup_field = 'name'
serializer_class = OffTopicChannelNameSerializer
+ queryset = OffTopicChannelName.objects.all()
def get_object(self) -> OffTopicChannelName:
"""
@@ -65,15 +65,14 @@ class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet):
If it doesn't, a HTTP 404 is returned by way of throwing an exception.
"""
- queryset = self.get_queryset()
name = self.kwargs[self.lookup_field]
- return get_object_or_404(queryset, name=name)
+ return get_object_or_404(self.queryset, name=name)
def get_queryset(self) -> QuerySet:
"""Returns a queryset that covers the entire OffTopicChannelName table."""
return OffTopicChannelName.objects.all()
- def create(self, request: HttpRequest) -> Response:
+ def create(self, request: Request, *args, **kwargs) -> Response:
"""
DRF method for creating a new OffTopicChannelName.
@@ -91,7 +90,7 @@ class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet):
'name': ["This query parameter is required."]
})
- def list(self, request: HttpRequest) -> Response:
+ def list(self, request: Request, *args, **kwargs) -> Response:
"""
DRF method for listing OffTopicChannelName entries.
@@ -109,13 +108,13 @@ class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet):
'random_items': ["Must be a positive integer."]
})
- queryset = self.get_queryset().order_by('used', '?')[:random_count]
+ queryset = self.queryset.order_by('used', '?')[:random_count]
# When any name is used in our listing then this means we reached end of round
# and we need to reset all other names `used` to False
if any(offtopic_name.used for offtopic_name in queryset):
# These names that we just got have to be excluded from updating used to False
- self.get_queryset().update(
+ self.queryset.update(
used=Case(
When(
name__in=(offtopic_name.name for offtopic_name in queryset),
@@ -126,13 +125,18 @@ class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet):
)
else:
# Otherwise mark selected names `used` to True
- self.get_queryset().filter(
+ self.queryset.filter(
name__in=(offtopic_name.name for offtopic_name in queryset)
).update(used=True)
serialized = self.serializer_class(queryset, many=True)
return Response(serialized.data)
- queryset = self.get_queryset()
+ params = {}
+
+ if active_param := request.query_params.get("active"):
+ params["active"] = active_param.lower() == "true"
+
+ queryset = self.queryset.filter(**params)
serialized = self.serializer_class(queryset, many=True)
return Response(serialized.data)
diff --git a/pydis_site/apps/api/viewsets/bot/reminder.py b/pydis_site/apps/api/viewsets/bot/reminder.py
index 111660d9..78d7cb3b 100644
--- a/pydis_site/apps/api/viewsets/bot/reminder.py
+++ b/pydis_site/apps/api/viewsets/bot/reminder.py
@@ -42,7 +42,8 @@ class ReminderViewSet(
... 'expiration': '5018-11-20T15:52:00Z',
... 'id': 11,
... 'channel_id': 634547009956872193,
- ... 'jump_url': "https://discord.com/channels/<guild_id>/<channel_id>/<message_id>"
+ ... 'jump_url': "https://discord.com/channels/<guild_id>/<channel_id>/<message_id>",
+ ... 'failures': 3
... },
... ...
... ]
@@ -67,7 +68,8 @@ class ReminderViewSet(
... 'expiration': '5018-11-20T15:52:00Z',
... 'id': 11,
... 'channel_id': 634547009956872193,
- ... 'jump_url': "https://discord.com/channels/<guild_id>/<channel_id>/<message_id>"
+ ... 'jump_url': "https://discord.com/channels/<guild_id>/<channel_id>/<message_id>",
+ ... 'failures': 3
... }
#### Status codes
@@ -80,7 +82,7 @@ class ReminderViewSet(
#### Request body
>>> {
... 'author': int,
- ... 'mentions': List[int],
+ ... 'mentions': list[int],
... 'content': str,
... 'expiration': str, # ISO-formatted datetime
... 'channel_id': int,
@@ -98,9 +100,10 @@ class ReminderViewSet(
#### Request body
>>> {
- ... 'mentions': List[int],
+ ... 'mentions': list[int],
... 'content': str,
- ... 'expiration': str # ISO-formatted datetime
+ ... 'expiration': str, # ISO-formatted datetime
+ ... 'failures': int
... }
#### Status codes
diff --git a/pydis_site/apps/api/viewsets/bot/user.py b/pydis_site/apps/api/viewsets/bot/user.py
index 25722f5a..1a5e79f8 100644
--- a/pydis_site/apps/api/viewsets/bot/user.py
+++ b/pydis_site/apps/api/viewsets/bot/user.py
@@ -11,7 +11,7 @@ from rest_framework.serializers import ModelSerializer
from rest_framework.viewsets import ModelViewSet
from pydis_site.apps.api.models.bot.infraction import Infraction
-from pydis_site.apps.api.models.bot.metricity import Metricity, NotFound
+from pydis_site.apps.api.models.bot.metricity import Metricity, NotFoundError
from pydis_site.apps.api.models.bot.user import User
from pydis_site.apps.api.serializers import UserSerializer
@@ -19,7 +19,7 @@ from pydis_site.apps.api.serializers import UserSerializer
class UserListPagination(PageNumberPagination):
"""Custom pagination class for the User Model."""
- page_size = 10000
+ page_size = 2500
page_size_query_param = "page_size"
def get_next_page_number(self) -> typing.Optional[int]:
@@ -271,11 +271,13 @@ class UserViewSet(ModelViewSet):
with Metricity() as metricity:
try:
data = metricity.user(user.id)
+
data["total_messages"] = metricity.total_messages(user.id)
- data["voice_banned"] = voice_banned
data["activity_blocks"] = metricity.total_message_blocks(user.id)
+
+ data["voice_banned"] = voice_banned
return Response(data, status=status.HTTP_200_OK)
- except NotFound:
+ except NotFoundError:
return Response(dict(detail="User not found in metricity"),
status=status.HTTP_404_NOT_FOUND)
@@ -290,6 +292,6 @@ class UserViewSet(ModelViewSet):
data["total_messages"] = metricity.total_messages(user.id)
data["top_channel_activity"] = metricity.top_channel_activity(user.id)
return Response(data, status=status.HTTP_200_OK)
- except NotFound:
+ except NotFoundError:
return Response(dict(detail="User not found in metricity"),
status=status.HTTP_404_NOT_FOUND)