def test_query_bit(self): INPUT = 'normal__shared_field__exact' query_string, joins = translate(INPUT, Related) self.assertEqual(query_string, INPUT) self.assertEqual(joins, ['normal__translations__language_code']) INPUT = 'normal__translated_field__exact' query_string, joins = translate(INPUT, Related) self.assertEqual(query_string, 'normal__translations__translated_field__exact') self.assertEqual(joins, ['normal__translations__language_code'])
def _translate_fieldnames(self, fields): self.language(self._language_code) newfields = [] extra_filters = Q() language_joins = [] for field in fields: newfield, langjoins = translate(field, self.model) newfields.append(newfield) for langjoin in langjoins: if langjoin not in language_joins: language_joins.append(langjoin) for langjoin in language_joins: extra_filters &= Q(**{langjoin: self._language_code}) return newfields, extra_filters
def _recurse_q(self, q): newchildren = [] language_joins = [] for child in q.children: if isinstance(child, Q): newq = self._recurse_q(child) newchildren.append(self._recurse_q(newq)) else: key, value = child newkey, langjoins = translate(key, self.model) newchildren.append((newkey, value)) for langjoin in langjoins: if langjoin not in language_joins: language_joins.append(langjoin) q.children = newchildren return q, language_joins
def _recurse_q(self, q): newchildren = [] language_joins = [] for child in q.children: if isinstance(child, Q): newq = self._recurse_q(child) newchildren.append(self._recurse_q(newq)) else: key, value = child newkey, langjoins =translate(key, self.model) newchildren.append((newkey, value)) for langjoin in langjoins: if langjoin not in language_joins: language_joins.append(langjoin) q.children = newchildren return q, language_joins
def _translate_args_kwargs(self, *args, **kwargs): self.language(self._language_code) language_joins = [] newkwargs = {} extra_filters = Q() for key, value in kwargs.items(): newkey, langjoins = translate(key, self.model) for langjoin in langjoins: if langjoin not in language_joins: language_joins.append(langjoin) newkwargs[newkey] = value newargs = [] for q in args: new_q, langjoins = self._recurse_q(q) newargs.append(new_q) for langjoin in langjoins: if langjoin not in language_joins: language_joins.append(langjoin) for langjoin in language_joins: extra_filters &= Q(**{langjoin: self._language_code}) return newargs, newkwargs, extra_filters
def test_simple(self): INPUT = 'normal__shared_field' query_string, joins = translate(INPUT, Related) self.assertEqual(query_string, INPUT) self.assertEqual(joins, ['normal__translations__language_code'])
def latest(self, field_name=None): extra_filters = Q() if field_name: field_name, extra_filters = translate(self, field_name) return self._filter_extra(extra_filters).latest(field_name)