def __new__(cls, name, bases, attrs): formfield_callback = attrs.get('formfield_callback', None) try: parents = [b for b in bases if issubclass(b, TranslationModelForm)] except NameError: # We are defining TranslationModelForm itself. parents = None new_class = super(TranslationModelFormMetaclass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class opts = new_class._meta if opts.model: if translation_pool.is_registered(opts.model): info = translation_pool.get_info(opts.model) translated_model = info.translated_model new_class.child_form_class = modelform_factory( translated_model, exclude=[info.translation_of_field], formfield_callback=formfield_callback) new_class.declared_fields.update( new_class.child_form_class.declared_fields) new_class.base_fields.update( new_class.child_form_class.base_fields) return new_class
def filter_queryset_language(request, queryset): language = getattr(request, 'LANGUAGE_CODE') model = queryset.model if translation_pool.is_registered(model): info = translation_pool.get_info(model) filter_expr = '%s__%s' % (info.translation_join_filter, info.language_field) if translation_pool.is_registered_translation(model): info = translation_pool.get_info(model) filter_expr = '%s' % info.language_field if filter_expr: queryset = queryset.filter( \ **{filter_expr: language}).distinct() return queryset
def filter_queryset_language(request, queryset): language = getattr(request, 'LANGUAGE_CODE', None) if not language: return queryset model = queryset.model filter_expr = None if translation_pool.is_registered(model): info = translation_pool.get_info(model) filter_expr = '%s__%s' % (info.translation_join_filter, info.language_field) if translation_pool.is_registered_translation(model): info = translation_pool.get_info(model) filter_expr = '%s' % info.language_field if filter_expr: queryset = queryset.filter( \ **{filter_expr: language}).distinct() return queryset
def __new__(cls, name, bases, attrs): formfield_callback = attrs.get('formfield_callback', None) try: parents = [b for b in bases if issubclass(b, TranslationModelForm)] except NameError: # We are defining TranslationModelForm itself. parents = None new_class = super(TranslationModelFormMetaclass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class opts = new_class._meta if opts.model: if translation_pool.is_registered(opts.model): info = translation_pool.get_info(opts.model) translated_model = info.translated_model new_class.child_form_class = modelform_factory(translated_model, exclude=[info.translation_of_field], formfield_callback=formfield_callback) new_class.declared_fields.update(new_class.child_form_class.declared_fields) new_class.base_fields.update(new_class.child_form_class.base_fields) return new_class
def process_view(self, request, view_func, view_args, view_kwargs): language = None if "language_code" in view_kwargs: # get language and set tralslation language = view_kwargs.pop("language_code") translation.activate(language) request.LANGUAGE_CODE = translation.get_language() if not language: if not self.has_language_fallback_middlewares(): super(MultilingualGenericsMiddleware, self).process_request(request) language = getattr(request, "LANGUAGE_CODE") if "queryset" in view_kwargs: filter_expr = None model = view_kwargs["queryset"].model if translation_pool.is_registered(model): info = translation_pool.get_info(model) filter_expr = "%s__%s" % (info.translation_join_filter, info.language_field) if translation_pool.is_registered_translation(model): info = translation_pool.get_info(model) filter_expr = "%s" % info.language_field if filter_expr: view_kwargs["queryset"] = view_kwargs["queryset"].filter(**{filter_expr: language}).distinct()