Opened 15 years ago

Closed 15 years ago

Last modified 13 years ago

#9956 closed (fixed)

Problematic Regexp in django.contrib.comments.urls

Reported by: james_stevenson Owned by: Kevin Kubasik
Component: contrib.comments Version: 1.0
Severity: Keywords: comments urls
Cc: kevin@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I'm using Django for my blog: http://www.mazelife.com/blog. My model for a blog post uses a slug as a primary key, and that field only allows letters, numbers and dashes to be used (in regex terms, [a-zA-Z0-9-]+)
When using the comments app, this causes and error when I try to call get_content_object_url on a comment object attached to a blog post.

Let's assume a hypothetical comment object, with the following properties:

comment.object_pk = "sample-blog-post-slug"

comment.content_type_id = 11

Step 1: comment.get_content_object_url() is called in my application
Step 2: in django.contrib.comments.models, this is in the get_content_object_url method, ln. 36 - 39:

        return urlresolvers.reverse(
            "comments-url-redirect",
            args=(self.content_type_id, self.object_pk)
        )

So, in this case, this code is executed:

	urlresolvers.reverse("comments-url-redirect", args=(11, "sample-blog-post-slug"))


Step 3. urlresolvers.reverse attempts to match this candidate:

"comments/cr/11/sample-blog-post-slug/"

against this pattern, from django.contrib.comments.urls (ln. 17):

	r'^cr/(\d+)/(\w+)/$'


The problem here is that \w sequence does not match "-" in the blog post slug, so urlresolvers.reverse raises a NoReverseMatch exception.
I think that, for a primary key that will be used in a url, "-" is a vaild character and is probably even more prevalent than "_", which is matched by \w.
My proposed solution would be to modify django.contrib.comments.urls to match "-" characters in an object_pk. I've attached a patch to django.contrib.comments.urls that does this.

Attachments (3)

urls.py (1.0 KB ) - added by james_stevenson 15 years ago.
patch to django.contrib.comments.urls to match comment object_pks with "-" characters
comment-urls-fix.diff (463 bytes ) - added by Devin S 15 years ago.
svn diff of the fix
comments_urls_9956.diff (8.4 KB ) - added by Kevin Kubasik 15 years ago.

Download all attachments as: .zip

Change History (12)

by james_stevenson, 15 years ago

Attachment: urls.py added

patch to django.contrib.comments.urls to match comment object_pks with "-" characters

comment:1 by james_stevenson, 15 years ago

Has patch: set
Needs tests: set

by Devin S, 15 years ago

Attachment: comment-urls-fix.diff added

svn diff of the fix

comment:2 by Devin S, 15 years ago

I also had this issue and the solution suggested works.

comment:3 by Jacob, 15 years ago

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:4 by Kevin Kubasik, 15 years ago

Owner: changed from nobody to Kevin Kubasik
Status: newassigned

Hey, I have a patch with tests.

by Kevin Kubasik, 15 years ago

Attachment: comments_urls_9956.diff added

comment:5 by Kevin Kubasik, 15 years ago

Cc: kevin@… added
Needs tests: unset

comment:6 by Alex Gaynor, 15 years ago

Unless I've missed something shouldn't [\w-]+ work, which is easier to read(and slightly faster probably).

comment:7 by Kevin Kubasik, 15 years ago

Hmmm, I'm testing now. Looks like it works to me.

Alex: do you need a new patch?

comment:8 by Jacob, 15 years ago

Resolution: fixed
Status: assignedclosed

Fixed in [10422].

comment:9 by Jacob, 13 years ago

milestone: 1.1

Milestone 1.1 deleted

Note: See TracTickets for help on using tickets.
Back to Top