From 805b31591c0a3fa2e5a76e55401c19353cc3ebec Mon Sep 17 00:00:00 2001 From: yujeong Date: Mon, 7 Oct 2024 14:39:14 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20materials/views.py=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20-=20=EC=88=98=EB=8F=99=20merge=EB=A1=9C=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=EB=90=9C=20=EC=BD=94=EB=93=9C=20=EC=A4=91=EB=B3=B5=20?= =?UTF-8?q?=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- materials/views.py | 124 --------------------------------------------- 1 file changed, 124 deletions(-) diff --git a/materials/views.py b/materials/views.py index 37be415..6231260 100644 --- a/materials/views.py +++ b/materials/views.py @@ -16,75 +16,6 @@ from .serializers import ImageSerializer, VideoSerializer, WatchHistorySerializer -class ImageCreateView(generics.CreateAPIView): - # POST 요청: 이미지 파일을 업로드합니다. - - queryset = Image.objects.all() - serializer_class = ImageSerializer - permission_classes = [] - parser_classes = (MultiPartParser, FormParser) - - def create(self, request, *args, **kwargs): - serializer = self.get_serializer(data=request.data) - if serializer.is_valid(): - file = request.FILES.get("file") - if not file: - return Response( - {"error": "No file provided"}, status=status.HTTP_400_BAD_REQUEST - ) - - # 이미지 파일 검증 - try: - img = PILImage.open(file) - img.verify() - except: - return Response( - {"error": "Invalid image file"}, status=status.HTTP_400_BAD_REQUEST - ) - - # S3 클라이언트 설정 - s3_client = boto3.client( - "s3", - aws_access_key_id=settings.AWS_ACCESS_KEY_ID, - aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY, - region_name=settings.AWS_S3_REGION_NAME, - ) - - try: - # 이미지 최적화 (선택사항) - img = PILImage.open(file) - buffer = io.BytesIO() - img.save(buffer, format="JPEG", quality=85) - buffer.seek(0) - - # S3에 파일 업로드 - file_name = f"images/{file.name}" - s3_client.upload_fileobj( - buffer, - settings.AWS_STORAGE_BUCKET_NAME, - file_name, - ExtraArgs={"ContentType": "image/jpeg"}, - ) - - # 업로드된 파일의 URL 생성 - file_url = f"https://{settings.AWS_S3_CUSTOM_DOMAIN}/{file_name}" - - # 이미지 객체 생성 및 저장 - image = serializer.save(file=file_url) - - return Response( - self.get_serializer(image).data, status=status.HTTP_201_CREATED - ) - except ClientError as e: - return Response( - {"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR - ) - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - - -from .serializers import ImageSerializer, VideoSerializer, WatchHistorySerializer - - class ImageCreateView(generics.CreateAPIView): # POST 요청: 이미지 파일을 업로드합니다. @@ -157,7 +88,6 @@ class ImageListCreateView(generics.ListCreateAPIView): queryset = Image.objects.all() serializer_class = ImageSerializer permission_classes = [] - permission_classes = [] def perform_create(self, serializer): serializer.save(course_id=self.request.data.get("course_id")) @@ -171,7 +101,6 @@ class ImageRetrieveUpdateDestroyView(generics.RetrieveUpdateDestroyAPIView): queryset = Image.objects.all() serializer_class = ImageSerializer permission_classes = [] - permission_classes = [] def check_object_permissions(self, request, obj): if not request.user.is_staff and obj.course.tutor != request.user: @@ -230,64 +159,12 @@ def create(self, request, *args, **kwargs): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) -class VideoCreateView(generics.CreateAPIView): - # POST 요청: 영상 파일을 업로드합니다. - - queryset = Video.objects.all() - serializer_class = VideoSerializer - permission_classes = [] - parser_classes = (MultiPartParser, FormParser) - - def create(self, request, *args, **kwargs): - serializer = self.get_serializer(data=request.data) - if serializer.is_valid(): - file = request.FILES.get("file") - if not file: - return Response( - {"error": "No file provided"}, status=status.HTTP_400_BAD_REQUEST - ) - - # S3 클라이언트 설정 - s3_client = boto3.client( - "s3", - aws_access_key_id=settings.AWS_ACCESS_KEY_ID, - aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY, - region_name=settings.AWS_S3_REGION_NAME, - ) - - try: - # S3에 파일 업로드 - file_name = f"videos/{file.name}" - s3_client.upload_fileobj( - file, - settings.AWS_STORAGE_BUCKET_NAME, - file_name, - ExtraArgs={"ContentType": file.content_type}, - ) - - # 업로드된 파일의 URL 생성 - file_url = f"https://{settings.AWS_S3_CUSTOM_DOMAIN}/{file_name}" - - # 비디오 객체 생성 및 저장 - video = serializer.save(file=file_url) - - return Response( - self.get_serializer(video).data, status=status.HTTP_201_CREATED - ) - except ClientError as e: - return Response( - {"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR - ) - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - - class VideoListCreateView(generics.ListCreateAPIView): # GET 요청: 영상 파일 목록을 조회합니다. queryset = Video.objects.all() serializer_class = VideoSerializer permission_classes = [] - permission_classes = [] def perform_create(self, serializer): serializer.save(topic_id=self.request.data.get("topic_id")) @@ -301,7 +178,6 @@ class VideoRetrieveUpdateDestroyView(generics.RetrieveUpdateDestroyAPIView): queryset = Video.objects.all() serializer_class = VideoSerializer permission_classes = [] - permission_classes = [] def check_object_permissions(self, request, obj): if request.method in ["PUT", "PATCH", "DELETE"]: