Ejemplo n.º 1
0
    def clean(self):
        """
        Slug must be unique per site. Show sensible errors when not. Can only
        check in clean method because sites need to be available in
        cleaned_data.
        """

        # Set slug on a clone and do no further validation
        if '_saveasnew' in self.data:
            cleaned_data = self.cleaned_data
            cleaned_data['slug'] = generate_slug(self.instance,
                                                 cleaned_data['title'])
            return cleaned_data

        slug = self.cleaned_data.get('slug')
        if slug:
            # Check if any combination of slug and site exists.
            for site in self.cleaned_data['sites']:
                q = ModelBase.objects.filter(sites=site, slug=slug)
                if self.instance:
                    q = q.exclude(id=self.instance.id)
                if q.exists():
                    msg = "The slug is already in use by item %s. To use the \
                        same slug the items may not have overlapping \
                        sites." % q[0]
                    self._errors["slug"] = self.error_class([msg])

        return self.cleaned_data
Ejemplo n.º 2
0
    def clean(self):
        """
        Slug must be unique per site. Show sensible errors when not. Can only
        check in clean method because sites need to be available in
        cleaned_data.
        """

        # Set slug on a clone and do no further validation
        if '_saveasnew' in self.data:
            cleaned_data = self.cleaned_data
            cleaned_data['slug'] = generate_slug(self.instance, cleaned_data['title'])
            return cleaned_data

        slug = self.cleaned_data.get('slug')
        if slug:
            # Check if any combination of slug and site exists.
            for site in self.cleaned_data['sites']:
                q = ModelBase.objects.filter(sites=site, slug=slug)
                if self.instance:
                    q = q.exclude(id=self.instance.id)
                if q.exists():
                    msg = "The slug is already in use by item %s. To use the \
                        same slug the items may not have overlapping \
                        sites." % q[0]
                    self._errors["slug"] = self.error_class([msg])

        return self.cleaned_data
Ejemplo n.º 3
0
    def save(self, *args, **kwargs):
        now = timezone.now()

        # set created time to now if not already set.
        if not self.created:
            self.created = now

        # set modified to now on each save.
        set_modified = kwargs.pop('set_modified', True)
        if set_modified:
            self.modified = now

        # set leaf class content type
        if not self.content_type:
            self.content_type = ContentType.objects.get_for_model(\
                    self.__class__)

        # set leaf class class name
        if not self.class_name:
            self.class_name = self.__class__.__name__

        # set title as slug uniquely exactly once
        if not self.slug:
            self.slug = generate_slug(self, self.title)

        super(ModelBase, self).save(*args, **kwargs)
Ejemplo n.º 4
0
    def save(self, *args, **kwargs):
        # set created time to now if not already set.
        if not self.created:
            self.created = datetime.now()

        # set modified to now on each save.
        self.modified = datetime.now()

        # set leaf class content type
        if not self.content_type:
            self.content_type = ContentType.objects.get_for_model(\
                    self.__class__)

        # set leaf class class name
        if not self.class_name:
            self.class_name = self.__class__.__name__

        # set title as slug uniquely
        self.slug = generate_slug(self, self.title)

        super(ModelBase, self).save(*args, **kwargs)
Ejemplo n.º 5
0
    def save(self, *args, **kwargs):
        now = timezone.now()

        # Set created time to now if not already set
        if not self.created:
            self.created = now

        # Set modified to now on each save
        set_modified = kwargs.pop("set_modified", True)
        if set_modified:
            self.modified = now

        # Set leaf class content type
        if not self.content_type:
            self.content_type = ContentType.objects.get_for_model(\
                    self.__class__)

        # Set leaf class class name
        if not self.class_name:
            self.class_name = self.__class__.__name__

        # Set title as slug uniquely exactly once
        if not self.slug:
            self.slug = generate_slug(self, self.title)

        # Raise an error if the slug is not unique per site.
        if self.id:
            for site in self.sites.all():
                q = jmbo.models.ModelBase.objects.filter(
                    slug=self.slug, sites=site).exclude(id=self.id)
                if q.exists():
                    raise IntegrityError(
                        "The slug %s is already in use for site %s by %s" %
                        (self.slug, site.domain, q[0].title))

        super(ModelBase, self).save(*args, **kwargs)
Ejemplo n.º 6
0
    def save(self, *args, **kwargs):
        now = timezone.now()

        # set created time to now if not already set.
        if not self.created:
            self.created = now

        # set modified to now on each save.
        set_modified = kwargs.pop('set_modified', True)
        if set_modified:
            self.modified = now

        # set leaf class content type
        if not self.content_type:
            self.content_type = ContentType.objects.get_for_model(\
                    self.__class__)

        # set leaf class class name
        if not self.class_name:
            self.class_name = self.__class__.__name__

        # set title as slug uniquely exactly once
        if not self.slug:
            self.slug = generate_slug(self, self.title)

        # Raise an error if the slug is not unique per site.
        if self.id:
            for site in self.sites.all():
                q = jmbo.models.ModelBase.objects.filter(
                        slug=self.slug, sites=site).exclude(id=self.id)
                if q.exists():
                    raise RuntimeError(
                        "The slug %s is already in use for site %s by %s" %
                        (self.slug, site.domain, q[0].title))

        super(ModelBase, self).save(*args, **kwargs)