aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py55
1 files changed, 25 insertions, 30 deletions
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 4328c894..e6cf8172 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
@@ -20,11 +20,9 @@ class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet):
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
- ... then the API will return `5` random items from the database.
- If the `mark_used` query parameter is given like...
- $ curl api.pydis.local:8000/bot/off-topic-channel-names?random_items=5&mark_used=true
- ... then the API will mark returned `5` items `used`.
- When running out of names, API will mark all names to not used and start new round.
+ ... 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.
#### Response format
Return a list of off-topic-channel names:
@@ -110,32 +108,29 @@ class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet):
'random_items': ["Must be a positive integer."]
})
- if 'mark_used' in request.query_params and request.query_params['mark_used']:
- queryset = self.get_queryset().order_by('?').exclude(used=True)[:random_count]
- self.get_queryset().filter(
+ queryset = self.get_queryset().order_by('?').exclude(used=True)[:random_count]
+ self.get_queryset().filter(
+ name__in=(query.name for query in queryset)
+ ).update(used=True)
+
+ # When client request more channel names than non-used names is available, start
+ # new round of names.
+ if len(queryset) < random_count:
+ # Get how much names still missing and don't fetch duplicate names.
+ need_more = random_count - len(queryset)
+ ext = self.get_queryset().order_by('?').exclude(
name__in=(query.name for query in queryset)
- ).update(used=True)
-
- # When client request more channel names than non-used names is available, start
- # new round of names.
- if len(queryset) < random_count:
- # Get how much names still missing and don't fetch duplicate names.
- need_more = random_count - len(queryset)
- ext = self.get_queryset().order_by('?').exclude(
- name__in=(query.name for query in queryset)
- )[:need_more]
-
- # Set all names `used` field to False except these that we just used.
- self.get_queryset().exclude(name__in=(
- query.name for query in ext)
- ).update(used=False)
- # Join original queryset (that had missing names)
- # and extension with these missing names.
- queryset = list(queryset) + list(ext)
- serialized = self.serializer_class(queryset, many=True)
- return Response(serialized.data)
-
- queryset = self.get_queryset().order_by('?')[:random_count]
+ )[:need_more]
+
+ # Set all names `used` field to False except these that we just used.
+ self.get_queryset().exclude(name__in=(
+ query.name for query in ext)
+ ).update(used=False)
+
+ # Join original queryset (that had missing names)
+ # and extension with these missing names.
+ queryset = list(queryset) + list(ext)
+
serialized = self.serializer_class(queryset, many=True)
return Response(serialized.data)