Ejemplo n.º 1
0
 def clean_name(self):
     name = self.cleaned_data['name']
     invalid_names = settings.CADASTA_INVALID_ENTITY_NAMES
     if slugify(name, allow_unicode=True) in invalid_names:
         raise forms.ValidationError(
             _("Organization name cannot be “Add” or “New”."))
     return name
Ejemplo n.º 2
0
    def validate_name(self, value):

        # Check that name is not restricted
        invalid_names = settings.CADASTA_INVALID_ENTITY_NAMES
        if slugify(value, allow_unicode=True) in invalid_names:
            raise serializers.ValidationError(
                _("Project name cannot be “Add” or “New”."))

        # Check that name is unique org-wide
        # (Explicit validation: see comment in the Meta class)
        is_create = not self.instance
        if is_create:
            org_slug = self.context['organization'].slug
        else:
            org_slug = self.instance.organization.slug
        queryset = Project.objects.filter(
            organization__slug=org_slug,
            name=value,
        )
        if is_create:
            not_unique = queryset.exists()
        else:
            not_unique = queryset.exclude(id=self.instance.id).exists()
        if not_unique:
            raise serializers.ValidationError(
                _("Project with this name already exists "
                  "in this organization."))

        return value
Ejemplo n.º 3
0
 def clean_name(self):
     name = self.cleaned_data['name']
     invalid_names = settings.CADASTA_INVALID_ENTITY_NAMES
     if slugify(name, allow_unicode=True) in invalid_names:
         raise forms.ValidationError(
             _("Organization name cannot be “Add” or “New”."))
     return name
Ejemplo n.º 4
0
    def validate_name(self, value):

        # Check that name is not restricted
        invalid_names = settings.CADASTA_INVALID_ENTITY_NAMES
        if slugify(value, allow_unicode=True) in invalid_names:
            raise serializers.ValidationError(
                _("Project name cannot be “Add” or “New”."))

        # Check that name is unique org-wide
        # (Explicit validation: see comment in the Meta class)
        is_create = not self.instance
        if is_create:
            org_slug = self.context['organization'].slug
        else:
            org_slug = self.instance.organization.slug
        queryset = Project.objects.filter(
            organization__slug=org_slug,
            name=value,
        )
        if is_create:
            not_unique = queryset.exists()
        else:
            not_unique = queryset.exclude(id=self.instance.id).exists()
        if not_unique:
            raise serializers.ValidationError(
                _("Project with this name already exists "
                  "in this organization."))

        return value
Ejemplo n.º 5
0
 def handle_error(self, e):
   logging.exception(e)
   try:
     e.code
   except AttributeError:
     e.code = 500
     e.name = e.description = 'Internal Server Error'
   return util.jsonpify({
       'status': 'error',
       'error_code': e.code,
       'error_name': util.slugify(e.name),
       'error_message': e.name,
       'error_class': e.__class__.__name__,
       'description': e.description,
     }), e.code
Ejemplo n.º 6
0
    def test_slugify(self):

        # text are slugified.
        assert "hey-im-a-feed" == slugify("Hey! I'm a feed!!")
        assert "you-and-me-n-every_feed" == slugify("You & Me n Every_Feed")
        assert "money-honey" == slugify("Money $$$       Honey")
        assert "some-title-somewhere" == slugify("Some (???) Title Somewhere")
        assert "sly-and-the-family-stone" == slugify("sly & the family stone")

        # The results can be pared down according to length restrictions.
        assert "happ" == slugify("Happy birthday", length_limit=4)

        # Slugified text isn't altered
        assert "already-slugified" == slugify("already-slugified")
Ejemplo n.º 7
0
    def validate_name(self, value):
        invalid_names = settings.CADASTA_INVALID_ENTITY_NAMES
        if slugify(value, allow_unicode=True) in invalid_names:
            raise serializers.ValidationError(
                _("Organization name cannot be “Add” or “New”."))

        is_create = not self.instance
        queryset = Organization.objects.filter(name__iexact=value)
        if is_create:
            not_unique = queryset.exists()
        else:
            not_unique = queryset.exclude(id=self.instance.id).exists()
        if not_unique:
            raise serializers.ValidationError(
                _("Organization with this name already exists."))

        return value
    def test_slug_field_is_set(self):
        request = APIRequestFactory().post('/')
        user = UserFactory.create()
        setattr(request, 'user', user)

        org_data = {'name': "Test Organization"}

        serializer = serializers.OrganizationSerializer(
            data=org_data, context={'request': request})
        serializer.is_valid(raise_exception=True)
        serializer.save()

        org_instance = serializer.instance
        assert org_instance.slug == slugify(org_data['name'],
                                            allow_unicode=True)
        assert OrganizationRole.objects.filter(
            organization=org_instance).count() == 1
Ejemplo n.º 9
0
    def clean_name(self):
        name = self.cleaned_data['name']
        invalid_names = settings.CADASTA_INVALID_ENTITY_NAMES
        if slugify(name, allow_unicode=True) in invalid_names:
            raise forms.ValidationError(
                _("Organization name cannot be “Add” or “New”."))

        is_create = not self.instance.id
        queryset = Organization.objects.filter(name__iexact=name)
        if is_create:
            not_unique = queryset.exists()
        else:
            not_unique = queryset.exclude(id=self.instance.id).exists()
        if not_unique:
            raise forms.ValidationError(
                self.fields['name'].error_messages['unique'])

        return name
Ejemplo n.º 10
0
 def save(self, *args, **kwargs):
     """
     Override save method to automatically add UPC and unique slug
     """
     if not self.upc:
         self.upc = upc_generator()
     
     count = 1
     if not self.slug:
         slug = slugify(self.name)
         self.slug = slug
         while True:
             existing = Product.objects.filter(slug=self.slug)
             if len(existing) == 0:
                 break
             self.slug = "%s-%s" % (slug, count + 1)
             count += 1
     return super(Product, self).save(*args, **kwargs)
Ejemplo n.º 11
0
    def clean_name(self):
        name = self.cleaned_data['name']

        # Check that name is not restricted
        invalid_names = settings.CADASTA_INVALID_ENTITY_NAMES
        if slugify(name, allow_unicode=True) in invalid_names:
            raise forms.ValidationError(
                _("Project name cannot be “Add” or “New”."))

        # Check that name is unique org-wide
        # (Explicit validation because we are using a wizard and the
        # unique_together validation cannot occur in the proper page)
        not_unique = Project.objects.filter(name__iexact=name).exclude(
            id=self.instance.id).exists()
        if not_unique:
            raise forms.ValidationError(
                _("Project with this name already exists"))

        return name
Ejemplo n.º 12
0
    def test_slug_field_is_set(self):
        request = APIRequestFactory().post('/')
        user = UserFactory.create()
        setattr(request, 'user', user)

        org_data = {'name': "Test Organization"}

        serializer = serializers.OrganizationSerializer(
            data=org_data,
            context={'request': request}
        )
        serializer.is_valid(raise_exception=True)
        serializer.save()

        org_instance = serializer.instance
        assert org_instance.slug == slugify(
            org_data['name'], allow_unicode=True)
        assert OrganizationRole.objects.filter(
            organization=org_instance).count() == 1
Ejemplo n.º 13
0
    def save(self, *args, **kwargs):
        max_length = self._meta.get_field('slug').max_length
        if not self.slug:
            self.slug = slugify(
                self.name, max_length=max_length, allow_unicode=True
            )

        orig_slug = self.slug

        if not self.id or self.__original_slug != self.slug:
            for x in itertools.count(1):
                if not type(self).objects.filter(slug=self.slug).exists():
                    break
                slug_length = max_length - int(math.log10(x)) - 2
                trial_slug = orig_slug[:slug_length]
                self.slug = '{}-{}'.format(trial_slug, x)

        self.__original_slug = self.slug

        return super().save(*args, **kwargs)
Ejemplo n.º 14
0
    def save(self, *args, **kwargs):
        max_length = self._meta.get_field('slug').max_length
        if not self.slug:
            self.slug = slugify(self.name,
                                max_length=max_length,
                                allow_unicode=True)

        orig_slug = self.slug

        if not self.id or self.__original_slug != self.slug:
            for x in itertools.count(1):
                if not type(self).objects.filter(slug=self.slug).exists():
                    break
                slug_length = max_length - int(math.log10(x)) - 2
                trial_slug = orig_slug[:slug_length]
                self.slug = '{}-{}'.format(trial_slug, x)

        self.__original_slug = self.slug

        return super().save(*args, **kwargs)
Ejemplo n.º 15
0
    def validate_name(self, value):

        # Check that name is not restricted
        invalid_names = settings.CADASTA_INVALID_ENTITY_NAMES
        if slugify(value, allow_unicode=True) in invalid_names:
            raise serializers.ValidationError(
                _("Project name cannot be “Add” or “New”."))

        # Check that name is unique globally
        # (Explicit validation: see comment in the Meta class)
        is_create = not self.instance
        queryset = Project.objects.filter(name__iexact=value)
        if is_create:
            not_unique = queryset.exists()
        else:
            not_unique = queryset.exclude(id=self.instance.id).exists()
        if not_unique:
            raise serializers.ValidationError(
                _("Project with this name already exists"))

        return value
Ejemplo n.º 16
0
    def clean_name(self):
        name = self.cleaned_data['name']

        # Check that name is not restricted
        invalid_names = settings.CADASTA_INVALID_ENTITY_NAMES
        if slugify(name, allow_unicode=True) in invalid_names:
            raise forms.ValidationError(
                _("Project name cannot be “Add” or “New”."))

        # Check that name is unique org-wide
        # (Explicit validation because we are using a wizard and the
        # unique_together validation cannot occur in the proper page)
        not_unique = Project.objects.filter(
            organization__slug=self.instance.organization.slug,
            name=name,
        ).exclude(id=self.instance.id).exists()
        if not_unique:
            raise forms.ValidationError(
                _("Project with this name already exists "
                  "in this organization."))

        return name
Ejemplo n.º 17
0
    def unique_username(self, first_name, last_name, splitter='-'):
        """A username for user with given name"""

        username = slugify(first_name + splitter + last_name, max_len=30)
        username = unique_field(username, User, 'username')
        return username
Ejemplo n.º 18
0
 def validate_name(self, value):
     invalid_names = settings.CADASTA_INVALID_ENTITY_NAMES
     if slugify(value, allow_unicode=True) in invalid_names:
         raise serializers.ValidationError(
             _("Organization name cannot be “Add” or “New”."))
     return value
Ejemplo n.º 19
0
 def validate_name(self, value):
     invalid_names = settings.CADASTA_INVALID_ENTITY_NAMES
     if slugify(value, allow_unicode=True) in invalid_names:
         raise serializers.ValidationError(
             _("Organization name cannot be “Add” or “New”."))
     return value
Ejemplo n.º 20
0
 def slugify_feed_title(cls, feed_title):
     return slugify(feed_title)