From c8c6cb8754bd0917e35eb157925028a9b6f1dcb9 Mon Sep 17 00:00:00 2001 From: Lucas Lindström Date: Tue, 6 Oct 2020 21:29:34 +0200 Subject: Added metricity db connection and user bot API --- pydis_site/apps/api/viewsets/bot/user.py | 46 ++++++++++++++++++++++++++++++++ pydis_site/settings.py | 3 ++- 2 files changed, 48 insertions(+), 1 deletion(-) (limited to 'pydis_site') diff --git a/pydis_site/apps/api/viewsets/bot/user.py b/pydis_site/apps/api/viewsets/bot/user.py index 9571b3d7..0eeacbb3 100644 --- a/pydis_site/apps/api/viewsets/bot/user.py +++ b/pydis_site/apps/api/viewsets/bot/user.py @@ -1,3 +1,10 @@ +import json + +from django.db import connections +from rest_framework import status +from rest_framework.decorators import action +from rest_framework.request import Request +from rest_framework.response import Response from rest_framework.viewsets import ModelViewSet from rest_framework_bulk import BulkCreateModelMixin @@ -53,6 +60,29 @@ class UserViewSet(BulkCreateModelMixin, ModelViewSet): - 200: returned on success - 404: if a user with the given `snowflake` could not be found + ### GET /bot/users//metricity_data + Gets metricity data for a single user by ID. + + #### Response format + >>> { + ... "id": "0", + ... "name": "foo", + ... "avatar_hash": "bar", + ... "joined_at": "2020-10-06T18:17:30.101677", + ... "created_at": "2020-10-06T18:17:30.101677", + ... "is_staff": False, + ... "opt_out": False, + ... "bot": False, + ... "is_guild": True, + ... "is_verified": False, + ... "public_flags": {}, + ... "verified_at": null + ...} + + #### Status codes + - 200: returned on success + - 404: if a user with the given `snowflake` could not be found + ### POST /bot/users Adds a single or multiple new users. The roles attached to the user(s) must be roles known by the site. @@ -115,7 +145,23 @@ class UserViewSet(BulkCreateModelMixin, ModelViewSet): #### Status codes - 204: returned on success - 404: if a user with the given `snowflake` does not exist + + """ serializer_class = UserSerializer queryset = User.objects + + @action(detail=True) + def metricity_data(self, request: Request, pk: str = None) -> Response: + """Request handler for metricity_data endpoint.""" + user = self.get_object() + column_keys = ["id", "name", "avatar_hash", "joined_at", "created_at", "is_staff", + "opt_out", "bot", "is_guild", "is_verified", "public_flags", "verified_at"] + with connections['metricity'].cursor() as cursor: + query = f"SELECT {','.join(column_keys)} FROM users WHERE id = '%s'" + cursor.execute(query, [user.id]) + values = cursor.fetchone() + data = dict(zip(column_keys, values)) + data["public_flags"] = json.loads(data["public_flags"]) + return Response(data, status=status.HTTP_200_OK) diff --git a/pydis_site/settings.py b/pydis_site/settings.py index 3769fa25..2e78e458 100644 --- a/pydis_site/settings.py +++ b/pydis_site/settings.py @@ -172,7 +172,8 @@ WSGI_APPLICATION = 'pydis_site.wsgi.application' # https://docs.djangoproject.com/en/2.1/ref/settings/#databases DATABASES = { - 'default': env.db() + 'default': env.db(), + 'metricity': env.db('METRICITY_DB_URL'), } # Password validation -- cgit v1.2.3 From a188ab8ebfa9299addb7fd0effce2de7a0509645 Mon Sep 17 00:00:00 2001 From: Lucas Lindström Date: Tue, 6 Oct 2020 22:02:08 +0200 Subject: Fix minor style issue. --- pydis_site/apps/api/viewsets/bot/user.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'pydis_site') diff --git a/pydis_site/apps/api/viewsets/bot/user.py b/pydis_site/apps/api/viewsets/bot/user.py index 0eeacbb3..059bc0f0 100644 --- a/pydis_site/apps/api/viewsets/bot/user.py +++ b/pydis_site/apps/api/viewsets/bot/user.py @@ -145,8 +145,6 @@ class UserViewSet(BulkCreateModelMixin, ModelViewSet): #### Status codes - 204: returned on success - 404: if a user with the given `snowflake` does not exist - - """ serializer_class = UserSerializer -- cgit v1.2.3 From 15086b4ab392a8bdc6c33414f0c4e2a294f4a2ef Mon Sep 17 00:00:00 2001 From: Lucas Lindström Date: Tue, 6 Oct 2020 22:40:14 +0200 Subject: Added total message count to metricity data response. --- postgres/init.sql | 16 ++++++++++++++++ pydis_site/apps/api/viewsets/bot/user.py | 9 ++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) (limited to 'pydis_site') diff --git a/postgres/init.sql b/postgres/init.sql index fd29ddbc..45ad440c 100644 --- a/postgres/init.sql +++ b/postgres/init.sql @@ -32,3 +32,19 @@ INSERT INTO users VALUES ( '{}', NULL ); + +CREATE TABLE messages ( + id varchar(255), + author_id varchar(255) references users(id), + primary key(id) +); + +INSERT INTO messages VALUES( + 0, + 0 +); + +INSERT INTO messages VALUES( + 1, + 0 +); diff --git a/pydis_site/apps/api/viewsets/bot/user.py b/pydis_site/apps/api/viewsets/bot/user.py index 059bc0f0..b3d880cc 100644 --- a/pydis_site/apps/api/viewsets/bot/user.py +++ b/pydis_site/apps/api/viewsets/bot/user.py @@ -76,7 +76,8 @@ class UserViewSet(BulkCreateModelMixin, ModelViewSet): ... "is_guild": True, ... "is_verified": False, ... "public_flags": {}, - ... "verified_at": null + ... "verified_at": None, + ... "total_messages": 2 ...} #### Status codes @@ -157,9 +158,15 @@ class UserViewSet(BulkCreateModelMixin, ModelViewSet): column_keys = ["id", "name", "avatar_hash", "joined_at", "created_at", "is_staff", "opt_out", "bot", "is_guild", "is_verified", "public_flags", "verified_at"] with connections['metricity'].cursor() as cursor: + # Get user data query = f"SELECT {','.join(column_keys)} FROM users WHERE id = '%s'" cursor.execute(query, [user.id]) values = cursor.fetchone() data = dict(zip(column_keys, values)) data["public_flags"] = json.loads(data["public_flags"]) + + # Get message count + cursor.execute("SELECT COUNT(*) FROM messages WHERE author_id = '%s'", [user.id]) + data["total_messages"], = cursor.fetchone() + return Response(data, status=status.HTTP_200_OK) -- cgit v1.2.3 From e83f445a9b8d2db4523e261759bb73ea83ed54c3 Mon Sep 17 00:00:00 2001 From: Lucas Lindström Date: Tue, 6 Oct 2020 23:56:47 +0200 Subject: Reduce metricity db setup script and API response to the bare necessities. --- postgres/init.sql | 28 ++++------------------------ pydis_site/apps/api/viewsets/bot/user.py | 29 ++++------------------------- 2 files changed, 8 insertions(+), 49 deletions(-) (limited to 'pydis_site') diff --git a/postgres/init.sql b/postgres/init.sql index 45ad440c..922ce1ad 100644 --- a/postgres/init.sql +++ b/postgres/init.sql @@ -3,39 +3,19 @@ CREATE DATABASE metricity; \c metricity; CREATE TABLE users ( - id varchar(255), - name varchar(255) not null, - avatar_hash varchar(255), - joined_at timestamp not null, - created_at timestamp not null, - is_staff boolean not null, - opt_out boolean default false, - bot boolean default false, - is_guild boolean default true, - is_verified boolean default false, - public_flags text default '{}', + id varchar, verified_at timestamp, primary key(id) ); INSERT INTO users VALUES ( 0, - 'foo', - 'bar', - current_timestamp, - current_timestamp, - false, - false, - false, - true, - false, - '{}', - NULL + current_timestamp ); CREATE TABLE messages ( - id varchar(255), - author_id varchar(255) references users(id), + id varchar, + author_id varchar references users(id), primary key(id) ); diff --git a/pydis_site/apps/api/viewsets/bot/user.py b/pydis_site/apps/api/viewsets/bot/user.py index b3d880cc..1b1af841 100644 --- a/pydis_site/apps/api/viewsets/bot/user.py +++ b/pydis_site/apps/api/viewsets/bot/user.py @@ -1,5 +1,3 @@ -import json - from django.db import connections from rest_framework import status from rest_framework.decorators import action @@ -65,18 +63,7 @@ class UserViewSet(BulkCreateModelMixin, ModelViewSet): #### Response format >>> { - ... "id": "0", - ... "name": "foo", - ... "avatar_hash": "bar", - ... "joined_at": "2020-10-06T18:17:30.101677", - ... "created_at": "2020-10-06T18:17:30.101677", - ... "is_staff": False, - ... "opt_out": False, - ... "bot": False, - ... "is_guild": True, - ... "is_verified": False, - ... "public_flags": {}, - ... "verified_at": None, + ... "verified_at": "2020-10-06T21:54:23.540766", ... "total_messages": 2 ...} @@ -155,18 +142,10 @@ class UserViewSet(BulkCreateModelMixin, ModelViewSet): def metricity_data(self, request: Request, pk: str = None) -> Response: """Request handler for metricity_data endpoint.""" user = self.get_object() - column_keys = ["id", "name", "avatar_hash", "joined_at", "created_at", "is_staff", - "opt_out", "bot", "is_guild", "is_verified", "public_flags", "verified_at"] with connections['metricity'].cursor() as cursor: - # Get user data - query = f"SELECT {','.join(column_keys)} FROM users WHERE id = '%s'" - cursor.execute(query, [user.id]) - values = cursor.fetchone() - data = dict(zip(column_keys, values)) - data["public_flags"] = json.loads(data["public_flags"]) - - # Get message count + data = {} + cursor.execute("SELECT verified_at FROM users WHERE id = '%s'", [user.id]) + data["verified_at"], = cursor.fetchone() cursor.execute("SELECT COUNT(*) FROM messages WHERE author_id = '%s'", [user.id]) data["total_messages"], = cursor.fetchone() - return Response(data, status=status.HTTP_200_OK) -- cgit v1.2.3 From 484eba7715fcbcc195d66f5a60ff56c8167ecf0e Mon Sep 17 00:00:00 2001 From: Lucas Lindström Date: Thu, 8 Oct 2020 00:17:47 +0200 Subject: Broke out metricity connection into an abstraction and added metricity endpoint unit tests. --- .coveragerc | 1 + pydis_site/apps/api/models/bot/metricity.py | 42 +++++++++++++++++++++ pydis_site/apps/api/tests/test_users.py | 58 +++++++++++++++++++++++++++++ pydis_site/apps/api/viewsets/bot/user.py | 17 +++++---- 4 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 pydis_site/apps/api/models/bot/metricity.py (limited to 'pydis_site') diff --git a/.coveragerc b/.coveragerc index f5ddf08d..0cccc47c 100644 --- a/.coveragerc +++ b/.coveragerc @@ -12,6 +12,7 @@ omit = */admin.py */apps.py */urls.py + pydis_site/apps/api/models/bot/metricity.py pydis_site/wsgi.py pydis_site/settings.py pydis_site/utils/resources.py diff --git a/pydis_site/apps/api/models/bot/metricity.py b/pydis_site/apps/api/models/bot/metricity.py new file mode 100644 index 00000000..25b42fa2 --- /dev/null +++ b/pydis_site/apps/api/models/bot/metricity.py @@ -0,0 +1,42 @@ +from django.db import connections + + +class NotFound(Exception): + """Raised when an entity cannot be found.""" + + pass + + +class Metricity: + """Abstraction for a connection to the metricity database.""" + + def __init__(self): + self.cursor = connections['metricity'].cursor() + + def __enter__(self): + return self + + def __exit__(self, *_): + self.cursor.close() + + def user(self, user_id: str) -> dict: + """Query a user's data.""" + columns = ["verified_at"] + query = f"SELECT {','.join(columns)} FROM users WHERE id = '%s'" + self.cursor.execute(query, [user_id]) + values = self.cursor.fetchone() + + if not values: + raise NotFound() + + return dict(zip(columns, values)) + + def total_messages(self, user_id: str) -> int: + """Query total number of messages for a user.""" + self.cursor.execute("SELECT COUNT(*) FROM messages WHERE author_id = '%s'", [user_id]) + values = self.cursor.fetchone() + + if not values: + raise NotFound() + + return values[0] diff --git a/pydis_site/apps/api/tests/test_users.py b/pydis_site/apps/api/tests/test_users.py index a02fce8a..76a21d3a 100644 --- a/pydis_site/apps/api/tests/test_users.py +++ b/pydis_site/apps/api/tests/test_users.py @@ -1,7 +1,10 @@ +from unittest.mock import patch + from django_hosts.resolvers import reverse from .base import APISubdomainTestCase from ..models import Role, User +from ..models.bot.metricity import NotFound class UnauthedUserAPITests(APISubdomainTestCase): @@ -170,3 +173,58 @@ class UserModelTests(APISubdomainTestCase): def test_correct_username_formatting(self): """Tests the username property with both name and discriminator formatted together.""" self.assertEqual(self.user_with_roles.username, "Test User with two roles#0001") + + +class UserMetricityTests(APISubdomainTestCase): + @classmethod + def setUpTestData(cls): + User.objects.create( + id=0, + name="Test user", + discriminator=1, + in_guild=True, + ) + + def test_get_metricity_data(self): + # Given + verified_at = "foo" + total_messages = 1 + self.mock_metricity_user(verified_at, total_messages) + + # When + url = reverse('bot:user-metricity-data', args=[0], host='api') + response = self.client.get(url) + + # Then + self.assertEqual(response.status_code, 200) + self.assertEqual(response.json(), { + "verified_at": verified_at, + "total_messages": total_messages, + }) + + def test_no_metricity_user(self): + # Given + self.mock_no_metricity_user() + + # When + url = reverse('bot:user-metricity-data', args=[0], host='api') + response = self.client.get(url) + + # Then + self.assertEqual(response.status_code, 404) + + def mock_metricity_user(self, verified_at, total_messages): + patcher = patch("pydis_site.apps.api.viewsets.bot.user.Metricity") + self.metricity = patcher.start() + self.addCleanup(patcher.stop) + self.metricity = self.metricity.return_value.__enter__.return_value + self.metricity.user.return_value = dict(verified_at=verified_at) + self.metricity.total_messages.return_value = total_messages + + def mock_no_metricity_user(self): + patcher = patch("pydis_site.apps.api.viewsets.bot.user.Metricity") + self.metricity = patcher.start() + self.addCleanup(patcher.stop) + self.metricity = self.metricity.return_value.__enter__.return_value + self.metricity.user.side_effect = NotFound() + self.metricity.total_messages.side_effect = NotFound() diff --git a/pydis_site/apps/api/viewsets/bot/user.py b/pydis_site/apps/api/viewsets/bot/user.py index 1b1af841..352d77c0 100644 --- a/pydis_site/apps/api/viewsets/bot/user.py +++ b/pydis_site/apps/api/viewsets/bot/user.py @@ -1,4 +1,3 @@ -from django.db import connections from rest_framework import status from rest_framework.decorators import action from rest_framework.request import Request @@ -6,6 +5,7 @@ from rest_framework.response import Response from rest_framework.viewsets import ModelViewSet from rest_framework_bulk import BulkCreateModelMixin +from pydis_site.apps.api.models.bot.metricity import Metricity, NotFound from pydis_site.apps.api.models.bot.user import User from pydis_site.apps.api.serializers import UserSerializer @@ -142,10 +142,11 @@ class UserViewSet(BulkCreateModelMixin, ModelViewSet): def metricity_data(self, request: Request, pk: str = None) -> Response: """Request handler for metricity_data endpoint.""" user = self.get_object() - with connections['metricity'].cursor() as cursor: - data = {} - cursor.execute("SELECT verified_at FROM users WHERE id = '%s'", [user.id]) - data["verified_at"], = cursor.fetchone() - cursor.execute("SELECT COUNT(*) FROM messages WHERE author_id = '%s'", [user.id]) - data["total_messages"], = cursor.fetchone() - return Response(data, status=status.HTTP_200_OK) + with Metricity() as metricity: + try: + data = metricity.user(user.id) + data["total_messages"] = metricity.total_messages(user.id) + return Response(data, status=status.HTTP_200_OK) + except NotFound: + return Response(dict(detail="User not found in metricity"), + status=status.HTTP_404_NOT_FOUND) -- cgit v1.2.3 From 9c812d5c40813b453bf0711538eaebea46e2b16b Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 10 Oct 2020 19:09:31 +0300 Subject: Add voice ban to infraction types and create migration for it --- .../migrations/0067_add_voice_ban_infraction_type.py | 18 ++++++++++++++++++ pydis_site/apps/api/models/bot/infraction.py | 3 ++- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 pydis_site/apps/api/migrations/0067_add_voice_ban_infraction_type.py (limited to 'pydis_site') diff --git a/pydis_site/apps/api/migrations/0067_add_voice_ban_infraction_type.py b/pydis_site/apps/api/migrations/0067_add_voice_ban_infraction_type.py new file mode 100644 index 00000000..9a940ff4 --- /dev/null +++ b/pydis_site/apps/api/migrations/0067_add_voice_ban_infraction_type.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.10 on 2020-10-10 16:08 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0066_merge_20201003_0730'), + ] + + operations = [ + migrations.AlterField( + model_name='infraction', + name='type', + field=models.CharField(choices=[('note', 'Note'), ('warning', 'Warning'), ('watch', 'Watch'), ('mute', 'Mute'), ('kick', 'Kick'), ('ban', 'Ban'), ('superstar', 'Superstar'), ('voice_ban', 'Voice Ban')], help_text='The type of the infraction.', max_length=9), + ), + ] diff --git a/pydis_site/apps/api/models/bot/infraction.py b/pydis_site/apps/api/models/bot/infraction.py index 7660cbba..5ef3a4ce 100644 --- a/pydis_site/apps/api/models/bot/infraction.py +++ b/pydis_site/apps/api/models/bot/infraction.py @@ -15,7 +15,8 @@ class Infraction(ModelReprMixin, models.Model): ("mute", "Mute"), ("kick", "Kick"), ("ban", "Ban"), - ("superstar", "Superstar") + ("superstar", "Superstar"), + ("voice_ban", "Voice Ban") ) inserted_at = models.DateTimeField( default=timezone.now, -- cgit v1.2.3 From 511152c0a91ae81949941ff9d8f8129f01338173 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 10 Oct 2020 19:14:36 +0300 Subject: Don't allow voice ban to be hidden infraction --- pydis_site/apps/api/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pydis_site') diff --git a/pydis_site/apps/api/serializers.py b/pydis_site/apps/api/serializers.py index 25c5c82e..10eb3839 100644 --- a/pydis_site/apps/api/serializers.py +++ b/pydis_site/apps/api/serializers.py @@ -167,7 +167,7 @@ class InfractionSerializer(ModelSerializer): raise ValidationError({'expires_at': [f'{infr_type} infractions cannot expire.']}) hidden = attrs.get('hidden') - if hidden and infr_type in ('superstar', 'warning'): + if hidden and infr_type in ('superstar', 'warning', 'voice_ban'): raise ValidationError({'hidden': [f'{infr_type} infractions cannot be hidden.']}) if not hidden and infr_type in ('note', ): -- cgit v1.2.3 From 990d8cfc98ec71a31ea3ef659cb109547f987e31 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 10 Oct 2020 19:56:54 +0300 Subject: Include voice ban status on metricity data --- pydis_site/apps/api/viewsets/bot/user.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'pydis_site') diff --git a/pydis_site/apps/api/viewsets/bot/user.py b/pydis_site/apps/api/viewsets/bot/user.py index 3ab71186..19f55555 100644 --- a/pydis_site/apps/api/viewsets/bot/user.py +++ b/pydis_site/apps/api/viewsets/bot/user.py @@ -1,6 +1,7 @@ import typing from collections import OrderedDict +from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist from rest_framework import status from rest_framework.decorators import action from rest_framework.pagination import PageNumberPagination @@ -9,6 +10,7 @@ from rest_framework.response import Response 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.user import User from pydis_site.apps.api.serializers import UserSerializer @@ -240,10 +242,21 @@ class UserViewSet(ModelViewSet): def metricity_data(self, request: Request, pk: str = None) -> Response: """Request handler for metricity_data endpoint.""" user = self.get_object() + + try: + Infraction.objects.get(user__id=user.id, active=True, type="voice_ban") + except ObjectDoesNotExist: + voice_banned = False + except MultipleObjectReturned: + voice_banned = True + else: + voice_banned = True + with Metricity() as metricity: try: data = metricity.user(user.id) data["total_messages"] = metricity.total_messages(user.id) + data["voice_banned"] = voice_banned return Response(data, status=status.HTTP_200_OK) except NotFound: return Response(dict(detail="User not found in metricity"), -- cgit v1.2.3 From f1bdb525af330f28742638fdaa19cd8c76235906 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 10 Oct 2020 20:15:01 +0300 Subject: Cover metricity voice ban status with tests --- pydis_site/apps/api/tests/test_users.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'pydis_site') diff --git a/pydis_site/apps/api/tests/test_users.py b/pydis_site/apps/api/tests/test_users.py index d03785ae..01bf208b 100644 --- a/pydis_site/apps/api/tests/test_users.py +++ b/pydis_site/apps/api/tests/test_users.py @@ -1,5 +1,6 @@ from unittest.mock import patch +from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist from django_hosts.resolvers import reverse from .base import APISubdomainTestCase @@ -419,6 +420,7 @@ class UserMetricityTests(APISubdomainTestCase): self.assertEqual(response.json(), { "verified_at": verified_at, "total_messages": total_messages, + "voice_banned": False, }) def test_no_metricity_user(self): @@ -432,6 +434,27 @@ class UserMetricityTests(APISubdomainTestCase): # Then self.assertEqual(response.status_code, 404) + def test_metricity_voice_banned(self): + cases = [ + {'exception': None, 'voice_banned': True}, + {'exception': ObjectDoesNotExist, 'voice_banned': False}, + {'exception': MultipleObjectsReturned, 'voice_banned': True}, + ] + + self.mock_metricity_user("foo", 1) + + for case in cases: + with self.subTest(exception=case['exception'], voice_banned=case['voice_banned']): + with patch("pydis_site.apps.api.viewsets.bot.user.Infraction.objects.get") as p: + p.side_effect = case['exception'] + + url = reverse('bot:user-metricity-data', args=[0], host='api') + response = self.client.get(url) + + self.assertEqual(response.status_code, 200) + self.assertEqual(response.json()["voice_banned"], case["voice_banned"]) + + def mock_metricity_user(self, verified_at, total_messages): patcher = patch("pydis_site.apps.api.viewsets.bot.user.Metricity") self.metricity = patcher.start() -- cgit v1.2.3 From d80c7f18fadd1abc1aa2c080c2de6e7dee395883 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 10 Oct 2020 20:19:53 +0300 Subject: Fix wrong exception name --- pydis_site/apps/api/viewsets/bot/user.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pydis_site') diff --git a/pydis_site/apps/api/viewsets/bot/user.py b/pydis_site/apps/api/viewsets/bot/user.py index 19f55555..367f6b65 100644 --- a/pydis_site/apps/api/viewsets/bot/user.py +++ b/pydis_site/apps/api/viewsets/bot/user.py @@ -247,7 +247,7 @@ class UserViewSet(ModelViewSet): Infraction.objects.get(user__id=user.id, active=True, type="voice_ban") except ObjectDoesNotExist: voice_banned = False - except MultipleObjectReturned: + except MultipleObjectsReturned: voice_banned = True else: voice_banned = True -- cgit v1.2.3 From 8cb18e635920d64734a66be77e849cae3441108b Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 10 Oct 2020 20:21:42 +0300 Subject: Remove unnecessary newline from user tests --- pydis_site/apps/api/tests/test_users.py | 1 - 1 file changed, 1 deletion(-) (limited to 'pydis_site') diff --git a/pydis_site/apps/api/tests/test_users.py b/pydis_site/apps/api/tests/test_users.py index 01bf208b..e512f506 100644 --- a/pydis_site/apps/api/tests/test_users.py +++ b/pydis_site/apps/api/tests/test_users.py @@ -454,7 +454,6 @@ class UserMetricityTests(APISubdomainTestCase): self.assertEqual(response.status_code, 200) self.assertEqual(response.json()["voice_banned"], case["voice_banned"]) - def mock_metricity_user(self, verified_at, total_messages): patcher = patch("pydis_site.apps.api.viewsets.bot.user.Metricity") self.metricity = patcher.start() -- cgit v1.2.3 From 74f2131d05fdaa27b841e32f6c2fce2926b60f70 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 10 Oct 2020 20:34:13 +0300 Subject: Add trailing comma to infraction types listing Co-authored-by: Joe Banks --- pydis_site/apps/api/models/bot/infraction.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pydis_site') diff --git a/pydis_site/apps/api/models/bot/infraction.py b/pydis_site/apps/api/models/bot/infraction.py index 5ef3a4ce..60c1e8dd 100644 --- a/pydis_site/apps/api/models/bot/infraction.py +++ b/pydis_site/apps/api/models/bot/infraction.py @@ -16,7 +16,7 @@ class Infraction(ModelReprMixin, models.Model): ("kick", "Kick"), ("ban", "Ban"), ("superstar", "Superstar"), - ("voice_ban", "Voice Ban") + ("voice_ban", "Voice Ban"), ) inserted_at = models.DateTimeField( default=timezone.now, -- cgit v1.2.3 From 9c4ed0b98e4f34fe3a589e4e26f2b31e6c5169f9 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 10 Oct 2020 20:35:32 +0300 Subject: Don't catch multiple object returned exception --- pydis_site/apps/api/viewsets/bot/user.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'pydis_site') diff --git a/pydis_site/apps/api/viewsets/bot/user.py b/pydis_site/apps/api/viewsets/bot/user.py index 367f6b65..5205dc97 100644 --- a/pydis_site/apps/api/viewsets/bot/user.py +++ b/pydis_site/apps/api/viewsets/bot/user.py @@ -1,7 +1,7 @@ import typing from collections import OrderedDict -from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist +from django.core.exceptions import ObjectDoesNotExist from rest_framework import status from rest_framework.decorators import action from rest_framework.pagination import PageNumberPagination @@ -247,8 +247,6 @@ class UserViewSet(ModelViewSet): Infraction.objects.get(user__id=user.id, active=True, type="voice_ban") except ObjectDoesNotExist: voice_banned = False - except MultipleObjectsReturned: - voice_banned = True else: voice_banned = True -- cgit v1.2.3 From 42e34e955724eb47a505e24bcd03dc55c8e66419 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 10 Oct 2020 20:36:18 +0300 Subject: Remove multiple objects returning test case --- pydis_site/apps/api/tests/test_users.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'pydis_site') diff --git a/pydis_site/apps/api/tests/test_users.py b/pydis_site/apps/api/tests/test_users.py index e512f506..72ffcb3c 100644 --- a/pydis_site/apps/api/tests/test_users.py +++ b/pydis_site/apps/api/tests/test_users.py @@ -1,6 +1,6 @@ from unittest.mock import patch -from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist +from django.core.exceptions import ObjectDoesNotExist from django_hosts.resolvers import reverse from .base import APISubdomainTestCase @@ -438,7 +438,6 @@ class UserMetricityTests(APISubdomainTestCase): cases = [ {'exception': None, 'voice_banned': True}, {'exception': ObjectDoesNotExist, 'voice_banned': False}, - {'exception': MultipleObjectsReturned, 'voice_banned': True}, ] self.mock_metricity_user("foo", 1) -- cgit v1.2.3