diff --git a/core/inventory/serializers.py b/core/inventory/serializers.py index adaf020..76d2a20 100644 --- a/core/inventory/serializers.py +++ b/core/inventory/serializers.py @@ -1,25 +1,49 @@ from django.utils import timezone from rest_framework import serializers +from rest_framework.relations import SlugRelatedField from files.models import File from inventory.models import Event, Container, Item from mail.models import EventAddress -class EventAdressSerializer(serializers.ModelSerializer): - class Meta: - model = EventAddress - fields = ['address'] +#class EventAdressSerializer(serializers.ModelSerializer): +# class Meta: +# model = EventAddress +# fields = ['address'] + +# def to_internal_value(self, data): +# if not isinstance(data, str): +# raise serializers.ValidationError('This field must be a string.') +# +# def create(self, validated_data): +# return EventAddress.objects.create(**validated_data) +# +# def validate(self, data): +# return isinstance(data, str) + class EventSerializer(serializers.ModelSerializer): - addresses = EventAdressSerializer(many=True, required=False) + #addresses = EventAdressSerializer(many=True, required=False) + addresses = SlugRelatedField(many=True, slug_field='address', queryset=EventAddress.objects.all()) class Meta: model = Event fields = ['eid', 'slug', 'name', 'start', 'end', 'pre_start', 'post_end', 'addresses'] read_only_fields = ['eid'] +# def update(self, instance, validated_data): +# addresses = validated_data.pop('addresses', None) +# instance.save(validated_data) +# if addresses: +# for address in addresses: +# nested_instance, created = EventAddress.objects.get_or_create(address=address) +# instance.addresses.add(nested_instance) +# +# return instance + + 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 affbd0e..6bbc701 100644 --- a/core/inventory/tests/v2/test_events.py +++ b/core/inventory/tests/v2/test_events.py @@ -47,6 +47,18 @@ 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.eid}/', {'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(len(Event.objects.all()), 1) + self.assertEqual(Event.objects.all()[0].slug, 'EVENT1') + self.assertEqual(Event.objects.all()[0].name, 'Event 1') + self.assertEqual(1, len(response.json()[0]['addresses'])) + def test_remove_event(self): event = Event.objects.create(slug='EVENT1', name='Event 1') Event.objects.create(slug='EVENT2', name='Event 2') @@ -65,4 +77,3 @@ class EventTestCase(TestCase): self.assertEqual('TEST1', response.json()[0]['slug']) self.assertEqual('Event', response.json()[0]['name']) self.assertEqual(1, len(response.json()[0]['addresses'])) - diff --git a/core/mail/migrations/0006_alter_eventaddress_address.py b/core/mail/migrations/0006_alter_eventaddress_address.py new file mode 100644 index 0000000..5a03261 --- /dev/null +++ b/core/mail/migrations/0006_alter_eventaddress_address.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.7 on 2024-11-06 06:13 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('mail', '0005_alter_eventaddress_event'), + ] + + operations = [ + migrations.AlterField( + model_name='eventaddress', + name='address', + field=models.CharField(max_length=255, unique=True), + ), + ] diff --git a/core/mail/models.py b/core/mail/models.py index 28524de..f8a3869 100644 --- a/core/mail/models.py +++ b/core/mail/models.py @@ -46,7 +46,7 @@ class Email(SoftDeleteModel): class EventAddress(models.Model): id = models.AutoField(primary_key=True) event = models.ForeignKey(Event, models.SET_NULL, null=True, related_name='addresses') - address = models.CharField(max_length=255) + address = models.CharField(max_length=255, unique=True) class EmailAttachment(AbstractFile):