def save(self, *args, **kwargs): translation_fields = getattr(self._meta, 'translation_fields', ()) lang_keys = [] for translation_field, translation_title in translation_fields: try: field_data = splitter.split( getattr(self, translation_field), title=translation_title ) if field_data: setattr( self, translation_field, splitter.convert( field_data, title=translation_title ).strip() ) lang_keys.append(field_data.keys()) except Exception: pass # TODO ordering issue self.languages = list( frozenset.intersection( *[frozenset(key_pair) for key_pair in lang_keys] ) ) return super().save(*args, **kwargs)
def get_text(self, obj): lang = self.context['request'].query_params.get('lang') if lang: split = splitter.split(obj.text) return split.get(lang) or 'languages: {}'.format(list( split.keys())) return obj.text
def to_representation(self, value): lang = self.context['request'].query_params.get('lang') if lang and value: split = splitter.split(value, title=True) return split.get(lang) or 'languages: {}'.format(list( split.keys())) return value
def to_representation(self, value): lang = self.context['request'].query_params.get('lang') item = {"id": value.pk, "name": value.name} if lang: split = splitter.split(value.name, title=True) item["name"] = split.get(lang) or \ 'languages: {}'.format(list(split.keys())) return item
def to_representation(self, value): lang = self.context['request'].query_params.get('lang') if lang and value: # Return given language, if exists langs = splitter.split(value, title=True) if lang in langs.keys(): return langs[lang] # else first language defined in doc. else: return list(langs.items())[0][-1] return value
def update(self, instance, validated_data): validated_data = self.process_categories(validated_data) languages = validated_data.get('languages', []) body = validated_data.get('body', '') title = validated_data.get('title', '') if languages: # update existing original_title = splitter.split(instance.title) original_body = splitter.split(instance.body) body_updates = splitter.split(body) title_updates = splitter.split(title) for lang in languages: if (lang in title_updates.keys()) and \ (lang in body_updates.keys()): original_title.update({lang: title_updates[lang]}) original_body.update({lang: body_updates[lang]}) validated_data['title'] = splitter.convert(original_title, title=True) validated_data['body'] = splitter.convert(original_body) return super(TopicSerializer, self).update(instance, validated_data)
def get_langsplit(lang, value): split = splitter.split(value, title=True) return split.get(lang) or 'languages: {}'.format(list(split.keys()))