Skip to content

Commit

Permalink
Merge ab57b18 into cb463b7
Browse files Browse the repository at this point in the history
  • Loading branch information
jchristgit committed Mar 31, 2024
2 parents cb463b7 + ab57b18 commit 4c39137
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 18 deletions.
20 changes: 16 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pydis_site/apps/api/tests/test_bumped_threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ def test_returns_404_for_non_existing_data(self):

response = self.client.get(url)
self.assertEqual(response.status_code, 404)
self.assertEqual(response.json(), {"detail": "No BumpedThread matches the given query."})
self.assertEqual(response.json(), {"detail": "Not found."})
7 changes: 1 addition & 6 deletions pydis_site/apps/api/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,7 @@ def test_fetch_non_existing(self) -> None:

response = self.client.get(f"{sequence.url()}/42")
self.assertEqual(response.status_code, 404)
parsed = response.json()
self.assertIn('detail', parsed)
self.assertIn(parsed['detail'], (
"No Filter matches the given query.",
"No FilterList matches the given query."
))
self.assertDictEqual(response.json(), {'detail': 'Not found.'})

def test_creation(self) -> None:
for name, sequence in get_test_sequences().items():
Expand Down
2 changes: 1 addition & 1 deletion pydis_site/apps/api/tests/test_infractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ def test_returns_400_for_second_active_infraction_of_the_same_type(self):
second_response.json(),
{
'non_field_errors': [
'The fields user, type must make a unique set.'
'This user already has an active infraction of this type.'
]
}
)
Expand Down
4 changes: 2 additions & 2 deletions pydis_site/apps/api/tests/test_nominations.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def test_returns_404_on_get_unknown_nomination(self):
response = self.client.get(url, data={})
self.assertEqual(response.status_code, 404)
self.assertEqual(response.json(), {
"detail": "No Nomination matches the given query."
"detail": "Not found."
})

def test_returns_404_on_patch_unknown_nomination(self):
Expand All @@ -391,7 +391,7 @@ def test_returns_404_on_patch_unknown_nomination(self):
response = self.client.patch(url, data={})
self.assertEqual(response.status_code, 404)
self.assertEqual(response.json(), {
"detail": "No Nomination matches the given query."
"detail": "Not found."
})

def test_returns_405_on_list_put(self):
Expand Down
2 changes: 1 addition & 1 deletion pydis_site/apps/api/tests/test_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,4 @@ def test_role_detail_404_all_methods(self):
for method in ('get', 'put', 'patch', 'delete'):
response = getattr(self.client, method)(url)
self.assertEqual(response.status_code, 404)
self.assertJSONEqual(response.content, '{"detail": "No Role matches the given query."}')
self.assertJSONEqual(response.content, '{"detail": "Not found."}')
3 changes: 1 addition & 2 deletions pydis_site/apps/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
bot_router = DefaultRouter(trailing_slash=False)
bot_router.register(
'filter/filter_lists',
FilterListViewSet,
basename='filter-filter-lists',
FilterListViewSet
)
bot_router.register(
"aoc-account-links",
Expand Down
26 changes: 26 additions & 0 deletions pydis_site/apps/api/viewsets/bot/infraction.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime

from django.db import IntegrityError
from django.db.models import QuerySet
from django.http.request import HttpRequest
from django_filters.rest_framework import DjangoFilterBackend
Expand Down Expand Up @@ -274,3 +275,28 @@ def partial_update_expanded(self, *args, **kwargs) -> Response:
"""
self.serializer_class = ExpandedInfractionSerializer
return self.partial_update(*args, **kwargs)

def create(self, request: HttpRequest, *args, **kwargs) -> Response:
"""
Create an infraction for a target user.
Called by the Django Rest Framework in response to the corresponding HTTP request.
"""
try:
return super().create(request, *args, **kwargs)
except IntegrityError as err:
# We need to use `__cause__` here, as Django reraises the internal
# UniqueViolation emitted by psycopg2 (which contains the attribute
# that we actually need)
#
# _meta is documented and mainly named that way to prevent
# name clashes: https://docs.djangoproject.com/en/dev/ref/models/meta/
if err.__cause__.diag.constraint_name == Infraction._meta.constraints[0].name:
raise ValidationError(
{
'non_field_errors': [
'This user already has an active infraction of this type.',
]
}
)
raise # pragma: no cover - no other constraint to test with
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ django-environ = "0.11.2"
django-filter = "24.2"
django-prometheus = "2.3.1"
django-simple-bulma = "2.6.0"
djangorestframework = "3.15.1"
djangorestframework = "3.14.0"
gunicorn = "21.2.0"
httpx = "0.27.0"
markdown = "3.6"
Expand Down

0 comments on commit 4c39137

Please sign in to comment.