From 61e111742d3d6a604bf2596c59dfa1a201097a8f Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Sun, 9 Jun 2019 23:54:13 +0200 Subject: [PATCH] Avoid unneccesary logs in some highly-used API endpoints --- src/pretix/api/views/event.py | 5 +++++ src/pretix/api/views/item.py | 9 +++++++++ src/tests/api/test_items.py | 16 ++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/pretix/api/views/event.py b/src/pretix/api/views/event.py index 4571cdd1d5..8249237885 100644 --- a/src/pretix/api/views/event.py +++ b/src/pretix/api/views/event.py @@ -244,6 +244,11 @@ class SubEventViewSet(ConditionalListView, viewsets.ModelViewSet): ) def perform_update(self, serializer): + if serializer.data == self.get_serializer(instance=serializer.instance).data: + # Performance optimization: If nothing was changed, we do not need to save or log anything. + # This costs us a few cycles on save, but avoids thousands of lines in our log. + return + super().perform_update(serializer) serializer.instance.log_action( diff --git a/src/pretix/api/views/item.py b/src/pretix/api/views/item.py index ae3fb3f646..97b149cc56 100644 --- a/src/pretix/api/views/item.py +++ b/src/pretix/api/views/item.py @@ -65,6 +65,10 @@ class ItemViewSet(ConditionalListView, viewsets.ModelViewSet): return ctx def perform_update(self, serializer): + if serializer.data == self.get_serializer(instance=serializer.instance).data: + # Performance optimization: If nothing was changed, we do not need to save or log anything. + # This costs us a few cycles on save, but avoids thousands of lines in our log. + return serializer.save(event=self.request.event) serializer.instance.log_action( 'pretix.event.item.changed', @@ -452,6 +456,11 @@ class QuotaViewSet(ConditionalListView, viewsets.ModelViewSet): return ctx def perform_update(self, serializer): + if serializer.data == self.get_serializer(instance=serializer.instance).data: + # Performance optimization: If nothing was changed, we do not need to save or log anything. + # This costs us a few cycles on save, but avoids thousands of lines in our log. + return + current_subevent = serializer.instance.subevent serializer.save(event=self.request.event) request_subevent = serializer.instance.subevent diff --git a/src/tests/api/test_items.py b/src/tests/api/test_items.py index 98953ea14a..e5547237e8 100644 --- a/src/tests/api/test_items.py +++ b/src/tests/api/test_items.py @@ -1553,6 +1553,22 @@ def test_quota_update(token_client, organizer, event, quota, item): quota = Quota.objects.get(pk=resp.data['id']) assert quota.name == "Ticket Quota Update" assert quota.size == 111 + assert quota.all_logentries().count() == 1 + + +@pytest.mark.django_db +def test_quota_update_unchanged(token_client, organizer, event, quota, item): + resp = token_client.patch( + '/api/v1/organizers/{}/events/{}/quotas/{}/'.format(organizer.slug, event.slug, quota.pk), + { + "size": 200, + }, + format='json' + ) + assert resp.status_code == 200 + quota = Quota.objects.get(pk=resp.data['id']) + assert quota.size == 200 + assert quota.all_logentries().count() == 0 @pytest.mark.django_db