Opened 14 years ago
Last modified 14 years ago
#36 closed defect (FIXED)
Need a way to generate slugs for media entries, etc
Reported by: | Christopher Allan Webber | Owned by: | Aaron Williamson |
---|---|---|---|
Priority: | minor | Milestone: | 0.0.2 |
Component: | Keywords: | ||
Cc: | Parent Tickets: |
Description
We need to be able to generate URL slugs for media entries. I'd really like to use `http://flask.pocoo.org/snippets/5/ <http://flask.pocoo.org/snippets/5/>`_ but have to clear it with the FSF that that public domain assertion is okay for our codebase and to merge. Regardless, we need to generate slugs for each entry anyway. Probably during the submission phase after setting the title there should be an entry.generate\_slug() method which sets the slug on the MediaEntry instance. This way we have a nice way of accessing for example the fictional address: [http://mediagoblin.example.org/\ :sub:`cwebber/w/a-walk-in-the-park/](http://mediagoblin.example.org/`\ cwebber/w/a-walk-in-the-park/) that last bit of the URL being the entry slug.
Attachments (5)
Change History (16)
by , 14 years ago
Attachment: | 0001-Patch-for-Feature-306.patch added |
---|
0001-Patch-for-Feature-306.patch
comment:2 by , 14 years ago
Made an effort at plucking this low-hanging fruit as described above. One concern though: if a user names several images "A walk in the park" (which seems quite possible) they will all have identical slugs. Should we account for this with some sort of iterator or other device?
comment:3 by , 14 years ago
Owner: | set to |
---|
Great, Aaron! I think the best thing to do if such a conflict occurs is maybe to either not generate a slug (hmmm) or tack a uuid onto the name (hmmmmm)?
comment:4 by , 14 years ago
One way I've solved this in the past is to query the database for items with identical slugs submitted by the same user. If there is one, add "-1" (or "-2", etc.) to the end of the slug. However, this assumes you only want slugs to be unique to users. If you want to use slugs in other contexts (e.g. sets of photos) this might be an inadequate solution. It also is probably slower than using a UUID, because it requires a database hit. (Hmmmmm?)
comment:5 by , 14 years ago
Yeah I think numbered slugs are just simply impossible with mongodb. Aaron, what do you think of tacking on the entry id of the MediaEntry if there's a conflict? Conflicts will probably be rare and it probably won't look **too** bad. Prepending it like :math:`${id}-$`{slug} probably works. Or the reverse. I think I like the former though. Thoughts? More useful than a uuid.
comment:6 by , 14 years ago
Yeah, that's a good idea. Or possibly even :math:`${id}/$`{slug}? Or would that break url patterns?
comment:7 by , 14 years ago
It might break the urlpatterns... I'd much rather use a dash. Here's some pedantic styling comments, thought this looks like a great start: :: 'slug':unicode, do: :: 'slug': unicode, def generate_slug(self): self['slug'] = util.slugify(self['title']) This looks good. Could you add a docstring? It's probably a good idea that you didn't .save() here, good call. You might want to add a second argument to util.slugify which is self['\_id']. Also, your unit test could really use some examples of non-ascii characters, for example on the snippet linked above there is the "u" to "ue". Aside from this and the conflict resolution we've been discussing, this patch looks great, like everything I hoped! Make that id addition change and we can merge this.
comment:8 by , 14 years ago
Milestone: | → 0.0.2 |
---|
comment:8 by , 14 years ago
Addressed all of Chris's concerns. Decided to do the dupe-checking in generate\_slug instead of slugify. Added a couple of tests for unicode characters.
comment:9 by , 14 years ago
Could you use git format-patch? If you do that it'll keep information about this commit being done by you and etc. Looks good though.
by , 14 years ago
Attachment: | 0003-Added-one-more-unicode-test-for-slugify.patch added |
---|
0003-Added-one-more-unicode-test-for-slugify.patch
comment:10 by , 14 years ago
Sure, sorry -- I'm not all that familiar with format-patch and I was trying to get all of this to you in a single patch file, but I guess that's unnecessary. Here are all of the applicable patches.
comment:12 by , 13 years ago
The original url for this bug was http://bugs.foocorp.net/issues/306 .
Note:
See TracTickets
for help on using tickets.