fix saving
[prawokultury.git] / migdal / helpers.py
index 4fec03a..8a6513d 100644 (file)
@@ -12,6 +12,7 @@ from django.utils.translation import get_language, string_concat
 
 
 class EntryType(namedtuple('EntryType', 'db slug commentable on_main')):
+    __slots__ = ()
     def __unicode__(self):
         return unicode(self.slug)
 
@@ -19,7 +20,7 @@ class EntryType(namedtuple('EntryType', 'db slug commentable on_main')):
 def field_getter(name):
     @property
     def getter(self):
-        val = getattr(self, "%s_%s" % (name, get_language()))
+        val = getattr(self, "%s_%s" % (name, get_language()), None)
         if not val:
             val = getattr(self, "%s_%s" % (name, settings.LANGUAGE_CODE))
         return val
@@ -70,3 +71,27 @@ def i18n_patterns(prefix, *args):
         return pattern_list
     return pattern_list + [MyLocaleRegexURLResolver(pattern_list)]
 
+
+def add_translatable_index(index_class, fields, languages=None):
+    """Adds some translatable fields to a search index, and a getter."""
+    if languages is None:
+        languages = settings.LANGUAGES
+    for name, field in fields.items():
+        for lang_code, lang_name in languages:
+            new_field = copy(field)
+            fname = "%s_%s" % (name, lang_code)
+            new_field.index_fieldname = new_field.index_fieldname \
+                and "%s_%s" % (new_field.index_fieldname, lang_code) \
+                or fname
+            new_field.model_attr = new_field.model_attr \
+                and "%s_%s" % (new_field.model_attr, lang_code) \
+                or fname
+            setattr(index_class, fname, new_field)
+            index_class.fields[fname] = new_field
+
+
+def translated_fields(field_names, languages=settings.LANGUAGES):
+    return tuple("%s_%s" % (field_name, lang_code)
+                for field_name in field_names
+                for lang_code, lang_name in languages
+                )