Skip to content

Commit

Permalink
Fix: 시간표 생성 메소드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
joy-river committed Aug 9, 2024
1 parent 79618fb commit 5ca7be2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 37 deletions.
50 changes: 17 additions & 33 deletions KUA/course/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from rest_framework import serializers
from .models import Course, Tag, Post, Comment, TimeTable
from student.models import Student
from django.contrib.auth.models import User


class CourseSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -42,38 +41,23 @@ class Meta:
fields = ['id', 'post_id', 'student_id']

class TimeTableSerializer(serializers.ModelSerializer):
class Meta:
model = TimeTable
fields = '__all__'
courses = serializers.PrimaryKeyRelatedField(queryset=Course.objects.all(), many=True)

def create(self, validated_data):

student = validated_data['student']
course = validated_data['course']
year = validated_data['year']
semester = validated_data['semester']

check_student = Student.objects.filter(student=student)
check_course = Course.objects.filter(
course = course, year=year, semester=semester).exists()

if check_student and check_course:
timetable = TimeTable(
student=student,
course=course,
year=year,
semester=semester,
)
return timetable
elif check_student == False:
return {'error': 'This User id is not in Student Table.'}

elif check_course == False:
return {'error': 'This Course is not in Course Table.'}

else:
return {'error': 'code error'}

class Meta:
model = TimeTable
fields = '__all__'
fields = ['student', 'year', 'semester', 'courses']

def create(self, validated_data):
courses = validated_data.pop('courses', [])
print(f"Creating timetable with courses: {courses}") # 디버깅용 출력

# 시간표 객체 생성
timetable = TimeTable.objects.create(**validated_data)

if courses:
timetable.courses.set(courses) # ManyToMany 관계 설정
print(f"Successfully added courses to timetable: {timetable.id}") # 디버깅용 출력
else:
print(f"No courses were provided for timetable: {timetable.id}") # 디버깅용 출력

return timetable
27 changes: 23 additions & 4 deletions KUA/course/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def retrieve(self, request, *args, **kwargs):
return super().retrieve(request, *args, **kwargs)

@swagger_auto_schema(
operation_summary="태그 수정",
operation_summary="태그 수정 - 완료",
operation_description="기존 태그 정보를 수정합니다.",
request_body=TagSerializer,
responses={200: TagSerializer}
Expand All @@ -160,7 +160,7 @@ def update(self, request, *args, **kwargs):
return super().update(request, *args, **kwargs)

@swagger_auto_schema(
operation_summary="태그 부분 수정",
operation_summary="태그 부분 수정 - 완료",
operation_description="태그 정보의 일부를 수정합니다.",
request_body=TagSerializer,
responses={200: TagSerializer}
Expand All @@ -169,7 +169,7 @@ def partial_update(self, request, *args, **kwargs):
return super().partial_update(request, *args, **kwargs)

@swagger_auto_schema(
operation_summary="태그 삭제",
operation_summary="태그 삭제 - 완료",
operation_description="ID로 특정 태그를 삭제합니다.",
responses={204: 'Deleted'}
)
Expand Down Expand Up @@ -387,7 +387,26 @@ def list(self, request, *args, **kwargs):
responses={201: TimeTableSerializer}
)
def create(self, request, *args, **kwargs):
return super().create(request, *args, **kwargs)
# student_id로 Student 인스턴스를 조회
student_id = request.data.get('student')
student = get_object_or_404(Student, id=student_id)

# course_fk_id로 Course 인스턴스를 조회
course_fk_id = request.data.get('course_fk_id')
course = get_object_or_404(Course, id=course_fk_id)

# validated_data를 수동으로 구성
validated_data = {
'student': student,
'year': request.data.get('year'),
'semester': request.data.get('semester'),
}

# TimeTable 객체 생성
timetable = TimeTable.objects.create(**validated_data)
timetable.courses.add(course) # ManyToMany 관계 설정

return Response({'message': 'TimeTable created'}, status=status.HTTP_201_CREATED)

@swagger_auto_schema(
operation_summary="시간표 조회 기능",
Expand Down

0 comments on commit 5ca7be2

Please sign in to comment.