def slugify(model, field_name): """ Given the instance of a model and a field to slugify, generates a unique slug. If a standard one exists in the DB, or the generated slug is numeric-only, a hyphen and the object's ID is appended to it to generate a unique and alphanumeric one. """ slug = do_slug(getattr(model, field_name, '')) if not slug: slug = randstr(6) try: is_int = isinstance(int(slug), int) except ValueError: is_int = False exists = bool(model.__class__.query.filter_by(slug=slug).count()) if is_int or exists: extra = model.pk if not extra: extra = randstr(6) slug += u'-%s' % extra return slug
def slugify(text, instance): """ Given the instance of a model and a field to slugify, generates a unique slug. If a standard one exists in the DB, or the generated slug is numeric-only, a hyphen and the object's ID is appended to it to generate a unique and alphanumeric one. """ slug = do_slug(text) if not slug: slug = randstr(6) try: is_int = isinstance(int(slug), int) except ValueError: is_int = False exists = bool(instance.__class__.query.filter_by(slug=slug).count()) if is_int or exists: # FIXME: In specific cases is finding itself as a duplicate. # Investigate. extra = instance.pk or randstr(6) slug += u'-%s' % extra return slug