X-Git-Url: https://git.mdrn.pl/prawokultury.git/blobdiff_plain/9c8f0adb8bb6c8b86517798d70c2c8dcd198e721..8debbb6ca3804057fe0458a6c2ac579bc9e91162:/migdal/helpers.py?ds=inline diff --git a/migdal/helpers.py b/migdal/helpers.py index 39f5a60..2de7ab0 100644 --- a/migdal/helpers.py +++ b/migdal/helpers.py @@ -34,9 +34,8 @@ def add_translatable(model, fields, languages=None): for name, field in fields.items(): for lang_code, lang_name in languages: new_field = copy(field) - if hasattr(field, 'verbose_name') and field.verbose_name: + if field.verbose_name: new_field.verbose_name = string_concat(field.verbose_name, ' [%s]' % lang_code) - new_field.contribute_to_class(model, "%s_%s" % (name, lang_code)) setattr(model, name, field_getter(name)) # add setter? @@ -72,3 +71,21 @@ 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 +