Esempio n. 1
0
def test_iter_callsign_choices():
    choices = iter_callsign_choices('FooBar')
    assert next(choices) == 'FB'
    assert next(choices) == 'FB2'
    assert next(choices) == 'FB3'
    assert next(choices) == 'FB4'

    choices = iter_callsign_choices('FooBarBaz')
    assert next(choices) == 'FBB'
    assert next(choices) == 'FBB2'
    assert next(choices) == 'FBB3'
    assert next(choices) == 'FBB4'

    choices = iter_callsign_choices('Grml')
    assert next(choices) == 'GR'
    assert next(choices) == 'GRM'
    assert next(choices) == 'GR2'
    assert next(choices) == 'GRM2'

    choices = iter_callsign_choices('42')
    assert next(choices) == 'PR'
    assert next(choices) == 'PR2'
    assert next(choices) == 'PR3'

    choices = iter_callsign_choices('GetHub')
    assert next(choices) == 'GH2'
    assert next(choices) == 'GH3'
Esempio n. 2
0
def test_iter_callsign_choices():
    choices = iter_callsign_choices('FooBar')
    assert next(choices) == 'FB'
    assert next(choices) == 'FB2'
    assert next(choices) == 'FB3'
    assert next(choices) == 'FB4'

    choices = iter_callsign_choices('FooBarBaz')
    assert next(choices) == 'FBB'
    assert next(choices) == 'FBB2'
    assert next(choices) == 'FBB3'
    assert next(choices) == 'FBB4'

    choices = iter_callsign_choices('Grml')
    assert next(choices) == 'GR'
    assert next(choices) == 'GRM'
    assert next(choices) == 'GR2'
    assert next(choices) == 'GRM2'

    choices = iter_callsign_choices('42')
    assert next(choices) == 'PR'
    assert next(choices) == 'PR2'
    assert next(choices) == 'PR3'

    choices = iter_callsign_choices('GetHub')
    assert next(choices) == 'GH2'
    assert next(choices) == 'GH3'
Esempio n. 3
0
    def clean_callsign(self):
        callsign = self.cleaned_data.get('callsign')
        if not callsign:
            it = iter_callsign_choices(self.cleaned_data.get('name') or '')
            for potential_callsign in it:
                try:
                    Project.objects.get(
                        organization=self.organization,
                        callsign=potential_callsign
                    )
                except Project.DoesNotExist:
                    return potential_callsign

        callsign = validate_callsign(callsign)
        if callsign is None:
            raise forms.ValidationError(_('Callsign must be between 2 '
                                          'and 6 letters'))
        try:
            other = Project.objects.get(
                organization=self.organization,
                callsign=callsign
            )
        except Project.DoesNotExist:
            return callsign
        raise forms.ValidationError(_('Another project (%s) is already '
                                      'using that callsign') % other.name)
Esempio n. 4
0
    def post(self, request, team):
        """
        Create a New Project
        ````````````````````

        Create a new project bound to a team.

        :pparam string organization_slug: the slug of the organization the
                                          team belongs to.
        :pparam string team_slug: the slug of the team to create a new project
                                  for.
        :param string name: the name for the new project.
        :param string slug: optionally a slug for the new project.  If it's
                            not provided a slug is generated from the name.
        :param string callsign: optionally a callsign for the new project.
        :auth: required
        """
        serializer = ProjectSerializer(data=request.DATA)

        if serializer.is_valid():
            result = serializer.object

            callsign = result.get('callsign')
            if not callsign:
                it = iter_callsign_choices(result['name'])
                for callsign in it:
                    try:
                        Project.objects.get(
                            organization=team.organization,
                            callsign=callsign
                        )
                    except Project.DoesNotExist:
                        break

            project = Project.objects.create(
                name=result['name'],
                slug=result.get('slug'),
                callsign=callsign,
                organization=team.organization,
                team=team
            )

            # XXX: create sample event?

            self.create_audit_entry(
                request=request,
                organization=team.organization,
                target_object=project.id,
                event=AuditLogEntryEvent.PROJECT_ADD,
                data=project.get_audit_log_data(),
            )

            return Response(serialize(project, request.user), status=201)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 5
0
def get_callsigns(projects):
    rv = {}

    for project in projects:
        if project.callsign is not None:
            rv[project.callsign] = project.id
            continue
        for callsign in iter_callsign_choices(project.name):
            if callsign in rv:
                continue
            rv[callsign] = project.id
            break

    return dict((v, k) for k, v in six.iteritems(rv))
Esempio n. 6
0
def get_callsigns(projects):
    rv = {}

    for project in projects:
        if project.callsign is not None:
            rv[project.callsign] = project.id
            continue
        for callsign in iter_callsign_choices(project.name):
            if callsign in rv:
                continue
            rv[callsign] = project.id
            break

    return dict((v, k) for k, v in rv.iteritems())
Esempio n. 7
0
    def post(self, request, team):
        """
        Create a New Project
        ````````````````````

        Create a new project bound to a team.

        :pparam string organization_slug: the slug of the organization the
                                          team belongs to.
        :pparam string team_slug: the slug of the team to create a new project
                                  for.
        :param string name: the name for the new project.
        :param string slug: optionally a slug for the new project.  If it's
                            not provided a slug is generated from the name.
        :param string callsign: optionally a callsign for the new project.
        :auth: required
        """
        serializer = ProjectSerializer(data=request.DATA)

        if serializer.is_valid():
            result = serializer.object

            callsign = result.get('callsign')
            if not callsign:
                it = iter_callsign_choices(result['name'])
                for callsign in it:
                    try:
                        Project.objects.get(organization=team.organization,
                                            callsign=callsign)
                    except Project.DoesNotExist:
                        break

            project = Project.objects.create(name=result['name'],
                                             slug=result.get('slug'),
                                             callsign=callsign,
                                             organization=team.organization,
                                             team=team)

            # XXX: create sample event?

            self.create_audit_entry(
                request=request,
                organization=team.organization,
                target_object=project.id,
                event=AuditLogEntryEvent.PROJECT_ADD,
                data=project.get_audit_log_data(),
            )

            return Response(serialize(project, request.user), status=201)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 8
0
    def clean_callsign(self):
        callsign = self.cleaned_data.get('callsign')
        if not callsign:
            it = iter_callsign_choices(self.cleaned_data.get('name') or '')
            for potential_callsign in it:
                try:
                    Project.objects.get(organization=self.organization,
                                        callsign=potential_callsign)
                except Project.DoesNotExist:
                    return potential_callsign

        callsign = validate_callsign(callsign)
        if callsign is None:
            raise forms.ValidationError(
                _('Callsign must be between 2 '
                  'and 6 letters'))
        try:
            other = Project.objects.get(organization=self.organization,
                                        callsign=callsign)
        except Project.DoesNotExist:
            return callsign
        raise forms.ValidationError(
            _('Another project (%s) is already '
              'using that callsign') % other.name)