def perform_create(self, serializer):
        project = serializer.save()

        mailerInstance = Mailer()
        link = "http://localhost:3000/projects/" + project.slug
        x = threading.Thread(target=mailerInstance.newProjectUpdate, args=(project.title, link, project.members.all()))
        x.start()
    def toggle_ban(self, request, pk):
        if request.user.is_superuser:
            user = User.objects.get(pk=pk)
            if user == request.user:
                return Response({"status": "You cannot change your own status!"})

            if user.banned:
                user.banned = False
            else:
                user.banned = True
                user.is_superuser = False
                user.is_staff = False

            user.save()

            mailer = Mailer()

            if user.banned:
                x = threading.Thread(target=mailer.banOrAdmitUser,
                                     args=(user.email,
                                           user.full_name,
                                           "banned",
                                           request.user.full_name))
            else:
                x = threading.Thread(target=mailer.banOrAdmitUser,
                                     args=(user.email,
                                           user.full_name,
                                           "admit",
                                           request.user.full_name))
            x.start()
            return Response({"status": "Status updated"}, status=status.HTTP_200_OK)
        else:
            return Response({"status": "You're not an admin"}, status=status.HTTP_403_FORBIDDEN)
    def resolve_or_reopen(self, request, pk):
        user_instance = self.request.user
        issue = Issue.objects.get(pk=pk)

        if user_instance.is_superuser or user_instance == issue.reported_by or user_instance in issue.project.members.all(
        ):
            pass
        else:
            return Response({"Status": "Not authorized"})

        if issue.resolved:
            issue.resolved = False
        else:
            issue.resolved = True

        issue.resolved_by = user_instance
        issue.save()

        project = issue.project
        link = "http://localhost:3000/projects/" + project.slug
        mailer = Mailer()

        if issue.resolved:
            x = threading.Thread(target=mailer.bugStatusChanged,
                                 args=(project.title, link, issue.subject,
                                       "resolved", request.user.full_name,
                                       project.members.all()))
        else:
            x = threading.Thread(target=mailer.bugStatusChanged,
                                 args=(project.title, link, issue.subject,
                                       "reopened", request.user.full_name,
                                       project.members.all()))
        x.start()
        ser = IssueGetSerializer(issue)
        return Response(ser.data, status=status.HTTP_200_OK)
    def destroy(self, request, *args, **kwargs):
        try:
            issue = self.get_object()
            project = issue.project
            user = self.request.user

            if user.is_superuser or user == issue.reported_by or user in project.members.all(
            ):
                pass
            else:
                return Response({"Status": "Not authorized"})

            link = "http://localhost:3000/projects/" + project.slug
            editor = issue.editorID
            images = Image.objects.filter(editorID=editor)

            for i in images:
                i.delete()
                url_tbd = BASE_DIR + i.url.url
                os.remove(url_tbd)

            mailer = Mailer()
            x = threading.Thread(target=mailer.bugStatusChanged,
                                 args=(project.title, link, issue.subject,
                                       "deleted", request.user.full_name,
                                       project.members.all()))
            x.start()
            self.perform_destroy(issue)

        except Http404:
            pass

        return Response(status=status.HTTP_204_NO_CONTENT)
    def assign_issue(self, request, pk):
        assign_to = self.request.query_params.get('assign_to')
        issue = Issue.objects.get(pk=pk)

        try:
            user = User.objects.get(pk=assign_to)
        except User.DoesNotExist:
            return Response({'Detail': 'User does not exist'},
                            status=status.HTTP_406_NOT_ACCEPTABLE)

        if user in issue.project.members.all():
            issue.assigned_to = user
            issue.save()

            assignment_link = "http://localhost:3000/mypage?show=my-assignments"
            project_instance = issue.project
            assigned = issue.assigned_to

            mailer = Mailer()
            x = threading.Thread(target=mailer.bugAssigned,
                                 args=(project_instance.title, assignment_link,
                                       issue.subject, assigned.full_name,
                                       assigned.email))
            x.start()

            return Response({'Detail': 'Assignment Successful'},
                            status=status.HTTP_202_ACCEPTED)
        else:
            return Response({'Detail': 'User not a team member'},
                            status=status.HTTP_406_NOT_ACCEPTABLE)
 def perform_create(self, serializer):
     issue_instance = serializer.save()
     project = issue_instance.project
     link = "http://localhost:3000/projects/" + project.slug
     mailer = Mailer()
     x = threading.Thread(
         target=mailer.newBugReported,
         args=(project.title, link, issue_instance.reported_by.full_name,
               issue_instance.subject, project.members.all()))
     x.start()
    def deploy_project(self, request, pk):
        project = Project.objects.get(pk=pk)

        if request.user.is_superuser or request.user in project.members.all():
            pass
        else:
            return Response({"Status": "Not authorized."}, status=status.HTTP_403_FORBIDDEN)

        if not Issue.objects.filter(project=project, resolved=False):
            project.deployed = True
            project.save()

            mailer = Mailer()
            x = threading.Thread(target=mailer.deployProject,
                                 args=(project.title,
                                       request.user.full_name,
                                       project.members.all()))
            x.start()

            return Response({'Status': 'Project successfully deployed'}, status=status.HTTP_202_ACCEPTED)
        else:
            return Response({'Status': 'All issues are not resolved for this project'})