Commits

Josh VanderLinden committed c88d5fc

#3 - I've added some code to the save method of the Article model that should
ensure that the slug is always unique for the year in which an article is
posted. I've tested it a little bit, and it seems to be working fine for me.

I moved the unique slug code out of the new check_for_articles_from_email
command and fixed a log message that I failed to edit in my last commit.

Comments (0)

Files changed (3)

articles/__init__.py

-__version__ = '1.9.3-pre1'
+__version__ = '1.9.4-pre1'
 
 """
 The Pygments reStructuredText directive

articles/management/commands/check_for_articles_from_email.py

 from django.contrib.auth.models import User
 from django.contrib.sites.models import Site
 from django.core.management.base import BaseCommand
-from django.template.defaultfilters import slugify
 from django.utils.translation import ugettext_lazy as _
 
 from articles.models import Article, MARKUP_HTML, MARKUP_MARKDOWN, MARKUP_REST, MARKUP_TEXTILE
             messages = handle.fetch()
 
             if len(messages):
-                self.log('Fetching messages')
+                self.log('Creating articles')
                 created = self.create_articles(messages)
 
                 if len(created):
 
         return None
 
-    def get_unique_slug(self, slug):
-        """Iterates until a unique slug is found"""
-
-        orig_slug = slug
-        year = datetime.now().year
-        counter = 1
-
-        while True:
-            not_unique = Article.objects.filter(publish_date__year=year, slug=slug)
-            if len(not_unique) == 0:
-                return slug
-
-            self.log('Found duplicate slug for year %s: %s. Trying again.' % (year, slug))
-            slug = '%s-%s' % (orig_slug, counter)
-            counter += 1
-
     def create_articles(self, emails):
         """Attempts to post new articles based on parsed email messages"""
 
-        self.log('Creating article objects')
         created = []
         site = Site.objects.get_current()
 
 
             # get the attributes for the article
             title = email.get('Subject', '--- article from email ---')
-            slug = self.get_unique_slug(slugify(title))
 
             content = self.get_email_content(email)
             try:
             article = Article(
                 author=author,
                 title=title,
-                slug=slug,
                 content=content,
                 markup=markup,
                 publish_date=publish_date,
 
             try:
                 article.save()
+                self.log('Article created.')
             except StandardError, err:
                 # log it and move on to the next message
                 self.log('Error creating article: %s' % (err,), 0)

articles/models.py

 from django.contrib.sites.models import Site
 from django.core.cache import cache
 from django.conf import settings
-from django.template.defaultfilters import striptags
+from django.template.defaultfilters import slugify, striptags
 from django.utils.translation import ugettext_lazy as _
 from datetime import datetime
 from base64 import encodestring
         if self.use_addthis_button and self.addthis_use_author and not self.addthis_username:
             self.addthis_username = self.author.username
 
+        # make sure the slug is always unique for the year this article was posted
+        if not self.id:
+            # make sure we have a slug first
+            if not len(self.slug.strip()):
+                self.slug = slugify(self.title)
+
+            self.slug = self.get_unique_slug(self.slug)
+
         super(Article, self).save(*args, **kwargs)
         requires_save = False
 
         if requires_save:
             super(Article, self).save(*args, **kwargs)
 
+    def get_unique_slug(self, slug):
+        """Iterates until a unique slug is found"""
+
+        # we need a publish date before we can do anything meaningful
+        if type(self.publish_date) is not datetime:
+            return slug
+
+        orig_slug = slug
+        year = self.publish_date.year
+        counter = 1
+
+        while True:
+            not_unique = Article.objects.filter(publish_date__year=year, slug=slug)
+            if len(not_unique) == 0:
+                return slug
+
+            slug = '%s-%s' % (orig_slug, counter)
+            counter += 1
+
     def _get_article_links(self):
         """
         Find all links in this article.  When a link is encountered in the
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.