Permalink
Browse files

Made app more robust when objects don't have translations for the cur…

…rent language.
  • Loading branch information...
1 parent 1044ab1 commit 82b0abec8d22297bfad56bd824308b4097f7e741 @mbrochh mbrochh committed Feb 21, 2013
Showing with 20 additions and 23 deletions.
  1. +4 −0 CHANGELOG.txt
  2. +8 −17 cmsplugin_blog_categories/admin.py
  3. +6 −6 cmsplugin_blog_categories/models.py
  4. +2 −0 requirements.txt
View
@@ -1,5 +1,9 @@
=== ongoing ===
+- Added get_translation method to the Category model
+- Make admin more robust when objects don't have translations for the current
+ language.
+
=== 0.3.1 ===
- Added better way to extend the EntryAdmin
@@ -5,7 +5,7 @@
from cmsplugin_blog.admin import EntryAdmin
from simple_translation.admin import TranslationAdmin
-from simple_translation.utils import get_translation_queryset
+from simple_translation.utils import get_preferred_translation_from_lang
from cmsplugin_blog_categories.models import Category, EntryCategory
@@ -19,33 +19,24 @@ class EntryCategoryAdmin(admin.ModelAdmin):
list_display = ['entry_title', 'category_title', ]
def category_title(self, obj):
- lang = get_language()
- try:
- return get_translation_queryset(obj.category).filter(
- language=lang)[0].title
- except IndexError:
- return 'None'
+ """Returns the best available translation for the category title."""
+ return obj.category.get_translation().title
category_title.short_description = _('Category')
def entry_title(self, obj):
+ """Returns the best available translation for the entry title."""
lang = get_language()
- try:
- return get_translation_queryset(obj.entry).filter(
- language=lang)[0].title
- except IndexError:
- return 'None'
+ entry_title = get_preferred_translation_from_lang(obj.entry, lang)
+ return entry_title.title
entry_title.short_description = _('Entry title')
class CategoryAdmin(TranslationAdmin):
list_display = ['title', 'languages', ]
def title(self, obj):
- lang = get_language()
- try:
- return get_translation_queryset(obj).filter(language=lang)[0].title
- except IndexError:
- return 'None'
+ """Returns the best available translation for the catrgory title."""
+ return obj.get_translation().title
title.short_description = _('Title')
@@ -4,7 +4,7 @@
from django.utils.translation import ugettext_lazy as _
from cms.models import CMSPlugin
-from simple_translation.utils import get_translation_queryset
+from simple_translation.utils import get_preferred_translation_from_lang
class Category(models.Model):
@@ -30,12 +30,11 @@ def get_absolute_url(self):
return ('blog_archive_category', (), {'category': self.slug, })
def get_title(self):
+ return self.get_translation().title
+
+ def get_translation(self):
lang = get_language()
- try:
- return get_translation_queryset(self).filter(
- language=lang)[0].title
- except IndexError:
- return 'None'
+ return get_preferred_translation_from_lang(self, lang)
class CategoryTitle(models.Model):
@@ -56,6 +55,7 @@ class EntryCategory(models.Model):
related_name='categories',
verbose_name=_('Entry'),
)
+
category = models.ForeignKey(
Category,
related_name='entry_categories',
View
@@ -24,3 +24,5 @@ coverage
django-coverage
-e git+git://github.com/jcrocholl/pep8.git#egg=pep8
-e git+git://github.com/bmcustodio/flake8.git#egg=flake8
+ipython
+ipdb

0 comments on commit 82b0abe

Please sign in to comment.