def forwards(apps, schema_editor, **kwargs): """ Add slugs to existing Units and Resources. """ force = kwargs.get('force', False) Resource = apps.get_model('resources', 'Resource') resources = Resource.objects.all() if force else Resource.objects.filter(slug='') for resource in resources: resource.slug = get_prepopulated_value(resource._meta.get_field('slug'), resource) resource.save() Unit = apps.get_model('resources', 'Unit') units = Unit.objects.all() if force else Unit.objects.filter(slug='') for unit in units: unit.slug = get_prepopulated_value(unit._meta.get_field('slug'), unit) unit.save()
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
def pre_save(self, model_instance, add): value = self.value_from_object(model_instance) new_value = get_prepopulated_value(self, model_instance) if self.populate_from and value != new_value: current_value = utils.get_prepopulated_value(self, model_instance) else: current_value = value setattr(model_instance, self.name, current_value) return current_value
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
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
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