From 850f72513367d909f220e313ca0b4cea13a76d7a Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 29 May 2015 18:43:03 +0200 Subject: [PATCH] Don't crash in display_url when object has no get_absolute_url --- linkcheck/models.py | 2 +- linkcheck/tests/sampleapp/linklists.py | 18 +++++++++++++++--- linkcheck/tests/sampleapp/models.py | 7 +++++++ linkcheck/tests/test_linkcheck.py | 24 ++++++++++++++++++------ 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/linkcheck/models.py b/linkcheck/models.py index e44858c..e64ca3b 100644 --- a/linkcheck/models.py +++ b/linkcheck/models.py @@ -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: diff --git a/linkcheck/tests/sampleapp/linklists.py b/linkcheck/tests/sampleapp/linklists.py index 592f31b..346bb65 100644 --- a/linkcheck/tests/sampleapp/linklists.py +++ b/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, +} diff --git a/linkcheck/tests/sampleapp/models.py b/linkcheck/tests/sampleapp/models.py index 1995937..c40034f 100644 --- a/linkcheck/tests/sampleapp/models.py +++ b/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) diff --git a/linkcheck/tests/test_linkcheck.py b/linkcheck/tests/test_linkcheck.py index ca75f16..5798055 100644 --- a/linkcheck/tests/test_linkcheck.py +++ b/linkcheck/tests/test_linkcheck.py @@ -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.""" @@ -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' @@ -160,7 +165,6 @@ 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: Example""") self.assertEqual(Url.objects.all().count(), 1) @@ -168,9 +172,17 @@ def test_found_links(self): 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: Example""") + 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'))