<commit_before>from __future__ import absolute_import

import logging

from rest_framework import serializers, status
from rest_framework.response import Response

from sentry.api.base import DocSection
from sentry.api.bases.team import TeamEndpoint
from sentry.api.decorators import sudo_required
from sentry.api.serializers import serialize
from sentry.models import AuditLogEntryEvent, Team, TeamStatus
from sentry.tasks.deletion import delete_team
from sentry.utils.apidocs import scenario, attach_scenarios


@scenario('GetTeam')
def get_team_scenario(runner):
    runner.request(
        method='GET',
        path='/teams/%s/%s/' % (
            runner.org.slug, runner.default_team.slug)
    )


@scenario('UpdateTeam')
def update_team_scenario(runner):
    team = runner.utils.create_team('The Obese Philosophers', runner.org)
    runner.request(
        method='PUT',
        path='/teams/%s/%s/' % (
            runner.org.slug, team.slug),
        data={
            'name': 'The Inflated Philosophers'
        }
    )


class TeamSerializer(serializers.ModelSerializer):
    class Meta:
        model = Team
        fields = ('name', 'slug')

    def validate_slug(self, attrs, source):
        value = attrs[source]
        if Team.objects.filter(slug=value).exclude(id=self.object.id):
            raise serializers.ValidationError('The slug "%s" is already in use.' % (value,))
        return attrs


class TeamDetailsEndpoint(TeamEndpoint):
    doc_section = DocSection.TEAMS

    @attach_scenarios([get_team_scenario])
    def get(self, request, team):
        """
        Retrieve a Team
        ```````````````

        Return details on an individual team.

        :pparam string organization_slug: the slug of the organization the
                                          team belongs to.
        :pparam string team_slug: the slug of the team to get.
        :auth: required
        """
        context = serialize(team, request.user)
        context['organization'] = serialize(team.organization, request.user)<commit_msg>Validate duplicate slugs correctly in team details API<commit_after>from __future__ import absolute_import

import logging

from rest_framework import serializers, status
from rest_framework.response import Response

from sentry.api.base import DocSection
from sentry.api.bases.team import TeamEndpoint
from sentry.api.decorators import sudo_required
from sentry.api.serializers import serialize
from sentry.models import AuditLogEntryEvent, Team, TeamStatus
from sentry.tasks.deletion import delete_team
from sentry.utils.apidocs import scenario, attach_scenarios


@scenario('GetTeam')
def get_team_scenario(runner):
    runner.request(
        method='GET',
        path='/teams/%s/%s/' % (
            runner.org.slug, runner.default_team.slug)
    )


@scenario('UpdateTeam')
def update_team_scenario(runner):
    team = runner.utils.create_team('The Obese Philosophers', runner.org)
    runner.request(
        method='PUT',
        path='/teams/%s/%s/' % (
            runner.org.slug, team.slug),
        data={
            'name': 'The Inflated Philosophers'
        }
    )


class TeamSerializer(serializers.ModelSerializer):
    class Meta:
        model = Team
        fields = ('name', 'slug')

    def validate_slug(self, attrs, source):
        value = attrs[source]
        qs = Team.objects.filter(
            slug=value,
            organization=self.object.organization,
        ).exclude(id=self.object.id)
        if qs.exists():
            raise serializers.ValidationError('The slug "%s" is already in use.' % (value,))
        return attrs


class TeamDetailsEndpoint(TeamEndpoint):
    doc_section = DocSection.TEAMS

    @attach_scenarios([get_team_scenario])
    def get(self, request, team):
        """
        Retrieve a Team
        ```````````````

        Return details on an individual team.

        :pparam string organization_slug: the slug of the organization the
                                          team belongs to.
        :pparam string team_slug: the slug of the team to get.
        :auth: required
        """
        context = serialize(team, request.user)
        context['organization'] = serialize(team.organization, request.user)
{'fraction_non_alphanumeric': 0.10127478753541076, 'fraction_numerical': 0.0, 'mean_word_length': 2.806828391734052, 'pattern_counts': {'":': 0, '<': 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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3300028', 'n_tokens_mistral': 1202, 'n_tokens_neox': 1176, 'n_words': 307}