Files
colio/projects/views.py

172 lines
7.7 KiB
Python

from django.shortcuts import get_object_or_404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import *
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import OrderingFilter
from rest_framework.generics import ListAPIView
from .serializers import ProjectListViewSerializer, ProjectCreateSerializer, ProjectTeamSerializer
from .paginations import ProjectPagination
from .filters import ProjectFilter
from .services import *
from django.db import transaction
from notifications.services import *
class ProjectListView(ListAPIView):
queryset = Project.objects.filter(is_published=True).order_by('-created_at')
serializer_class = ProjectListViewSerializer
pagination_class = ProjectPagination
filterset_class = ProjectFilter
filter_backends = [DjangoFilterBackend, OrderingFilter]
ordering_fields = ['view_count', 'like_count', 'scrap_count', 'created_at'] # 허용할 필드 추천순 제외
ordering = ['-created_at'] # 기본 정렬은 최신순
class ProjectCreateAPIView(APIView):
@transaction.atomic
def post(self, request):
user = request.user
serializer = ProjectCreateSerializer(data=request.data, context={"request": request})
if serializer.is_valid():
serializer.save()
return Response({"message": "create project success"}, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class ProjectTeamManageAPIView(APIView):
# 팀원 초대 및 알림
@transaction.atomic
def post(self, request, pk):
user = request.user
project = get_object_or_404(Project, id=pk)
if user != project.owner:
return Response({"message": "Not owner"}, status=status.HTTP_403_FORBIDDEN)
nickname = request.data.get('nickname')
new_member = get_object_or_404(User, nickname=nickname)
if ProjectTeamList.objects.filter(project=project, user=new_member).exists():
return Response({"message": "already team member"}, status=status.HTTP_400_BAD_REQUEST)
new_notification = NotifiationService.create_notification(user=new_member, note_type=NotificationType.INVITE)
ProjectInvitationService.create_project_invitation(
project=project,
from_user=user,
to_user=new_member,
notification=new_notification
)
return Response({"message": "invite success"}, status=status.HTTP_200_OK)
# 팀원 추방
@transaction.atomic
def delete(self, request, pk):
user = request.user
project = get_object_or_404(Project, id=pk)
if user != project.owner:
return Response({"message": "Not owner"}, status=status.HTTP_403_FORBIDDEN)
nickname = request.data.get('nickname')
member = get_object_or_404(User, nickname=nickname)
user_in_project = get_object_or_404(ProjectTeamList, project=project, user=member)
user_in_project.delete()
return Response({"message": "delete user in project success"})
class ProjectDeleteAPIView(APIView):
@transaction.atomic
def delete(self, request, pk):
user = request.user
project = get_object_or_404(Project, id=pk)
if user != project.owner:
return Response({"message": "Not owner"}, status=status.HTTP_403_FORBIDDEN)
project.delete()
return Response({"message": "delete success"}, status=status.HTTP_200_OK)
class ProjectSetRepresentAPIView(APIView):
@transaction.atomic
def patch(self, request, pk):
user = request.user
project = get_object_or_404(Project, id=pk)
if user != project.owner:
return Response({"message": "Not owner"}, status=status.HTTP_403_FORBIDDEN)
if before_represent := Project.objects.filter(owner=user, is_represent=True).first():
before_represent.is_represent = False
before_represent.save()
project.is_represent = True
project.save()
return Response({"message": "change represent success"}, status=status.HTTP_200_OK)
class ProjectSetPublishAPIView(APIView):
@transaction.atomic
def patch(self, request, pk):
user = request.user
project = get_object_or_404(Project, id=pk)
if user != project.owner:
return Response({"message": "Not owner"}, status=status.HTTP_403_FORBIDDEN)
project.is_published = True
project.save()
return Response({"message": "publish success"}, status=status.HTTP_200_OK)
class ProjectChangeState(APIView):
@transaction.atomic
def patch(self, request, pk):
project = get_object_or_404(Project, pk=pk)
user = request.user
action_type = request.query_params.get('type')
try:
if ProjectBeforeRelCheckService.check_user_project_rel(action_type, project, user):
return Response({"message": "already done"}, status=status.HTTP_400_BAD_REQUEST)
except ValueError as e:
return Response({'message': str(e)}, status=status.HTTP_400_BAD_REQUEST)
return self._handle_action(action_type, project, user, add=True)
@transaction.atomic
def delete(self, request, pk):
project = get_object_or_404(Project, pk=pk)
user = request.user
action_type = request.query_params.get('type')
try:
if not ProjectBeforeRelCheckService.check_user_project_rel(action_type, project, user):
return Response({"message": "never done before"}, status=status.HTTP_400_BAD_REQUEST)
except ValueError as e:
return Response({'message': str(e)}, status=status.HTTP_400_BAD_REQUEST)
return self._handle_action(action_type, project, user, add=False)
def _handle_action(self, action_type, project, user, add=True):
if not action_type:
return Response({'message': 'Missing action type'}, status=status.HTTP_400_BAD_REQUEST)
try:
ProjectStateChangeService.change_user_project_rel(action_type, project, user, add)
ProjectStateChangeService.change_count_project_state(action_type, project, add)
except ValueError as e:
return Response({'message': str(e)}, status=status.HTTP_400_BAD_REQUEST)
return Response({
'message': f'{action_type} {"added" if add else "removed"}'
}, status=status.HTTP_200_OK)
class ProjectInvitationAPIView(APIView):
# 팀원 초대 수락/거절
@transaction.atomic
def post(self, request):
accept = request.data['accept']
project_invitation = get_object_or_404(ProjectInvitation, id=request.data['project_invitation_id'])
new_member = project_invitation.to_user
project = project_invitation.project
if request.user != new_member:
return Response({"message": "Not account owner"}, status=status.HTTP_403_FORBIDDEN)
if project_invitation.status != InvitationStatus.PENDING:
return Response({"message": "already handled invitation"}, status=status.HTTP_400_BAD_REQUEST)
if accept:
project_invitation.status = InvitationStatus.ACCEPTED
project_invitation.save()
serializer = ProjectTeamSerializer(data={'user':new_member.id, 'project':project.id})
if serializer.is_valid():
serializer.save()
return Response({"message": "invitation accepted"}, status=status.HTTP_200_OK)
else:
project_invitation.status = InvitationStatus.REJECTED
project_invitation.save()
return Response({"message": "invitation rejected"}, status=status.HTTP_200_OK)