Skip to content
This repository has been archived by the owner on Nov 11, 2019. It is now read-only.

increase max_length for title #249

Open
willkg opened this issue Jul 14, 2014 · 9 comments
Open

increase max_length for title #249

willkg opened this issue Jul 14, 2014 · 9 comments

Comments

@willkg
Copy link
Member

willkg commented Jul 14, 2014

I've had a couple of videos before that had long titles and I think we hit another set with SciPy. We should increase the max_length to 100 characters or something.

@codersquid
Copy link
Contributor

For the SciPy videos, I think it is the length of the generated slug, not the title, that is the problem. I'm looking in to it right now.

@codersquid
Copy link
Contributor

Yes, the slug length was the problem. When creating multiple videos with the same title, the unique counter appended to the slug bumps the entire thing over the max_length of 50 chars.

One way to avoid this is to slice out 3 more characters than before, to save room for the slug counter. That's what 4249b8b is doing.

@willkg
Copy link
Member Author

willkg commented Jul 20, 2014

Two things:

  1. we should create a PR so it's easier to comment on the changes
  2. instead of slicing the description and then going through the counter, it's probably better to do both at the same time so we're only slicing off as many characters as we need for the counter

@codersquid
Copy link
Contributor

  1. done
  2. not done yet (but wanted to make a PR to be helpful).

@codersquid
Copy link
Contributor

Hey, I was wondering if you'd rather do something like a query filter where you search for anything where the slug_field starts with the potential slug? If the results are 0, then you know you can use the slug, otherwise, you need to increment the length of the results (unless they are at the sanity check limit you have).

@willkg
Copy link
Member Author

willkg commented Jul 21, 2014

I'm not sure what you mean by that. Can you put that in code?

@codersquid
Copy link
Contributor

Sure, I was playing around with it last night.

def generate_unique_slug(obj, slug_from, slug_field='slug'):
    """ generate slug with a trailing counter to prevent name collision """

    max_length = obj.__class__._meta.get_field(slug_field).max_length
    text = getattr(obj, slug_from)[:max_length-3]
    slug = slugify(text_type(text))

    d = {'{}__startswith'.format(slug_field): slug}
    collisions = len(obj.__class__.objects.filter(**d))

    if collisions == 0:
        return slug

    if collisions > 98:
        # sanity check: prevent new slug if 99 previous slugs already exist.
        raise ValueError('No valid slugs available.')

    return u'{}-{}'.format(text, collisions+1)

@codersquid
Copy link
Contributor

also, when I have error messages sometimes I like to provide information about the invalid input, so in this case I might suggest

raise ValueError('No valid slugs available for: {}.'.format(text))

But the drawback to this kind of error is that it can throw exceptions itself, etc. so sometimes I include that kind of thing in a debug log statement instead.

@codersquid
Copy link
Contributor

let me know if #269 is satisfactory

@willkg willkg removed their assignment Apr 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants