Skip to content

Commit

Permalink
Don't crash in display_url when object has no get_absolute_url
Browse files Browse the repository at this point in the history
  • Loading branch information
claudep committed May 29, 2015
1 parent 42f33e8 commit 850f725
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion linkcheck/models.py
Expand Up @@ -321,7 +321,7 @@ class Link(models.Model):
def display_url(self):
# when page /test/ has a anchor link to /test/#anchor, we display it
# as "#anchor" rather than "/test/#anchor"
if self.url.url.count('#'):
if self.url.url.count('#') and hasattr(self.content_object, 'get_absolute_url'):
url_part, anchor_part = self.url.url.split('#')
absolute_url = self.content_object.get_absolute_url()
if url_part == absolute_url:
Expand Down
18 changes: 15 additions & 3 deletions linkcheck/tests/sampleapp/linklists.py
@@ -1,10 +1,22 @@
from linkcheck import Linklist
from linkcheck.tests.sampleapp.models import Book
from linkcheck.tests.sampleapp.models import Author, Book

class SampleLinklist(Linklist):

class BookLinklist(Linklist):
""" Class to let linkcheck app discover fields containing links """
model = Book
object_filter = {}
html_fields = ['description']

linklists = {'Books': SampleLinklist}

class AuthorLinklist(Linklist):
""" Class to let linkcheck app discover fields containing links """
model = Author
object_filter = {}
url_fields = ['website']


linklists = {
'Books': BookLinklist,
'Authors': AuthorLinklist,
}
7 changes: 7 additions & 0 deletions linkcheck/tests/sampleapp/models.py
@@ -1,8 +1,15 @@
from django.db import models


class Book(models.Model):
title = models.CharField(max_length=50)
description = models.TextField()

def get_absolute_url(self):
return "/book/%s/" % self.id


class Author(models.Model):
# This model has purposefully no get_absolute_url
name = models.CharField(max_length=50)
website = models.URLField(blank=True)
24 changes: 18 additions & 6 deletions linkcheck/tests/test_linkcheck.py
Expand Up @@ -3,6 +3,15 @@
import re
import os

from django.conf import settings
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import TestCase
from linkcheck.models import Link, Url

from .sampleapp.models import Author, Book


#MOCK addinfurl
class addinfoUrl():
"""class to add info() and getUrl(url=) methods to an open file."""
Expand Down Expand Up @@ -47,10 +56,6 @@ def mock_urlopen(url, data=None, timeout=timeout):

raise urllib2.HTTPError(url, code, msg, None, None)

from django.conf import settings
from django.core.urlresolvers import reverse
from django.test import TestCase
from linkcheck.models import Url

class InternalCheckTestCase(TestCase):
urls = 'linkcheck.tests.test_urls'
Expand Down Expand Up @@ -160,17 +165,24 @@ def test_external_check_404(self):

class FindingLinksTestCase(TestCase):
def test_found_links(self):
from linkcheck.tests.sampleapp.models import Book
self.assertEqual(Url.objects.all().count(), 0)
Book.objects.create(title='My Title', description="""Here's a link: <a href="http://www.example.org">Example</a>""")
self.assertEqual(Url.objects.all().count(), 1)
self.assertEqual(Url.objects.all()[0].url, "http://www.example.org")

class ReportViewTestCase(TestCase):
def setUp(self):
from django.contrib.auth.models import User
User.objects.create_superuser('admin', 'admin@example.org', 'password')

def test_display_url(self):
Book.objects.create(title='My Title', description="""Here's a link: <a href="http://www.example.org">Example</a>""")
Author.objects.create(name="John Smith", website="http://www.example.org#john")
self.assertEqual(Link.objects.count(), 2)
self.assertEqual(
set([l.display_url for l in Link.objects.all()]),
{'http://www.example.org', 'http://www.example.org#john'}
)

def test_report_view(self):
self.client.login(username='admin', password='password')
response = self.client.get(reverse('linkcheck.views.report'))
Expand Down

0 comments on commit 850f725

Please sign in to comment.