def save()

in kitsune/wiki/models.py [0:0]


    def save(self, *args, **kwargs):
        slug_changed = hasattr(self, "old_slug")
        title_changed = hasattr(self, "old_title")

        self.is_template = (
            self.title.startswith(TEMPLATE_TITLE_PREFIX)
            or self.category == TEMPLATES_CATEGORY
            or (self.parent.category if self.parent else None) == TEMPLATES_CATEGORY
        )
        treat_as_template = self.is_template or (
            self.old_title if title_changed else ""
        ).startswith(TEMPLATE_TITLE_PREFIX)

        self._raise_if_collides("slug", SlugCollision)
        self._raise_if_collides("title", TitleCollision)

        # These are too important to leave to a (possibly omitted) is_valid
        # call:
        self._clean_is_localizable()
        self._ensure_inherited_attr("is_archived")
        # Everything is validated before save() is called, so the only thing
        # that could cause save() to exit prematurely would be an exception,
        # which would cause a rollback, which would negate any category changes
        # we make here, so don't worry:
        self._clean_category()
        self._clean_template_status()

        if slug_changed:
            # Clear out the share link so it gets regenerated.
            self.share_link = ""

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

        # Make redirects if there's an approved revision and title or slug
        # changed. Allowing redirects for unapproved docs would (1) be of
        # limited use and (2) require making Revision.creator nullable.
        #
        # Having redirects for templates doesn't really make sense, and
        # none of the rest of the KB really deals with it, so don't bother.
        if self.current_revision and (slug_changed or title_changed) and not treat_as_template:
            try:
                doc = Document.objects.create(
                    locale=self.locale,
                    title=self._attr_for_redirect("title", REDIRECT_TITLE),
                    slug=self._attr_for_redirect("slug", REDIRECT_SLUG),
                    category=self.category,
                    is_localizable=False,
                )
                Revision.objects.create(
                    document=doc,
                    content=REDIRECT_CONTENT % self.title,
                    is_approved=True,
                    reviewer=self.current_revision.creator,
                    creator=self.current_revision.creator,
                )
            except TitleCollision:
                pass

        if slug_changed:
            del self.old_slug
        if title_changed:
            del self.old_title

        self.parse_and_calculate_links()
        self.clear_cached_html()