rename LegacyModel to ModelBase, add translation support

This commit is contained in:
Jeff Balogh 2009-12-10 13:20:59 -08:00
Родитель ad6edf2403
Коммит d0bf20a712
2 изменённых файлов: 36 добавлений и 27 удалений

Просмотреть файл

@ -6,7 +6,7 @@ import functools
from django.core import paginator from django.core import paginator
from .managers import CachingManager from .managers import CachingManager
from .models import LegacyModel, TranslatedField from .models import ModelBase
def paginate(request, queryset, per_page=20): def paginate(request, queryset, per_page=20):

Просмотреть файл

@ -1,42 +1,51 @@
from django.db import models from django.db import models
from django.utils import translation as translation_utils
from . import managers
class LegacyModel(models.Model): class ModelBase(models.Model):
"""Adds automatic created and modified fields to the model.""" """
Base class for AMO models to abstract some common features.
* Adds automatic created and modified fields to the model.
* Fetches all translations in one subsequent query during initialization
"""
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True) modified = models.DateTimeField(auto_now=True)
class Meta: class Meta:
abstract = True abstract = True
def __init__(self, *args, **kw):
super(ModelBase, self).__init__(*args, **kw)
self.set_tranlated_fields()
class TranslatedField(models.IntegerField): def set_tranlated_fields(self):
__metaclass__ = models.SubfieldBase """Fetch and attach all of this object's translations."""
if not hasattr(self._meta, 'translated_fields'):
return
def to_python(self, value): # Map the attribute name to the object name: 'name_id' => 'name'
locale = 'en-US' names = dict((f.attname, f.name) for f in self._meta.translated_fields)
try: # Map the foreign key to the attribute name: self.name_id => 'name_id'
o = Translation.objects.get(id=value, locale=locale) ids = dict((getattr(self, name), name) for name in names)
return o.localized_string
except Translation.DoesNotExist:
return value
Translation = self._meta.translated_fields[0].rel.to
lang = translation_utils.get_language()
q = self._fetch_translations(Translation, ids, lang)
# Putting Translation in here since TranslatedField depends on it. for translation in q:
class Translation(LegacyModel): attr = names[ids[translation.id]]
setattr(self, attr, translation)
autoid = models.AutoField(primary_key=True) def _fetch_translations(self, Translation, ids, lang):
id = models.IntegerField() """
locale = models.CharField(max_length=10) Performs the query for finding Translation objects.
localized_string = models.TextField()
objects = managers.CachingManager() ``Translation`` is the :class:`translations.Translation` class
``ids`` is a list of the foreign keys to the object's translations
``lang`` is the language of the current request
class Meta: Override this to search for translations in an unusual way.
db_table = 'translations' """
return Translation.objects.filter(id__in=ids, locale=lang)
@property
def cache_key(self):
return self._cache_key(id=self.id, locale=self.locale)