def update_project_status(self, request, pk): project = Project.objects.get(pk=pk) user = self.request.user if (user.is_master or user == project.creator or user in project.members.all()): old_status = project.status try: new_status = self.request.data['status'] except KeyError: return Response( {'message': 'New status not provided for the project.'}, status=status.HTTP_400_BAD_REQUEST) if new_status != old_status: project.status = new_status project.save() status_updated_by = user projectPageLink = f"{FRONTEND_URL}/projects/{slugify(project.name)}" email_notification = threading.Thread( target=project_status_update, args=(project.name, project.link, projectPageLink, old_status, new_status, status_updated_by, User.objects.all())) email_notification.start() serializer = ProjectSerializer(project) return Response(serializer.data) else: return Response( { 'message': 'You do not have permission to perform this action' }, status=status.HTTP_401_UNAUTHORIZED)
def get(self, request): query = self.request.query_params.get('q', None) if query is not None: issue_query = Q(title__icontains=query) project_query = Q(name__icontains=query) user_query = Q(name__icontains=query) issue_results = IssueSerializer( Issue.objects.filter(issue_query).distinct(), many=True ).data project_results = ProjectSerializer( Project.objects.filter(project_query).distinct(), many=True ).data user_results = UserSerializer( User.objects.filter(user_query).distinct(), many=True ).data return Response({ 'issues': issue_results, 'projects': project_results, 'users': user_results }) return Response({})
def update_project_members(self, request, pk): project = Project.objects.get(pk=pk) user = self.request.user if (user.is_master or user == project.creator or user in project.members.all()): old_member_list = list(project.members.all()) try: members = self.request.data['members'] except KeyError: return Response({'message': 'No member list provided.'}, status=status.HTTP_400_BAD_REQUEST) if len(members) == 0: serializer = ProjectSerializer(project) return Response(serializer.data) if set(old_member_list) != set(members): new_member_list = list(set(members)) for i in range(len(new_member_list)): new_member_list[i] = User.objects.get( id=new_member_list[i]) members_to_be_added = list( set(new_member_list).difference(set(old_member_list))) # members_to_be_removed = list(set(old_member_list).difference(set(new_member_list))) project.members.set(new_member_list) project.save() projectPageLink = "http://127.0.0.1:3000/projects/" + \ slugify(project.name) # can also send email to those who have been removed from the project (members_to_be_removed) # but not implementing this for now. email_notification = threading.Thread( target=add_project_member, args=(project.name, project.link, projectPageLink, user.name, project.members.all(), project.status, members_to_be_added)) email_notification.start() serializer = ProjectSerializer(project) return Response(serializer.data) else: return Response( { 'message': 'You do not have permission to perform this action' }, status=status.HTTP_401_UNAUTHORIZED)
def create(self, request, *args, **kwargs): project = request.data project['creator'] = request.user.id serializer = ProjectSerializer(data=project) if serializer.is_valid(): project_slug = slugify(project['name']) slugs = [] for p in Project.objects.all(): slugs.append(slugify(p.name)) if project_slug in slugs: return Response( {"message": "Project names should be strictly unique."}, status=status.HTTP_400_BAD_REQUEST) else: self.perform_create(serializer) return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)