Ticket #12924: translation-wtf-party.2.diff

File translation-wtf-party.2.diff, 7.0 KB (added by Alex Gaynor, 9 years ago)

Added some forgotten files (tests), also updated some comments.

  • django/db/models/fields/related.py

    diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
    index 8329792..9235a67 100644
    a b signals.class_prepared.connect(do_pending_lookups) 
    8787class RelatedField(object):
    8888    def contribute_to_class(self, cls, name):
    8989        sup = super(RelatedField, self)
    90 
    91         # Add an accessor to allow easy determination of the related query path for this field
    92         self.related_query_name = curry(self._get_related_query_name, cls._meta)
     90       
     91        # Store the opts for related_query_name()
     92        self.opts = cls._meta
    9393
    9494        if hasattr(sup, 'contribute_to_class'):
    9595            sup.contribute_to_class(cls, name)
    class RelatedField(object): 
    174174            return []
    175175        raise TypeError("Related Field has invalid lookup: %s" % lookup_type)
    176176
    177     def _get_related_query_name(self, opts):
     177    def related_query_name(self):
    178178        # This method defines the name that can be used to identify this
    179179        # related object in a table-spanning query. It uses the lower-cased
    180180        # object_name by default, but this can be overridden with the
    181181        # "related_name" option.
    182         return self.rel.related_name or opts.object_name.lower()
     182        return self.rel.related_name or self.opts.object_name.lower()
    183183
    184184class SingleRelatedObjectDescriptor(object):
    185185    # This class provides the functionality that makes the related-object
  • django/utils/functional.py

    diff --git a/django/utils/functional.py b/django/utils/functional.py
    index e52ab76..4539497 100644
    a b def lazy(func, *resultclasses): 
    147147    the lazy evaluation code is triggered. Results are not memoized; the
    148148    function is evaluated on every access.
    149149    """
     150    # When lazy() is called by the __reduce_ex__ machinery to reconstitute the
     151    # __proxy__ class it can't call with *args, so the first item will just be
     152    # a tuple.
     153    if len(resultclasses) == 1 and isinstance(resultclasses[0], tuple):
     154        resultclasses = resultclasses[0]
     155   
    150156    class __proxy__(Promise):
    151157        """
    152158        Encapsulate a function call and act as a proxy for methods that are
    def lazy(func, *resultclasses): 
    161167            self.__kw = kw
    162168            if self.__dispatch is None:
    163169                self.__prepare_class__()
    164 
     170       
     171        def __reduce_ex__(self, protocol):
     172            return (lazy, (self.__func, resultclasses), self.__dict__)
     173       
    165174        def __prepare_class__(cls):
    166175            cls.__dispatch = {}
    167176            for resultclass in resultclasses:
  • django/utils/translation/__init__.py

    diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py
    index c0a0df9..54fb86d 100644
    a b  
    11"""
    22Internationalization support.
    33"""
    4 from django.utils.functional import lazy
     4from django.conf import settings
    55from django.utils.encoding import force_unicode
     6from django.utils.functional import lazy, curry
     7from django.utils.translation import trans_real, trans_null
     8
    69
    710__all__ = ['gettext', 'gettext_noop', 'gettext_lazy', 'ngettext',
    811        'ngettext_lazy', 'string_concat', 'activate', 'deactivate',
    __all__ = ['gettext', 'gettext_noop', 'gettext_lazy', 'ngettext', 
    1922# replace the functions with their real counterparts (once we do access the
    2023# settings).
    2124
    22 def delayed_loader(*args, **kwargs):
     25def delayed_loader(real_name, *args, **kwargs):
    2326    """
    24     Replace each real_* function with the corresponding function from either
    25     trans_real or trans_null (e.g. real_gettext is replaced with
    26     trans_real.gettext or trans_null.gettext). This function is run once, the
    27     first time any i18n method is called. It replaces all the i18n methods at
    28     once at that time.
     27    Call the real, underlying function.  We have a level of indirection here so
     28    that modules can use the translation bits without actually requiring
     29    Django's settings bits to be configured before import.
    2930    """
    30     import traceback
    31     from django.conf import settings
    3231    if settings.USE_I18N:
    33         import trans_real as trans
     32        trans = trans_real
    3433    else:
    35         import trans_null as trans
    36     caller = traceback.extract_stack(limit=2)[0][2]
    37     g = globals()
    38     for name in __all__:
    39         if hasattr(trans, name):
    40             g['real_%s' % name] = getattr(trans, name)
     34        trans = trans_null
    4135
    4236    # Make the originally requested function call on the way out the door.
    43     return g['real_%s' % caller](*args, **kwargs)
     37    return getattr(trans, real_name)(*args, **kwargs)
    4438
    4539g = globals()
    4640for name in __all__:
    47     g['real_%s' % name] = delayed_loader
     41    g['real_%s' % name] = curry(delayed_loader, name)
    4842del g, delayed_loader
    4943
    5044def gettext_noop(message):
    def templatize(src): 
    10296def deactivate_all():
    10397    return real_deactivate_all()
    10498
    105 def string_concat(*strings):
     99def _string_concat(*strings):
    106100    """
    107101    Lazy variant of string concatenation, needed for translations that are
    108102    constructed from multiple parts.
    109103    """
    110104    return u''.join([force_unicode(s) for s in strings])
    111 string_concat = lazy(string_concat, unicode)
     105string_concat = lazy(_string_concat, unicode)
  • tests/regressiontests/i18n/tests.py

    diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py
    index 31150a6..941f66f 100644
    a b class TranslationTests(TestCase): 
    4646        unicode(string_concat(...)) should not raise a TypeError - #4796
    4747        """
    4848        import django.utils.translation
    49         self.assertEqual(django.utils.translation, reload(django.utils.translation))
    5049        self.assertEqual(u'django', unicode(django.utils.translation.string_concat("dja", "ngo")))
    5150
    5251    def test_safe_status(self):
  • new file tests/regressiontests/queryset_pickle/models.py

    diff --git a/tests/regressiontests/queryset_pickle/__init__.py b/tests/regressiontests/queryset_pickle/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/queryset_pickle/models.py b/tests/regressiontests/queryset_pickle/models.py
    new file mode 100644
    index 0000000..1c7dc4f
    - +  
     1from django.db import models
     2
     3
     4class Group(models.Model):
     5    name = models.CharField(max_length=100)
     6
     7class Event(models.Model):
     8    group = models.ForeignKey(Group)
  • new file tests/regressiontests/queryset_pickle/tests.py

    diff --git a/tests/regressiontests/queryset_pickle/tests.py b/tests/regressiontests/queryset_pickle/tests.py
    new file mode 100644
    index 0000000..cc0d0a6
    - +  
     1import pickle
     2
     3from django.test import TestCase
     4
     5from models import Group, Event
     6
     7
     8class PickleabilityTestCase(TestCase):
     9    def assert_pickles(self, qs):
     10        self.assertEqual(list(pickle.loads(pickle.dumps(qs))), list(qs))
     11   
     12    def test_related_field(self):
     13        g = Group.objects.create(name="Ponies Who Own Maybachs")
     14        self.assert_pickles(Event.objects.filter(group=g.id))
Back to Top