<commit_before>from django.core.exceptions import ObjectDoesNotExist
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import status


from .models import Company, Team, Employee
from .serializers import TeamSerializer, CompanySerializer, EmployeeSerializer

class TeamView(APIView):

    queryset = Team.objects.all()
    serializer_class = TeamSerializer

    def get(self, request, format=None, **kwargs):
        #antes pegar o company id para verificar se o time pertence aquela empresa
        checkIfCompanyExists(kwargs.get('company_id'))

        if kwargs.get('team_id'):
            try:
                #verificar aqui se esse team pertence a company?
                team = Team.objects.get(id=kwargs['team_id'])
                team_data = TeamSerializer(team)
                return Response(team_data.data)
            except ObjectDoesNotExist:
                return Response({"error": "could not find team"}    , status=400)
        else:
            all_teams = Team.objects.all()
            all_teams_serialized = TeamSerializer(all_teams, many=True)
            return Response(all_teams_serialized.data)

    def post(self, request, format=None, **kwargs):
        response = Response()
        new_team = Team(**kwargs)
        new_team.save()

        if new_team:
            response.status_code = 201
        else:
            response.status_code = 400
        return response

    def delete(self, request, format=None, **kwargs):
        response = Response()
        if kwargs.get("team_id"):
            team = Team.objects.get(id=kwargs['team_id'])
            if team:
                team.delete()
                response.status_code = 204
            else:
                pass
        else:
            response.status_code = 400
        return response

class CompanyView(APIView):
    serializer_class = CompanySerializer

    def get(self, request, format=None):
        companies = Company.objects.all()
        serializer = CompanySerializer(companies, many=True)
        return Response(serializer.data)

    def post(self, request, format=None):
        serializer = CompanySerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
      
    def delete(self, request, format=None, **kwargs):
        response = Response()
        if kwargs.get("company_id"):
            company = Company.objects.get(id=kwargs['company_id'])
            if company:
                company.delete()
                response.status_code = 204
            else:
                pass
        else:
            response.status_code = 400
        return response

class EmployeeView(APIView):
    serializer_class = EmployeeSerializer
    def get(self, request, format=None, **kwargs):
        checkIfCompanyExists(kwargs.get('company_id'))
<commit_msg>adicionando delete view para company<commit_after>from django.core.exceptions import ObjectDoesNotExist
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import status


from .models import Company, Team, Employee
from .serializers import TeamSerializer, CompanySerializer, EmployeeSerializer

class TeamView(APIView):

    queryset = Team.objects.all()
    serializer_class = TeamSerializer

    def get(self, request, format=None, **kwargs):
        #antes pegar o company id para verificar se o time pertence aquela empresa
        checkIfCompanyExists(kwargs.get('company_id'))

        if kwargs.get('team_id'):
            try:
                #verificar aqui se esse team pertence a company?
                team = Team.objects.get(id=kwargs['team_id'])
                team_data = TeamSerializer(team)
                return Response(team_data.data)
            except ObjectDoesNotExist:
                return Response({"error": "could not find team"}    , status=400)
        else:
            all_teams = Team.objects.all()
            all_teams_serialized = TeamSerializer(all_teams, many=True)
            return Response(all_teams_serialized.data)

    def post(self, request, format=None, **kwargs):
        response = Response()
        new_team = Team(**kwargs)
        new_team.save()

        if new_team:
            response.status_code = 201
        else:
            response.status_code = 400
        return response

    def delete(self, request, format=None, **kwargs):
        response = Response()
        if kwargs.get("team_id"):
            team = Team.objects.get(id=kwargs['team_id'])
            if team:
                team.delete()
                response.status_code = 204
            else:
                pass
        else:
            response.status_code = 400
        return response

class CompanyView(APIView):
    serializer_class = CompanySerializer

    def get(self, request, format=None):
        companies = Company.objects.all()
        serializer = CompanySerializer(companies, many=True)
        return Response(serializer.data)

    def post(self, request, format=None):
        serializer = CompanySerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
      
    def delete(self, request, format=None, **kwargs):
        response = Response()
        if kwargs.get("company_id"):
            company = Company.objects.get(id=kwargs['company_id'])

            if company.owner.id == kwargs.get("owner_id") and company.editors.filter(kwargs.get("editor_id")):
                company.delete()
                response.status_code = 204
            else:
                pass
        else:
            response.status_code = 400
        return response

class EmployeeView(APIView):
    serializer_class = EmployeeSerializer
    def get(self, request, format=None, **kwargs):
        checkIfCompanyExists(kwargs.get('company_id'))

{'fraction_non_alphanumeric': 0.08886006161829091, 'fraction_numerical': 0.008756283444138155, 'mean_word_length': 2.3778751369112814, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1320128', 'n_tokens_mistral': 1691, 'n_tokens_neox': 1600, 'n_words': 391}