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 put(self, request, organization): """ Update Short IDs ```````````````` Updates the call signs of projects within the organization. :pparam string organization_slug: the slug of the organization the short ID should be looked up in. :param callsigns: a dictionary of project IDs to their intended callsigns. :auth: required """ callsigns = request.DATA.get('callsigns', {}) for project_id, callsign in callsigns.iteritems(): callsign = validate_callsign(callsign) if callsign is None: return Response({'detail': 'invalid callsign "%s"' % callsign}, status=400) callsigns[project_id] = callsign if len(callsigns) != len(set(callsigns.values())): return Response({'detail': 'Duplicate callsigns'}, status=400) project_q = organization.project_set.filter( pk__in=[int(x) for x in callsigns]) rv = {} with transaction.atomic(): projects = {} # Clear out all call-signs first so that we can move them # around through the uniqueness for project in project_q: projects[str(project.id)] = project project.callsign = None project.save() # Set new ones for project_id, callsign in callsigns.iteritems(): project = projects.get(project_id) if project is None: continue other = Project.objects.filter( callsign=callsign, organization=organization).exclude(id=project.id).first() if other is not None: if len(callsigns) != len(callsigns.values()): return Response( {'detail': 'Duplicate callsign %s' % callsign}, status=400) project.callsign = callsign project.update_option('sentry:reviewed-callsign', True) project.save() rv[project_id] = callsign return Response({'updated_short_ids': rv})
def clean_callsign(self): # If no callsign was provided we go with the old one. This # primarily exists so that people without the callsign feature # enabled will not screw up their callsigns. callsign = self.cleaned_data.get('callsign') if not callsign: return self.instance.callsign callsign = validate_callsign(callsign) if callsign is None: raise forms.ValidationError(_('Callsign must be between 2 ' 'and 6 letters')) other = Project.objects.filter( callsign=callsign, organization=self.organization ).exclude(id=self.instance.id).first() if other is not None: raise forms.ValidationError(_('Another project (%s) is already ' 'using that callsign') % other.name) return callsign
def clean_callsign(self): # If no callsign was provided we go with the old one. This # primarily exists so that people without the callsign feature # enabled will not screw up their callsigns. callsign = self.cleaned_data.get('callsign') if not callsign: return self.instance.callsign callsign = validate_callsign(callsign) if callsign is None: raise forms.ValidationError( _('Callsign must be between 2 ' 'and 6 letters')) other = Project.objects.filter(callsign=callsign, organization=self.organization).exclude( id=self.instance.id).first() if other is not None: raise forms.ValidationError( _('Another project (%s) is already ' 'using that callsign') % other.name) return callsign
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 validate(self, data): callsign = data.get('callsign') if callsign and validate_callsign(callsign) is None: raise serializers.ValidationError( 'Callsign must be between 2 and 6 characters long.') return data
def put(self, request, organization): """ Update Short IDs ```````````````` Updates the call signs of projects within the organization. :pparam string organization_slug: the slug of the organization the short ID should be looked up in. :param callsigns: a dictionary of project IDs to their intended callsigns. :auth: required """ callsigns = request.DATA.get('callsigns', {}) for project_id, callsign in callsigns.iteritems(): callsign = validate_callsign(callsign) if callsign is None: return Response({'detail': 'invalid callsign "%s"' % callsign}, status=400) callsigns[project_id] = callsign if len(callsigns) != len(set(callsigns.values())): return Response({'detail': 'Duplicate callsigns'}, status=400) project_q = organization.project_set.filter( pk__in=[int(x) for x in callsigns] ) rv = {} with transaction.atomic(): projects = {} # Clear out all call-signs first so that we can move them # around through the uniqueness for project in project_q: projects[str(project.id)] = project project.callsign = None project.save() # Set new ones for project_id, callsign in callsigns.iteritems(): project = projects.get(project_id) if project is None: continue other = Project.objects.filter( callsign=callsign, organization=organization ).exclude(id=project.id).first() if other is not None: if len(callsigns) != len(callsigns.values()): return Response({'detail': 'Duplicate callsign %s' % callsign}, status=400) project.callsign = callsign project.update_option('sentry:reviewed-callsign', True) project.save() rv[project_id] = callsign return Response({ 'updated_short_ids': rv })