Commits

Josh VanderLinden committed 928a2b3

Version bump. Adding some logging. Tweaking link title lookup.

Comments (0)

Files changed (8)

articles/__init__.py

-__version__ = '2.2.1'
+__version__ = '2.3.0'
 
 from articles.directives import *
 try:

articles/fixtures/tags.json

         "pk": 1,
         "model": "articles.tag",
         "fields": {
-            "name": "Demo"
+            "name": "Demo",
+            "slug": "demo"
         }
     },
     {
         "pk": 2,
         "model": "articles.tag",
         "fields": {
-            "name": "This Is A Test"
+            "name": "This Is A Test",
+            "slug": "this-is-a-test"
         }
     }
 ]

articles/models.py

 from base64 import encodestring
 from datetime import datetime
+import logging
 import mimetypes
 import re
 import urllib
 
 # regex used to find links in an article
 LINK_RE = re.compile('<a.*?href="(.*?)".*?>(.*?)</a>', re.I|re.M)
-TITLE_RE = re.compile('<title>(.*?)</title>', re.I|re.M)
+TITLE_RE = re.compile('<title.*?>(.*?)</title>', re.I|re.M)
 TAG_RE = re.compile('[^a-z0-9\-_\+\:\.]?', re.I)
 
+log = logging.getLogger(__file__)
+
 def get_name(user):
     """
     Provides a way to fall back to a user's username if their full name has not
     been entered.
     """
+
     key = 'username_for_%s' % user.id
+
+    log.debug('Looking for "%s" in cache (%s)' % (key, user))
     name = cache.get(key)
     if not name:
+        log.debug('Name not found')
+
         if len(user.get_full_name().strip()):
+            log.debug('Using full name')
             name = user.get_full_name()
         else:
+            log.debug('Using username')
             name = user.username
+
+        log.debug('Caching %s as "%s" for a while' % (key, name))
         cache.set(key, name, 86400)
 
     return name
 
         name = name.replace(' ', '-').encode('ascii', 'ignore')
         name = TAG_RE.sub('', name)
-        return name.lower().strip()
+        clean = name.lower().strip()
+
+        log.debug('Cleaned tag "%s" to "%s"' % (name, clean))
+        return clean
 
     def save(self, *args, **kwargs):
         """Cleans up any characters I don't want in a URL"""
 
+        log.debug('Ensuring that tag "%s" has a slug' % (self,))
         self.slug = Tag.clean_tag(self.name)
         super(Tag, self).save(*args, **kwargs)
 
         ordering = ('name',)
 
 class ArticleStatusManager(models.Manager):
+
     def default(self):
         default = self.all()[:1]
 
             return self.name
 
 class ArticleManager(models.Manager):
+
     def active(self):
         """
-        Retrieves all active articles which have been published and have not yet
-        expired.
+        Retrieves all active articles which have been published and have not
+        yet expired.
         """
         now = datetime.now()
         return self.get_query_set().filter(
     objects = ArticleManager()
 
     def __init__(self, *args, **kwargs):
-        """
-        Make sure that we have some rendered content to use.
-        """
+        """Makes sure that we have some rendered content to use"""
 
         super(Article, self).__init__(*args, **kwargs)
 
         return self.title
 
     def save(self, *args, **kwargs):
-        """
-        Renders the article using the appropriate markup language.
-        """
+        """Renders the article using the appropriate markup language."""
+
         using = kwargs.get('using', DEFAULT_DB)
 
         self.do_render_markup()
         used as the title.  Once a title is determined, it is cached for a week
         before it will be requested again.
         """
-        links = {}
-        keys = []
+
+        links = []
 
         # find all links in the article
+        log.debug('Locating links in article: %s' % (self,))
         for link in LINK_RE.finditer(self.rendered_content):
             url = link.group(1)
+            log.debug('Do we have a title for "%s"?' % (url,))
             key = 'href_title_' + encodestring(url).strip()
 
             # look in the cache for the link target's title
-            if not cache.get(key):
+            title = cache.get(key)
+            if title is None:
+                log.debug('Nope... Getting it and caching it.')
                 title = link.group(2)
 
                 if LOOKUP_LINK_TITLE:
                     try:
+                        log.debug('Looking up title for URL: %s' % (url,))
                         # open the URL
                         c = urllib.urlopen(url)
                         html = c.read()
                         c.close()
 
                         # try to determine the title of the target
-                        title = TITLE_RE.search(html)
-                        if title: title = title.group(1)
+                        title_m = TITLE_RE.search(html)
+                        if title_m:
+                            title = title_m.group(1)
+                            log.debug('Found title: %s' % (title,))
                     except:
                         # if anything goes wrong (ie IOError), use the link's text
-                        pass
+                        log.warn('Failed to retrieve the title for "%s"; using link text "%s"' % (url, title))
 
                 # cache the page title for a week
+                log.debug('Using "%s" as title for "%s"' % (title, url))
                 cache.set(key, title, 604800)
 
-            # get the link target's title from cache
-            val = cache.get(key)
-            if val:
-                # add it to the list of links and titles
-                links[url] = val
+            # add it to the list of links and titles
+            if url not in (l[0] for l in links):
+                links.append((url, title))
 
-                # don't duplicate links to the same page
-                if url not in keys: keys.append(url)
-
-        # now go thru and sort the links according to where they appear in the
-        # article
-        sorted = []
-        for key in keys:
-            sorted.append((key, links[key]))
-
-        return tuple(sorted)
+        return tuple(links)
     links = property(_get_article_links)
 
     def _get_word_count(self):

articles/static/css/jquery.autocomplete.css

 	cursor: default;
 	display: block;
 	/*
-	if width will be 100% horizontal scrollbar will apear
+	if width will be 100% horizontal scrollbar will appear
 	when scroll mode will be used
 	*/
 	/*width: 100%;*/

articles/templates/404.html

+{% extends 'base.html' %}
+
+{% block title %}Page Not Found{% endblock %}
+
+{% block content %}
+<h1>Page Not Found</h1>
+
+<p>The page you requested does not exist on this site.  Sorry man!</p>
+{% endblock %}

articles/tests.py

+# -*- coding: utf-8 -*-
+
 from django.contrib.auth.models import User
 from django.test import TestCase
 from django.test.client import Client
Add a comment to this file

sample/articles_demo/demo.db

Binary file modified.

sample/articles_demo/settings.py

 
     'articles',
     'south',
+
+    'django_coverage',
 )
 
 # Change this to be your Disqus site's short name
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.