add frontend to edit event details
This commit is contained in:
parent
690a9c954e
commit
01b022128d
5 changed files with 245 additions and 34 deletions
|
@ -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()
|
||||
|
|
|
@ -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])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue