Commits

Josh VanderLinden committed 18c55f6

#10 - Unicode in tags. Version bump.

Comments (0)

Files changed (8)

articles/__init__.py

-__version__ = '2.1.4'
+__version__ = '2.2.0'
 
 from articles.directives import *
 try:

articles/fixtures/tags.json

+[
+    {
+        "pk": 1,
+        "model": "articles.tag",
+        "fields": {
+            "name": "Demo"
+        }
+    },
+    {
+        "pk": 2,
+        "model": "articles.tag",
+        "fields": {
+            "name": "This Is A Test"
+        }
+    }
+]
+
+

articles/forms.py

 from django.utils.translation import ugettext_lazy as _
 from models import Article, Tag
 
+def tag(name):
+    """Returns a Tag object for the given name"""
+
+    t = Tag.objects.get_or_create(slug=Tag.clean_tag(name))[0]
+    if not t.name:
+        t.name = name
+        t.save()
+
+    return t
+
 class ArticleAdminForm(forms.ModelForm):
     tags = forms.CharField(initial='', required=False,
                            widget=forms.TextInput(attrs={'size': 100}),
     def clean_tags(self):
         """Turns the string of tags into a list"""
 
-        tag = lambda n: Tag.objects.get_or_create(name=Tag.clean_tag(n))[0]
         tags = [tag(t) for t in self.cleaned_data['tags'].split()]
         self.cleaned_data['tags'] = tags
         return self.cleaned_data['tags']

articles/migrations/0003_auto__add_field_tag_slug.py

+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+class Migration(SchemaMigration):
+
+    def forwards(self, orm):
+
+        # Adding field 'Tag.slug'
+        db.add_column('articles_tag', 'slug', self.gf('django.db.models.fields.CharField')(default='', unique=True, max_length=64, null=True, blank=True), keep_default=False)
+
+
+    def backwards(self, orm):
+
+        # Deleting field 'Tag.slug'
+        db.delete_column('articles_tag', 'slug')
+
+
+    models = {
+        'articles.article': {
+            'Meta': {'ordering': "('-publish_date', 'title')", 'object_name': 'Article'},
+            'addthis_use_author': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'addthis_username': ('django.db.models.fields.CharField', [], {'default': 'None', 'max_length': '50', 'blank': 'True'}),
+            'author': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"}),
+            'auto_tag': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'content': ('django.db.models.fields.TextField', [], {}),
+            'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+            'expiration_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
+            'followup_for': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "'followups'", 'blank': 'True', 'to': "orm['articles.Article']"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'keywords': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+            'login_required': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'markup': ('django.db.models.fields.CharField', [], {'default': "'h'", 'max_length': '1'}),
+            'publish_date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'related_articles': ('django.db.models.fields.related.ManyToManyField', [], {'related_name': "'related_articles_rel_+'", 'blank': 'True', 'to': "orm['articles.Article']"}),
+            'rendered_content': ('django.db.models.fields.TextField', [], {}),
+            'sites': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['sites.Site']", 'symmetrical': 'False', 'blank': 'True'}),
+            'slug': ('django.db.models.fields.SlugField', [], {'max_length': '50', 'db_index': 'True'}),
+            'status': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['articles.ArticleStatus']"}),
+            'tags': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['articles.Tag']", 'symmetrical': 'False', 'blank': 'True'}),
+            'title': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'use_addthis_button': ('django.db.models.fields.BooleanField', [], {'default': 'True'})
+        },
+        'articles.articlestatus': {
+            'Meta': {'ordering': "('ordering', 'name')", 'object_name': 'ArticleStatus'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'is_live': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}),
+            'ordering': ('django.db.models.fields.IntegerField', [], {'default': '0'})
+        },
+        'articles.attachment': {
+            'Meta': {'ordering': "('-article', 'id')", 'object_name': 'Attachment'},
+            'article': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'attachments'", 'to': "orm['articles.Article']"}),
+            'attachment': ('django.db.models.fields.files.FileField', [], {'max_length': '100'}),
+            'caption': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
+        },
+        'articles.tag': {
+            'Meta': {'ordering': "('name',)", 'object_name': 'Tag'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '64'}),
+            'slug': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '64'})
+        },
+        'auth.group': {
+            'Meta': {'object_name': 'Group'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
+            'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
+        },
+        'auth.permission': {
+            'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
+            'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+        },
+        'auth.user': {
+            'Meta': {'object_name': 'User'},
+            'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
+            'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+            'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+            'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+            'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
+            'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
+        },
+        'contenttypes.contenttype': {
+            'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+            'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+        },
+        'sites.site': {
+            'Meta': {'ordering': "('domain',)", 'object_name': 'Site', 'db_table': "'django_site'"},
+            'domain': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+        }
+    }
+
+    complete_apps = ['articles']

articles/models.py

 
 class Tag(models.Model):
     name = models.CharField(max_length=64, unique=True)
+    slug = models.CharField(max_length=64, unique=True, null=True, blank=True)
 
     def __unicode__(self):
         return self.name
     def clean_tag(name):
         """Replace spaces with dashes, in case someone adds such a tag manually"""
 
-        name = name.replace(' ', '-')
+        name = name.replace(' ', '-').encode('ascii', 'ignore')
         name = TAG_RE.sub('', name)
         return name.lower().strip()
 
     def save(self, *args, **kwargs):
         """Cleans up any characters I don't want in a URL"""
 
-        self.name = Tag.clean_tag(self.name)
+        self.slug = Tag.clean_tag(self.name)
         super(Tag, self).save(*args, **kwargs)
 
     @models.permalink
     def get_absolute_url(self):
-        return ('articles_display_tag', (self.name,))
+        return ('articles_display_tag', (self.clean,))
+
+    @property
+    def clean(self):
+        """Returns the clean version of the tag"""
+
+        return self.slug or Tag.clean_tag(self.name)
 
     @property
     def rss_name(self):
-        return u'tags/%s' % self.name
+        return u'tags/%s' % self.clean
 
     class Meta:
         ordering = ('name',)

articles/templatetags/article_tags.py

 
     return GetPageURLNode(args[1], varname)
 
-class TagCloudNode(template.Node):
-    def __init__(self, varname):
-        self.varname = varname
-
-    def render(self, context):
-        context[self.varname] = tags
-
 def tag_cloud():
     """Provides the tags with a "weight" attribute to build a tag cloud"""
 

articles/tests.py

 from django.contrib.auth.models import User
 from django.test import TestCase
+from django.test.client import Client
 
 from models import Article, ArticleStatus, Tag
 
+class TagTestCase(TestCase):
+    fixtures = ['tags']
+
+    def setUp(self):
+        self.client = Client()
+
+    def test_unicode_tag(self):
+        """Unicode characters in tags (issue #10)"""
+
+        name = u'Căutare avansată'
+        t = Tag.objects.create(name=name)
+        self.assertEqual(t.slug, 'cutare-avansat')
+
+        response = self.client.get(t.get_absolute_url())
+        self.assertEqual(response.status_code, 200)
+
+        # make sure older tags still work
+        t2 = Tag.objects.get(pk=2)
+        response = self.client.get(t2.get_absolute_url())
+        self.assertEqual(response.status_code, 200)
+
 class ArticleTestCase(TestCase):
     fixtures = ['users']
 

articles/views.py

 
     context = {}
     if tag:
-        tag = get_object_or_404(Tag, name__iexact=tag)
+        try:
+            tag = get_object_or_404(Tag, slug__iexact=tag)
+        except Http404:
+            # for backwards-compatibility
+            tag = get_object_or_404(Tag, name__iexact=tag)
+
         articles = tag.article_set.live(user=request.user).select_related()
         template = 'articles/display_tag.html'
         context['tag'] = tag