Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
def main():
    if not app.config.get('SECRET_KEY'):
        error = ('Error: Please define a `SECRET_KEY` in your config file.\n'
                 'You can use the following one:\n\n    %s\n' % randstr(64))
        sys.stderr.write(error)
        sys.exit(1)

    try:
        port = int(sys.argv[1])
    except:
        port = 9002

    app.run(host='0.0.0.0', port=port, debug=True)
Ejemplo n.º 4
0
 def test_long_string(self):
     nose.ok_(len(randstr(128)) == 128)
Ejemplo n.º 5
0
 def test_short_string(self):
     nose.ok_(len(randstr(1)) == 1)
Ejemplo n.º 6
0
 def test_empty_string(self):
     nose.ok_(randstr(0) == '')