示例#1
0
 def generate_slug(cls, title, instance_pk=None):
     original_slug = slugify(title)
     return generate_unique_slug(
         cls._meta.get_field_by_name('slug')[0],
         cls.all_objects.filter(pk=instance_pk).first() or cls(),
         original_slug,
         cls.all_objects
     )
示例#2
0
    def save(self, *args, **kwargs):
        slug_field = self._meta.get_field('slug')

        # we need to manually use autoslug utils here with ModelManager, because automatic slug populating
        # uses our default manager, which can lead to a slug collision between this and a deleted hearing
        self.slug = generate_unique_slug(slug_field, self, self.slug, Hearing.original_manager)

        super().save(*args, **kwargs)
def fill_slug(apps, schema_editor):
    ErrorPost = apps.get_model('error_posts', 'ErrorPost')
    for error_post in ErrorPost.objects.all():
        error_post.slug = generate_unique_slug(
            field=ErrorPost._meta.get_field('slug'),
            instance=error_post,
            slug=error_posts.models._generate_slug(error_post),
            manager=None)
        error_post.save()
示例#4
0
    def save(self, *args, **kwargs):
        slug_field = self._meta.get_field('slug')

        # we need to manually use autoslug utils here with ModelManager, because automatic slug populating
        # uses our default manager, which can lead to a slug collision between this and a deleted hearing
        self.slug = generate_unique_slug(slug_field, self, self.slug,
                                         Hearing.original_manager)

        super().save(*args, **kwargs)
示例#5
0
    def pre_save(self, instance, add):

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

        if self.manager is not None:
            manager = self.manager
        elif self.manager_name is not None:
            manager = getattr(self.model, self.manager_name)
        else:
            manager = None

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

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

        slug = None
        if value:
            slug = self.slugify(value)
        if not slug:
            slug = None

            if not self.blank:
                slug = instance._meta.model_name
            elif not self.null:
                slug = ''

        if not self.blank:
            assert slug, 'slug is defined before trying to ensure uniqueness'

        if slug:
            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)

        # modeltranslation support
        if 'modeltranslation' in settings.INSTALLED_APPS \
                and not hasattr(self.populate_from, '__call__') \
                and autoslug_modeltranslation_enable:
            post_save.connect(modeltranslation_update_slugs,
                              sender=type(instance))

        return slug
示例#6
0
def create_default_slugs_for_presentations(apps, schema_editor):
    Presentation = apps.get_model("symposion_schedule", "Presentation")
    for presentation in Presentation.objects.all():
        presentation.slug = generate_unique_slug(
            Presentation._meta.get_field("slug"),
            presentation,
            slugify(presentation.title),
            None,
        )
        presentation.save()
示例#7
0
def create_default_slugs_for_speakers(apps, schema_editor):
    Speaker = apps.get_model("symposion_speakers", "Speaker")
    for speaker in Speaker.objects.all():
        speaker.slug = generate_unique_slug(
            Speaker._meta.get_field("slug"),
            speaker,
            slugify(speaker.name),
            None,
        )
        speaker.save()
示例#8
0
    def pre_save(self, instance, add):

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

        if self.manager is not None:
            manager = self.manager
        elif self.manager_name is not None:
            manager = getattr(self.model, self.manager_name)
        else:
            manager = None

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

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

            if value:
                slug = self.slugify(value)
            else:
                slug = None

                if not self.blank:
                    slug = instance._meta.model_name
                elif not self.null:
                    slug = ''

            if not self.blank:
                assert slug, 'slug is defined before trying to ensure uniqueness'

            if slug:
                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)

            # modeltranslation support
            if 'modeltranslation' in settings.INSTALLED_APPS \
                    and not hasattr(self.populate_from, '__call__') \
                    and autoslug_modeltranslation_enable:
                post_save.connect(modeltranslation_update_slugs, sender=type(instance))

            return slug
        return value
示例#9
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)

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

        if value:
            slug = self.slugify(value)
        else:
            slug = None

            if not self.blank:
                slug = instance._meta.module_name
            elif not self.null:
                slug = ''

        if not self.blank:
            assert slug, 'slug is defined before trying to ensure uniqueness'

        if slug:
            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
示例#10
0
文件: fields.py 项目: 18dubu/MMS
    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)

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

        if value:
            slug = self.slugify(value)
        else:
            slug = None

            if not self.blank:
                slug = instance._meta.module_name
            elif not self.null:
                slug = ''

        if not self.blank:
            assert slug, 'slug is defined before trying to ensure uniqueness'

        if slug:
            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
示例#11
0
    def pre_save(self, instance, add):
        # get actual value field
        value = self.value_from_object(instance)

        manager = self.manager

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

            # pragma: nocover
            if __debug__ and not values and not self.blank:
                print('Failed to populate slug %s.%s from %s' % \
                    (instance._meta.object_name, self.name, self.populate_from))
        else:
            # force values to be a list
            values = [value]

        # remove possible empty values
        values = [value for value in values if value]

        # if prepopulation return no values
        if not values:
            if self.default and isinstance(self.default, basestring):
                values = [self.default]
                warn (u'Failed to populate slug %s.%s from %s. Set default' % \
                    (instance._meta.object_name, self.name, self.populate_from))
            elif self.blank:
                if self.null:
                    setattr(instance, self.name, None)
                    warn (u'Failed to populate slug %s.%s from %s. Set null' % \
                        (instance._meta.object_name, self.name, self.populate_from))
                    return None
                else:
                    setattr(instance, self.name, u'')
                    warn (u'Failed to populate slug %s.%s from %s. Set blank' % \
                        (instance._meta.object_name, self.name, self.populate_from))
                    return u''
            else:
                values = [instance._meta.module_name]
                warn (u'Failed to populate slug %s.%s from %s. Set model name' % \
                    (instance._meta.object_name, self.name, self.populate_from))

        slugs = [self.slugify(value) for value in values]

        if not slugs:
            warn (u'Failed to populate slug %s.%s from %s' % \
                (instance._meta.object_name, self.name, self.populate_from))
            return

        assert slugs, 'slugs are defined before trying to ensure uniqueness'

        slugs = [utils.crop_slug(self, slug) for slug in slugs]

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

        if not slug:
            warn (u'Failed to populate slug %s.%s from %s' % \
                (instance._meta.object_name, self.name, self.populate_from))
            return

        assert slug, 'value must be filled before saving'

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

        return slug
示例#12
0
    def pre_save(self, instance, add):
        # get actual value field
        value = self.value_from_object(instance)

        manager = self.manager

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

            # pragma: nocover
            if __debug__ and not values and not self.blank:
                print('Failed to populate slug %s.%s from %s' % \
                    (instance._meta.object_name, self.name, self.populate_from))
        else: 
            # force values to be a list
            values = [value]

        # remove possible empty values
        values = [value for value in values if value]

        # if prepopulation return no values
        if not values: 
            if self.default and isinstance(self.default, basestring): 
                values = [self.default]
                warn (u'Failed to populate slug %s.%s from %s. Set default' % \
                    (instance._meta.object_name, self.name, self.populate_from))
            elif self.blank: 
                if self.null: 
                    setattr(instance, self.name, None)
                    warn (u'Failed to populate slug %s.%s from %s. Set null' % \
                        (instance._meta.object_name, self.name, self.populate_from))
                    return None 
                else: 
                    setattr(instance, self.name, u'')
                    warn (u'Failed to populate slug %s.%s from %s. Set blank' % \
                        (instance._meta.object_name, self.name, self.populate_from))
                    return u'' 
            else: 
                values = [instance._meta.module_name]
                warn (u'Failed to populate slug %s.%s from %s. Set model name' % \
                    (instance._meta.object_name, self.name, self.populate_from))

        slugs = [self.slugify(value) for value in values]

        if not slugs: 
            warn (u'Failed to populate slug %s.%s from %s' % \
                (instance._meta.object_name, self.name, self.populate_from))
            return 

        assert slugs, 'slugs are defined before trying to ensure uniqueness'

        slugs = [utils.crop_slug(self, slug) for slug in slugs]

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

        if not slug: 
            warn (u'Failed to populate slug %s.%s from %s' % \
                (instance._meta.object_name, self.name, self.populate_from))
            return 

        assert slug, 'value must be filled before saving'

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

        return slug