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'
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)
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)
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))
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())
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)
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)