Skip to content

Commit 7d3ed4b

Browse files
committed
Version bump. Adding some logging. Tweaking link title lookup.
1 parent 679009d commit 7d3ed4b

File tree

8 files changed

+62
-35
lines changed

8 files changed

+62
-35
lines changed

articles/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = '2.2.1'
1+
__version__ = '2.3.0'
22

33
from articles.directives import *
44
try:

articles/fixtures/tags.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
"pk": 1,
44
"model": "articles.tag",
55
"fields": {
6-
"name": "Demo"
6+
"name": "Demo",
7+
"slug": "demo"
78
}
89
},
910
{
1011
"pk": 2,
1112
"model": "articles.tag",
1213
"fields": {
13-
"name": "This Is A Test"
14+
"name": "This Is A Test",
15+
"slug": "this-is-a-test"
1416
}
1517
}
1618
]

articles/models.py

+43-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from base64 import encodestring
22
from datetime import datetime
3+
import logging
34
import mimetypes
45
import re
56
import urllib
@@ -37,21 +38,32 @@
3738

3839
# regex used to find links in an article
3940
LINK_RE = re.compile('<a.*?href="(.*?)".*?>(.*?)</a>', re.I|re.M)
40-
TITLE_RE = re.compile('<title>(.*?)</title>', re.I|re.M)
41+
TITLE_RE = re.compile('<title.*?>(.*?)</title>', re.I|re.M)
4142
TAG_RE = re.compile('[^a-z0-9\-_\+\:\.]?', re.I)
4243

44+
log = logging.getLogger(__file__)
45+
4346
def get_name(user):
4447
"""
4548
Provides a way to fall back to a user's username if their full name has not
4649
been entered.
4750
"""
51+
4852
key = 'username_for_%s' % user.id
53+
54+
log.debug('Looking for "%s" in cache (%s)' % (key, user))
4955
name = cache.get(key)
5056
if not name:
57+
log.debug('Name not found')
58+
5159
if len(user.get_full_name().strip()):
60+
log.debug('Using full name')
5261
name = user.get_full_name()
5362
else:
63+
log.debug('Using username')
5464
name = user.username
65+
66+
log.debug('Caching %s as "%s" for a while' % (key, name))
5567
cache.set(key, name, 86400)
5668

5769
return name
@@ -70,11 +82,15 @@ def clean_tag(name):
7082

7183
name = name.replace(' ', '-').encode('ascii', 'ignore')
7284
name = TAG_RE.sub('', name)
73-
return name.lower().strip()
85+
clean = name.lower().strip()
86+
87+
log.debug('Cleaned tag "%s" to "%s"' % (name, clean))
88+
return clean
7489

7590
def save(self, *args, **kwargs):
7691
"""Cleans up any characters I don't want in a URL"""
7792

93+
log.debug('Ensuring that tag "%s" has a slug' % (self,))
7894
self.slug = Tag.clean_tag(self.name)
7995
super(Tag, self).save(*args, **kwargs)
8096

@@ -96,6 +112,7 @@ class Meta:
96112
ordering = ('name',)
97113

98114
class ArticleStatusManager(models.Manager):
115+
99116
def default(self):
100117
default = self.all()[:1]
101118

@@ -122,10 +139,11 @@ def __unicode__(self):
122139
return self.name
123140

124141
class ArticleManager(models.Manager):
142+
125143
def active(self):
126144
"""
127-
Retrieves all active articles which have been published and have not yet
128-
expired.
145+
Retrieves all active articles which have been published and have not
146+
yet expired.
129147
"""
130148
now = datetime.now()
131149
return self.get_query_set().filter(
@@ -185,9 +203,7 @@ class Article(models.Model):
185203
objects = ArticleManager()
186204

187205
def __init__(self, *args, **kwargs):
188-
"""
189-
Make sure that we have some rendered content to use.
190-
"""
206+
"""Makes sure that we have some rendered content to use"""
191207

192208
super(Article, self).__init__(*args, **kwargs)
193209

@@ -208,9 +224,8 @@ def __unicode__(self):
208224
return self.title
209225

210226
def save(self, *args, **kwargs):
211-
"""
212-
Renders the article using the appropriate markup language.
213-
"""
227+
"""Renders the article using the appropriate markup language."""
228+
214229
using = kwargs.get('using', DEFAULT_DB)
215230

216231
self.do_render_markup()
@@ -372,51 +387,48 @@ def _get_article_links(self):
372387
used as the title. Once a title is determined, it is cached for a week
373388
before it will be requested again.
374389
"""
375-
links = {}
376-
keys = []
390+
391+
links = []
377392

378393
# find all links in the article
394+
log.debug('Locating links in article: %s' % (self,))
379395
for link in LINK_RE.finditer(self.rendered_content):
380396
url = link.group(1)
397+
log.debug('Do we have a title for "%s"?' % (url,))
381398
key = 'href_title_' + encodestring(url).strip()
382399

383400
# look in the cache for the link target's title
384-
if not cache.get(key):
401+
title = cache.get(key)
402+
if title is None:
403+
log.debug('Nope... Getting it and caching it.')
385404
title = link.group(2)
386405

387406
if LOOKUP_LINK_TITLE:
388407
try:
408+
log.debug('Looking up title for URL: %s' % (url,))
389409
# open the URL
390410
c = urllib.urlopen(url)
391411
html = c.read()
392412
c.close()
393413

394414
# try to determine the title of the target
395-
title = TITLE_RE.search(html)
396-
if title: title = title.group(1)
415+
title_m = TITLE_RE.search(html)
416+
if title_m:
417+
title = title_m.group(1)
418+
log.debug('Found title: %s' % (title,))
397419
except:
398420
# if anything goes wrong (ie IOError), use the link's text
399-
pass
421+
log.warn('Failed to retrieve the title for "%s"; using link text "%s"' % (url, title))
400422

401423
# cache the page title for a week
424+
log.debug('Using "%s" as title for "%s"' % (title, url))
402425
cache.set(key, title, 604800)
403426

404-
# get the link target's title from cache
405-
val = cache.get(key)
406-
if val:
407-
# add it to the list of links and titles
408-
links[url] = val
409-
410-
# don't duplicate links to the same page
411-
if url not in keys: keys.append(url)
412-
413-
# now go thru and sort the links according to where they appear in the
414-
# article
415-
sorted = []
416-
for key in keys:
417-
sorted.append((key, links[key]))
427+
# add it to the list of links and titles
428+
if url not in (l[0] for l in links):
429+
links.append((url, title))
418430

419-
return tuple(sorted)
431+
return tuple(links)
420432
links = property(_get_article_links)
421433

422434
def _get_word_count(self):

articles/static/css/jquery.autocomplete.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
cursor: default;
2121
display: block;
2222
/*
23-
if width will be 100% horizontal scrollbar will apear
23+
if width will be 100% horizontal scrollbar will appear
2424
when scroll mode will be used
2525
*/
2626
/*width: 100%;*/

articles/templates/404.html

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% extends 'base.html' %}
2+
3+
{% block title %}Page Not Found{% endblock %}
4+
5+
{% block content %}
6+
<h1>Page Not Found</h1>
7+
8+
<p>The page you requested does not exist on this site. Sorry man!</p>
9+
{% endblock %}

articles/tests.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# -*- coding: utf-8 -*-
2+
13
from django.contrib.auth.models import User
24
from django.test import TestCase
35
from django.test.client import Client

sample/articles_demo/demo.db

0 Bytes
Binary file not shown.

sample/articles_demo/settings.py

+2
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@
104104

105105
'articles',
106106
'south',
107+
108+
'django_coverage',
107109
)
108110

109111
# Change this to be your Disqus site's short name

0 commit comments

Comments
 (0)