Django

Code

Ticket #9956 (closed: fixed)

Opened 6 months ago

Last modified 3 months ago

Problematic Regexp in django.contrib.comments.urls

Reported by: james_stevenson Assigned to: kkubasik
Milestone: 1.1 Component: django.contrib.comments
Version: 1.0 Keywords: comments urls
Cc: kevin@kubasik.net Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

urls.py (1.0 kB) - added by james_stevenson on 01/04/09 17:29:57.
patch to django.contrib.comments.urls to match comment object_pks with "-" characters
comment-urls-fix.diff (463 bytes) - added by devin_s on 02/03/09 11:05:01.
svn diff of the fix
comments_urls_9956.diff (8.4 kB) - added by kkubasik on 04/06/09 14:01:21.

Change History

01/04/09 17:29:57 changed by james_stevenson

  • attachment urls.py added.

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

01/04/09 17:38:30 changed by james_stevenson

  • needs_better_patch changed.
  • has_patch set to 1.
  • needs_tests set to 1.
  • needs_docs changed.

02/03/09 11:05:01 changed by devin_s

  • attachment comment-urls-fix.diff added.

svn diff of the fix

02/03/09 11:05:58 changed by devin_s

I also had this issue and the solution suggested works.

02/27/09 09:13:06 changed by jacob

  • stage changed from Unreviewed to Accepted.
  • milestone set to 1.1.

04/06/09 13:59:26 changed by kkubasik

  • owner changed from nobody to kkubasik.
  • status changed from new to assigned.

Hey, I have a patch with tests.

04/06/09 14:01:21 changed by kkubasik

  • attachment comments_urls_9956.diff added.

04/06/09 14:02:04 changed by kkubasik

  • cc set to kevin@kubasik.net.
  • needs_tests deleted.

04/06/09 14:06:16 changed by Alex

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

04/06/09 14:14:24 changed by kkubasik

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

Alex: do you need a new patch?

04/07/09 14:42:08 changed by jacob

  • status changed from assigned to closed.
  • resolution set to fixed.

Fixed in [10422].


Add/Change #9956 (Problematic Regexp in django.contrib.comments.urls)




Change Properties
Action