Skip to content

Commit d7aed63

Browse files
committed
fix: foreign key 객체를 제대로 수정할 수 있도록 ModificationAudit 수정
1 parent 81e99e1 commit d7aed63

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

app/participant_portal_api/models.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,28 @@ def __str__(self) -> str:
6060

6161
def apply_modification(self, save: bool = False) -> models.Model:
6262
for field, value in self.modification_data.items():
63-
setattr(self.instance, field, value)
63+
if isinstance(value, list):
64+
if not (sub_qs := getattr(self.instance, field, None)):
65+
continue
66+
if not isinstance(sub_qs, models.manager.BaseManager):
67+
continue
68+
for sub_value in value:
69+
if not isinstance(sub_value, dict):
70+
continue
71+
if not (sub_instance_id := sub_value.get("id")):
72+
continue
73+
sub_qs.filter(pk=sub_instance_id).update(**sub_value)
74+
elif isinstance(value, dict):
75+
if not (sub_instance := getattr(self.instance, field, None)):
76+
continue
77+
for sub_field, sub_value in value.items():
78+
setattr(sub_instance, sub_field, sub_value)
79+
sub_instance.save()
80+
else:
81+
setattr(self.instance, field, value)
6482

6583
if save:
66-
self.instance.save(update_fields=self.modification_data.keys())
84+
self.instance.save()
6785

6886
return self.instance
6987

app/participant_portal_api/serializers/presentation.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import uuid
33

44
from core.util.thread_local import get_current_user
5-
from django.db import transaction
65
from event.presentation.models import Presentation, PresentationSpeaker
76
from file.models import PublicFile
87
from participant_portal_api.serializers.modification_audit import ModificationAuditCreationPortalSerializer
@@ -71,27 +70,22 @@ def to_representation(self, instance):
7170

7271
return result
7372

74-
def create(self, validated_data):
75-
raise NotImplementedError("Creation of presentations is not allowed in the participant portal.")
73+
def validate(self, attrs: dict) -> dict:
74+
attrs = super().validate(attrs)
7675

77-
@transaction.atomic
78-
def update(self, presentation, validated_data):
79-
speakers = typing.cast(list[PresentationSpeakerPortalData], validated_data["speakers"])
76+
speakers = typing.cast(list[PresentationSpeakerPortalData], attrs["speakers"])
8077
if not isinstance(speakers, list):
8178
raise serializers.ValidationError("Speakers must be a list.")
8279

8380
for speaker_data in speakers:
8481
if not (speaker_instance := self.get_speaker_instance(speaker_data["id"])):
85-
raise serializers.ValidationError(
86-
f"Speaker with ID {speaker_data['id']} not found or does not belong to this presentation."
87-
)
82+
err_msg = f"Speaker with ID {speaker_data['id']} not found or does not belong to this presentation."
83+
raise serializers.ValidationError(err_msg)
8884

89-
speaker_serializer = PresentationSpeakerPortalSerializer(
85+
PresentationSpeakerPortalSerializer(
9086
instance=speaker_instance,
9187
data=speaker_data,
9288
partial=True,
93-
)
94-
speaker_serializer.is_valid(raise_exception=True)
95-
speaker_serializer.save()
89+
).is_valid(raise_exception=True)
9690

97-
return super().update(presentation, validated_data)
91+
return attrs

0 commit comments

Comments
 (0)