def _parse_item(self, item) -> list: self._check_item(item) translation, is_new_translation = self._get_translation( item['id'], None, item['link'] ) title, is_new_title = self._get_title(item, None, translation) translator = self._get_translator(item, translation) translation.save() if is_new_title: self._set_material_data(item, title) episode, is_new_episode = Episode.objects.get_or_create( defaults={'url': item['link'],}, number=1, translation=translation, ) return [episode] if is_new_episode else []
def test_translatedfieldsmodel_str(self): """Test converting TranslatedFieldsModel to string""" missing_language_code = 'xx' obj = SimpleModel.objects.create(tr_title='Something') # Adjust translation object to use language_code that is not # configured. It is easier because various Django version behave # differently if we try to use not configured language. translation = obj.translations.get() translation.language_code = missing_language_code translation.save() # Try to get str() of the TranslatedFieldsModel instance. try: translation_as_str = str(obj.translations.get()) except KeyError: self.fail("Converting translation to string raises KeyError") # Check that we get language code as a fallback, when language is # not configured. self.assertEqual(translation_as_str, missing_language_code)
def _parse_item(self, item) -> list: self._check_item(item) added_episodes = [] for season, season_data in item['seasons'].items(): translation, is_new_translation = self._get_translation( item['id'], season, season_data['link'] ) title, is_new_title = self._get_title(item, season, translation) translator = self._get_translator(item, translation) translation.save() if is_new_title: self._set_material_data(item, title) added_episodes.extend( self._set_episodes(item['seasons'][season]['episodes'], translation) ) return added_episodes
def rate_translation(request: HttpRequest): """ 评价某条其他用户的翻译 :param request: :return: """ tid = get_json(request, 'id') liked = get_json(request, 'liked') try: translation = UserTranslation.objects.get(pk=tid) except UserTranslation.DoesNotExist: return error_response(ErrorEnum.NOT_FOUND) if liked: translation.likes = F('likes') + 1 else: translation.dislikes = F('dislikes') + 1 translation.save() translation.refresh_from_db() return success({ 'likes': translation.likes, 'dislikes': translation.dislikes })
def test_clean_on_save(self): translation = NoURLsTranslation.objects.create(id=1001, localized_string='foo') assert (translation.localized_string_clean == translation.localized_string == 'foo') translation = NoURLsTranslation.objects.create( id=1002, localized_string='foo', localized_string_clean='bar') assert (translation.localized_string_clean == translation.localized_string == 'foo') translation = NoURLsTranslation.objects.create( id=1003, localized_string='foo http://example.com', localized_string_clean=None, ) assert translation.localized_string == 'foo http://example.com' assert translation.localized_string_clean == 'foo' translation.localized_string_clean = 'xaxaxa' translation.save() assert translation.localized_string == 'foo http://example.com' assert translation.localized_string_clean == 'foo'