예제 #1
0
    def pre_save(self, instance, add):

        # get currently entered slug
        value = self.value_from_object(instance)

        # autopopulate
        if self.always_update or (self.populate_from and not value):
            value = utils.get_prepopulated_value(self, instance)

            if __debug__ and not value:
                print 'Failed to populate slug %s.%s from %s' % \
                    (instance._meta.object_name, self.name, self.populate_from)

        slug = self.slugify(value)

        if not slug:
            # no incoming value,  use model name
            slug = instance._meta.module_name

        assert slug, 'slug is defined before trying to ensure uniqueness'

        slug = utils.crop_slug(self, slug)

        # ensure the slug is unique (if required)
        if self.unique or self.unique_with:
            slug = utils.generate_unique_slug(self, instance, slug)

        assert slug, 'value is filled before saving'

        # make the updated slug available as instance attribute
        setattr(instance, self.name, slug)

        return slug
예제 #2
0
    def pre_save(self, instance, add):

        # get currently entered slug
        value = self.value_from_object(instance)

        manager = self.manager

        # autopopulate
        if self.always_update or (self.populate_from and not value):
            value = utils.get_prepopulated_value(self, instance)

            if __debug__ and not value:
                print 'Failed to populate slug %s.%s from %s' % \
                    (instance._meta.object_name, self.name, self.populate_from)

        slug = self.slugify(value)

        if not slug:
            # no incoming value,  use model name
            slug = instance._meta.module_name

        assert slug, 'slug is defined before trying to ensure uniqueness'

        slug = utils.crop_slug(self, slug)

        # ensure the slug is unique (if required)
        if self.unique or self.unique_with:
            slug = utils.generate_unique_slug(self, instance, slug, manager)

        assert slug, 'value is filled before saving'

        # make the updated slug available as instance attribute
        setattr(instance, self.name, slug)

        return slug
예제 #3
0
    def pre_save(self, instance, add):

        # get currently entered slug
        value = self.value_from_object(instance)

        manager = self.manager

        # autopopulate
        if self.always_update or (self.populate_from and not value):
            value = utils.get_prepopulated_value(self, instance)

            if __debug__ and not value:
                print 'Failed to populate slug %s.%s from %s' % \
                    (instance._meta.object_name, self.name, self.populate_from)

        slug = self.slugify(value)

        if not slug:
            # no incoming value,  use model name
            slug = instance._meta.module_name

        assert slug, 'slug is defined before trying to ensure uniqueness'

        slug = utils.crop_slug(self, slug)

        # ensure the slug is unique (if required)
        if self.unique or self.unique_with:
            slug = utils.generate_unique_slug(self, instance, slug, manager)

        assert slug, 'value is filled before saving'

        # check if redirect tracking is on and if so prepare
        if not add and self.redirect_tracking:

            # Can we import the needed django.contrib.redirects app
            try:
                from django.contrib.redirects.models import Redirect
                from django.contrib.sites.models import Site

            except ImportError:
                raise Exception("You are using redirect_tracking. Please be sure to add django contrib apps Redirect and Site to your INSTALLED_APPS.")

            # does model instance have get_absolute_url defined
            try:
                # capture current get_absolute_url
                pre_update_absolute_url = instance.get_absolute_url()

            except AttributeError:
                raise Exception("You are using redirect_tracking on a field whose model does not have get_absolute_url defined. You must define this method on your model.")

        # make the updated slug available as instance attribute
        setattr(instance, self.name, slug)

        if not add and self.redirect_tracking:
            # check if the slug update caused a change in get_absolute_url
            # if so and redirect_tracking = True, record in the django contrib redirect app.
            post_update_absolute_url = instance.get_absolute_url()

            if pre_update_absolute_url != post_update_absolute_url:
                # current site
                site = Site.objects.get_current()

                # add a redirect
                redirect = Redirect(
                    site=site,
                    old_path=pre_update_absolute_url,
                    new_path=post_update_absolute_url
                )
                redirect.save()

        return slug