Commits

Ivan Zakrevsky  committed 588fcd7

localization

  • Participants
  • Parent commits 17df642

Comments (0)

Files changed (4)

File imaging/fields.py

 from __future__ import absolute_import, unicode_literals
 from django.db import models
 from django import forms
-from django.utils.translation import ugettext as _
+from django.utils.translation import ugettext_lazy as _, get_language
 from django.db.models.signals import post_save
 from imaging.models import Image
 from imaging.widgets import ImageSelection
     def contribute_to_class(self, cls, name):
         super(ImagingField, self).contribute_to_class(cls, name)
         #content_type = ContentType.objects.get_for_model(cls)
-        setattr(cls, self.name + "_set2", generic.GenericRelation(Image))
+        rel_name = '{}_set'.format(name)
+        generic.GenericRelation(Image).contribute_to_class(cls, rel_name)
+
+        def get_localized_qs(self):
+            return getattr(self, rel_name).all().filter(language=get_language())
+
+        setattr(cls, "{}_qs".format(name), property(get_localized_qs))
 
     def pre_save(self, model_instance, add):
         # set a new attribute which hold the field

File imaging/forms.py

 class AjaxUploadForm(forms.ModelForm):
     class Meta:
         model = Image
-        exclude = ('content_type', 'content_object', 'object_id', 'ordering')
+        exclude = ('content_type', 'content_object', 'object_id', 'ordering', 'language')

File imaging/models.py

 from django.contrib.contenttypes.models import ContentType
 from django.contrib.contenttypes import generic
 from django.db import models
+from django.utils.translation import get_language
 
 try:
     str = unicode  # Python 2.* compatible
     content_type = models.ForeignKey(ContentType, null=True, blank=True)
     object_id = models.PositiveIntegerField(null=True, blank=True)
     content_object = generic.GenericForeignKey('content_type', 'object_id')
+    language = models.CharField(
+        max_length=10,
+        choices=settings.LANGUAGES,
+        default=get_language,
+        blank=False,
+        null=False,
+        db_index=True
+    )
 
     class Meta:
         ordering = ('ordering', 'name')

File imaging/widgets.py

 from django.utils.html import conditional_escape
 from django.utils.encoding import force_unicode
 from django.utils.safestring import mark_safe
+from django.utils.translation import get_language
 
 from imaging.models import Image
 
         value = value.replace(' ', '')
         if value:
             ids = value.split(",")
-            initial_values = Image.objects.filter(pk__in=ids)
+            initial_values = Image.objects.filter(pk__in=ids, language=get_language())
         value = force_unicode(value)
         final_attrs = self.build_attrs(attrs, name=name)
         html = render_to_string('imaging/image_selection.html', {