From e8887fee8b66d29226f8220c1b795e1cfdc7f0e9 Mon Sep 17 00:00:00 2001 From: jedi Date: Wed, 6 Nov 2024 21:11:18 +0100 Subject: [PATCH] add frontend to edit event details --- core/inventory/serializers.py | 16 ++-- core/inventory/tests/v2/test_events.py | 26 ++++++ web/src/components/ExpandableTable.vue | 124 +++++++++++++++++++++++++ web/src/store.js | 7 ++ web/src/views/admin/Events.vue | 106 +++++++++++++++------ 5 files changed, 245 insertions(+), 34 deletions(-) create mode 100644 web/src/components/ExpandableTable.vue diff --git a/core/inventory/serializers.py b/core/inventory/serializers.py index 9a611f6..2611ce3 100644 --- a/core/inventory/serializers.py +++ b/core/inventory/serializers.py @@ -9,20 +9,22 @@ from mail.models import EventAddress from tickets.shared_serializers import BasicIssueSerializer -class EventAdressSerializer(serializers.ModelSerializer): - class Meta: - model = EventAddress - fields = ['address'] - - class EventSerializer(serializers.ModelSerializer): - addresses = EventAdressSerializer(many=True, required=False) + addresses = SlugRelatedField(many=True, slug_field='address', queryset=EventAddress.objects.all()) class Meta: model = Event fields = ['id', 'slug', 'name', 'start', 'end', 'pre_start', 'post_end', 'addresses'] read_only_fields = ['id'] + def to_internal_value(self, data): + data = data.copy() + addresses = data.pop('addresses', None) + dict = super().to_internal_value(data) + if addresses: + dict['addresses'] = [EventAddress.objects.get_or_create(address=x)[0] for x in addresses] + return dict + class ContainerSerializer(serializers.ModelSerializer): itemCount = serializers.SerializerMethodField() diff --git a/core/inventory/tests/v2/test_events.py b/core/inventory/tests/v2/test_events.py index 48ddb01..bd58515 100644 --- a/core/inventory/tests/v2/test_events.py +++ b/core/inventory/tests/v2/test_events.py @@ -47,6 +47,20 @@ class EventTestCase(TestCase): self.assertEqual(Event.objects.all()[0].slug, 'EVENT2') self.assertEqual(Event.objects.all()[0].name, 'Event 2 new') + def test_update_event(self): + from rest_framework.test import APIClient + event = Event.objects.create(slug='EVENT1', name='Event 1') + response = APIClient().patch(f'/api/2/events/{event.id}/', {'addresses': ['foo@bar.baz', 'foo1@bar.baz']}) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.json()['slug'], 'EVENT1') + self.assertEqual(response.json()['name'], 'Event 1') + self.assertEqual(2, len(response.json()['addresses'])) + self.assertEqual('foo@bar.baz', response.json()['addresses'][0]) + self.assertEqual('foo1@bar.baz', response.json()['addresses'][1]) + self.assertEqual(len(Event.objects.all()), 1) + self.assertEqual(Event.objects.all()[0].slug, 'EVENT1') + self.assertEqual(Event.objects.all()[0].name, 'Event 1') + def test_remove_event(self): event = Event.objects.create(slug='EVENT1', name='Event 1') Event.objects.create(slug='EVENT2', name='Event 2') @@ -66,3 +80,15 @@ class EventTestCase(TestCase): self.assertEqual('Event', response.json()[0]['name']) self.assertEqual(1, len(response.json()[0]['addresses'])) + def test_items_remove_addresss(self): + from mail.models import EventAddress + from rest_framework.test import APIClient + event1 = Event.objects.create(slug='TEST1', name='Event') + EventAddress.objects.create(event=event1, address='foo@bar.baz') + EventAddress.objects.create(event=event1, address='fo1o@bar.baz') + response = APIClient().patch(f'/api/2/events/{event1.id}/', {'addresses': ['foo1@bar.baz']}) + self.assertEqual(response.status_code, 200) + self.assertEqual('TEST1', response.json()['slug']) + self.assertEqual('Event', response.json()['name']) + self.assertEqual(1, len(response.json()['addresses'])) + self.assertEqual('foo1@bar.baz', response.json()['addresses'][0]) diff --git a/web/src/components/ExpandableTable.vue b/web/src/components/ExpandableTable.vue new file mode 100644 index 0000000..6bd5175 --- /dev/null +++ b/web/src/components/ExpandableTable.vue @@ -0,0 +1,124 @@ + + + + + \ No newline at end of file diff --git a/web/src/store.js b/web/src/store.js index dcc3aea..60dba61 100644 --- a/web/src/store.js +++ b/web/src/store.js @@ -339,6 +339,13 @@ const store = createStore({ commit('replaceEvents', [...state.events.filter(e => e.eid !== event_id)]) } }, + async updateEvent({commit, dispatch, state}, {id, partial_event}){ + console.log(id, partial_event); + const {data, success} = await http.patch(`/2/events/${id}/`, partial_event, state.user.token); + if (success) { + commit('replaceEvents', [...state.events.filter(e => e.eid !== id), data]) + } + }, async fetchTicketStates({commit, state, getters}) { if (!state.user.token) return; if (state.fetchedData.states > Date.now() - 1000 * 60 * 60 * 24) return; diff --git a/web/src/views/admin/Events.vue b/web/src/views/admin/Events.vue index 1aab608..e2ff952 100644 --- a/web/src/views/admin/Events.vue +++ b/web/src/views/admin/Events.vue @@ -1,50 +1,102 @@ \ No newline at end of file