def test_translation_bool(): t = lambda s: Translation(localized_string=s) assert bool(t('text')) is True assert bool(t(' ')) is False assert bool(t('')) is False assert bool(t(None)) is False
def create_translation(model: object, translations: list, object_id: int) -> None: """ create translations objects for model which get from params """ model_type_id = ContentType.objects.get_for_model(model).id list_for_create, billboard_static_list_create = [], [] for translation, number in zip(translations, range(len(translations))): if model is Billboard: if 'url_link' in translation: billboard_static_list_create.append( MediaBillboard(billboard_id=object_id, url_link=translation['url_link'], language=translation['language'])) elif 'pdf_file' in translation: billboard_static_list_create.append( MediaBillboard(billboard_id=object_id, pdf_file=translation['pdf_file'], language=translation['language'])) # special translation model's fields for field in model.TranslatableMeta.fields: list_for_create.append( Translation(text=translation[field], object_id=object_id, field=field, language=translation['language'], content_type_id=model_type_id)) # multiple create Translation.objects.bulk_create(list_for_create) if model is Billboard: MediaBillboard.objects.bulk_create(billboard_static_list_create) return None
def setUp(self): self.feed = feeds.CategoriesRss() self.u = u'Ελληνικά' self.wut = Translation(localized_string=self.u, locale='el') self.feed.request = mock.Mock() self.feed.request.APP.pretty = self.u self.category = Category(name=self.u) self.addon = Addon(name=self.u, id=2, type=1, slug='xx') self.addon._current_version = Version(version='v%s' % self.u)
def setUp(self): self.feed = feeds.ReviewsRss() self.u = u'Ελληνικά' self.wut = Translation(localized_string=self.u, locale='el') self.addon = mock.Mock() self.addon.name = self.wut self.user = mock.Mock() self.user.name = self.u self.review = mock.Mock() self.review.title = self.wut self.review.rating = 4 self.review.user = self.user
def create_translation(self, audio_format, language, phrase, row): if Translation.objects.filter(phrase=phrase, language=language).exists(): return t = Translation(content=row['TRANSLATED_TEXT'], language=language, phrase=phrase) audio_url = row.get('AUDIO_URL') if audio_url: tmp_path = self.download_audio(audio_format, audio_url) md5 = hashlib.md5(row['TRANSLATED_TEXT'].encode()).hexdigest() with open(tmp_path, 'rb') as f: t.audio_clip.save(f'{md5}.{audio_format}', File(f)) try: t.save() except IntegrityError: self.stderr.write(f'FAIL for {row["ENGLISH"]}') return t
def get_trans(items): if not items: return connection = connections[multidb.get_slave()] cursor = connection.cursor() model = items[0].__class__ sql, params = build_query(model, connection) item_dict = dict((item.pk, item) for item in items) ids = ','.join(map(str, item_dict.keys())) cursor.execute(sql.format(ids='(%s)' % ids), tuple(params)) step = len(trans_fields) for row in cursor.fetchall(): # We put the item's pk as the first selected field. item = item_dict[row[0]] for index, field in enumerate(model._meta.translated_fields): start = 1 + step * index t = Translation(*row[start:start + step]) if t.id is not None and t.localized_string is not None: setattr(item, field.name, t)
def get_trans(items): if not items: return model = items[0].__class__ # FIXME: if we knew which db the queryset we are transforming used, we could # make sure we are re-using the same one. dbname = router.db_for_read(model) connection = connections[dbname] sql, params = build_query(model, connection) item_dict = dict((item.pk, item) for item in items) ids = ','.join(map(str, item_dict.keys())) cursor = connection.cursor() cursor.execute(sql.format(ids='(%s)' % ids), tuple(params)) step = len(trans_fields) for row in cursor.fetchall(): # We put the item's pk as the first selected field. item = item_dict[row[0]] for index, field in enumerate(model._meta.translated_fields): start = 1 + step * index t = Translation(*row[start:start+step]) if t.id is not None and t.localized_string is not None: setattr(item, field.name, t)
def test_whitespace(self): t = Translation(localized_string=' khaaaaaan! ', id=999) t.save() eq_('khaaaaaan!', t.localized_string)
def test_page_title_unicode(): t = Translation(localized_string=u'\u30de\u30eb\u30c1\u30d712\u30eb') request = Mock() request.APP = amo.FIREFOX helpers.editor_page_title({'request': request}, title=t)
def create_language(request, lang=None, fvlist=None, clone=False, *args, **kwargs): me = 'language' state = 'new' user = request.user # sort values into categories cats = make_feature_list_for_lang(lang=lang, fvlist=fvlist) editorform = EditorForm() cloned_from_lang = None if clone: author = request.user.profile.display_name name = 'Clone' if lang: name = 'Clone of %s' % lang elif fvlist: name = 'Clone of %i features' % len(fvlist) background = name langform = LanguageForm(initial={ 'name': name, 'background': background, 'author': author }) cloned_from_lang = lang else: langform = LanguageForm() if request.method == 'POST': langform = LanguageForm(data=request.POST, initial=request.POST) if langform.is_valid(): with transaction.atomic(): lang = langform.save(commit=False) if language_exists(lang.name): url = '/language/%s/' % lang.get_slug() msg = '''A language named <a href="%s">%s</a> already exists, you should edit that one or change the name of this one''' % (url, escape(lang.name)) messages.error(request, msg) else: # Good, not a dupe lang.added_by = user lang.last_modified_by = user if not lang.manager: lang.manager = user # Must save early since is foreign-key in many other tables lang.save(user=user, solo=False) # Save tags if any lang, tags_changed = set_tags_for_lang( langform.cleaned_data['tags'], lang) # Set editors editorform = EditorForm(data=request.POST, instance=lang) if editorform.is_valid(): editorform.save() # greeting if lang.greeting: # use signal instead? greetingexercise = TranslationExercise.objects.get( id=1) trans = Translation(translation=lang.greeting, language=lang, translator=user, exercise=greetingexercise) trans.save() # values lang = set_featurevalues_for_lang( lang, request.POST.getlist('value')) # Final save lang.save(user=user) if cloned_from_lang: streamaction.send(request.user, verb='cloned the language', action_object=cloned_from_lang, target=lang) else: streamaction.send(request.user, verb='added the language', action_object=lang) messages.info( request, 'You successfully added the language %s to CALS' % lang.name) return HttpResponseRedirect('/language/%s/' % lang.slug) else: if not clone: error = "Couldn't store language-description: " + str( langform.errors) messages.error(request, error) else: help = "Remember to fill out the name and author of the language" messages.warn(request, help) data = { 'form': langform, 'categories': cats, 'me': me, 'editorform': editorform, 'state': state, 'clone': clone, } return render(request, 'language_form.html', data)
def smorgasbord(request): """ Gather many different kinds of tasty add-ons together. Great for testing install buttons. """ def _compat(min, max): # Helper for faking compatible_apps. return {'min': {'version': min}, 'max': {'version': max}} addons = [] normal_version = _compat('1.0', '10.0') older_version = _compat('1.0', '2.0') newer_version = _compat('9.0', '10.0') def all_versions(addon, base_tag): x = (('', normal_version), (' + older version', older_version), (' + newer version', newer_version)) for extra, version in x: a = addon() a.tag = base_tag + extra a.compatible_apps[request.APP] = version addons.append(a) # Featured. featured = Addon.objects.featured(request.APP) addons.append(featured[0]) addons[-1].tag = 'featured' normal = Addon.objects.listed(request.APP).exclude(id__in=featured) # Normal, Older Version, Newer Version. all_versions(lambda: normal[0], 'normal') # Unreviewed. exp = Addon.objects.unreviewed() all_versions(lambda: exp[0], 'unreviewed') # Multiple Platforms. addons.append(Addon.objects.get(id=2313)) addons[-1].tag = 'platformer' # Multiple Platforms + EULA. addons.append(Addon.objects.get(id=2313)) addons[-1].eula = Translation(localized_string='xxx') addons[-1].tag = 'platformer + eula' # Incompatible Platform + EULa. addons.append(Addon.objects.get(id=5308)) addons[-1].eula = Translation(localized_string='xxx') addons[-1].tag = 'windows/linux-only + eula' # Incompatible Platform. all_versions(lambda: Addon.objects.get(id=5308), 'windows/linux-only') # EULA. eula = (Q(eula__isnull=False, eula__localized_string__isnull=False) & ~Q(eula__localized_string='')) addons.append(normal.filter(eula)[0]) addons[-1].tag = 'eula' addons.append(exp.filter(eula)[0]) addons[-1].tag = 'eula + unreviewed' # Contributions. addons.append(normal.filter(annoying=1)[0]) addons[-1].tag = 'contrib: passive' addons.append(normal.filter(annoying=2)[0]) addons[-1].tag = 'contrib: after' addons.append(normal.filter(annoying=3)[0]) addons[-1].tag = 'contrib: roadblock' addons.append(Addon.objects.get(id=2608)) addons[-1].tag = 'after + eula' addons.append(Addon.objects.get(id=8442)) addons[-1].tag = 'roadblock + eula' # Other App. addons.append(Addon.objects.get(id=5326)) addons[-1].tag = 'tbird' # Mobile. addons.append(Addon.objects.get(id=53476)) addons[-1].tag = 'mobile' # Search Engine. addons.append(Addon.objects.filter(type=amo.ADDON_SEARCH)[0]) addons[-1].tag = 'search engine' # Beta Version beta = normal.filter(versions__files__status=amo.STATUS_BETA)[0] beta.tag = 'beta version' # Theme. # Persona. addons.append(Addon.objects.filter(type=amo.ADDON_PERSONA)[0]) addons[-1].tag = 'persona' # Future Version. # No versions. return jingo.render(request, 'addons/smorgasbord.html', { 'addons': addons, 'beta': beta })
def update_translation(model: object, translations: list, object_id: int) -> object: """ update translations objects for model which get from params """ model_type_id = ContentType.objects.get_for_model(model).id list_for_update, list_for_create, list_for_delete = [], [], [] billboard_static_list_create, billboard_static_list_update = [], [] # compare translations in db with translations in request for translation in translations: old_translations = Translation.objects.filter( object_id=object_id, language=translation['language'], content_type_id=model_type_id) for trans in old_translations: """ prepare list with translation ids, which need to exclude from translation ids in db """ if trans.language == translation['language']: list_for_delete.append(trans.id) # list with translation objects, which should be deleted from db Translation.objects.filter(object_id=object_id, content_type_id=model_type_id).exclude( pk__in=list_for_delete).delete() for translation, number in zip(translations, range(len(translations))): if model is Billboard: try: if 'url_link' in translation: billboard_static_list_create.append( MediaBillboard(billboard_id=object_id, url_link=translation['url_link'], language=translation['language'])) elif 'pdf_file' in translation: billboard_static_list_create.append( MediaBillboard(billboard_id=object_id, pdf_file=translation['pdf_file'], language=translation['language'])) except MediaBillboard.DoesNotExist: if 'url_link' in translation: billboard_static_list_create.append( MediaBillboard(billboard_id=object_id, url_link=translation['url_link'], language=translation['language'])) elif 'pdf_file' in translation: billboard_static_list_create.append( MediaBillboard(billboard_id=object_id, pdf_file=translation['pdf_file'], language=translation['language'])) # special translation model's fields for field in model.TranslatableMeta.fields: try: trans = Translation.objects.get( object_id=object_id, language=translation['language'], field=field, content_type_id=model_type_id) # scrip not changed translations if trans.text == translation[field]: continue list_for_update.append( Translation(id=trans.id, text=translation[field])) except Translation.DoesNotExist: # if translation doesn't exist that create them list_for_create.append( Translation(object_id=object_id, field=field, text=translation[field], language=translation['language'], content_type_id=model_type_id)) # multiple update Translation.objects.bulk_update(list_for_update, ['text']) # multiple create Translation.objects.bulk_create(list_for_create) if model is Billboard: MediaBillboard.objects.bulk_update(billboard_static_list_update, ['url_link', 'pdf_file']) MediaBillboard.objects.bulk_create(billboard_static_list_create) return None
def sync_project(project: Project): """ Syncing the Translation objects with the git repo for a project. This function creates Translation records for every key in a project's localisation files for languages that are configured. """ sync_time = now() GITHUB_ACCESS_TOKEN = config("GITHUB_ACCESS_TOKEN") # Access git repo for project g = Github(GITHUB_ACCESS_TOKEN) repo = g.get_repo(project.repository_name) contents = repo.get_contents(project.locale_files_path) # Get previous translations previous_translations = filter_latest_translations( Translation.objects.filter(project=project) ) # Collect all translations in project, to check which translations are removed all_translations_in_project: Dict[str, Dict[str, Translation]] = {} # Get all files recursively from the repo files = [] while contents: file_content = contents.pop(0) if file_content.type == "dir": contents.extend(repo.get_contents(file_content.path)) else: files.append(file_content) for file in files: # The file path relative to the locale files directory for the project relative_filepath = file.path.replace(project.locale_files_path, ".") # Add file path to all translations dict all_translations_in_project[relative_filepath] = {} # Get language language_code = relative_filepath.split("/")[-1].split(".")[0] # Get commit date for file commit = repo.get_commits(path=file.path)[0].commit commit_date = commit.committer.date # If language in the project languages if language_code in [lang.language_code for lang in project.languages.all()]: try: file_object = yaml.load(file.decoded_content, Loader=BaseLoader) flat_file_object = json_normalize( file_object, sep=".", errors="ignore" ).to_dict(orient="records")[0] # Loop through each translation key/value pair and create translation object for key, value in flat_file_object.items(): if isinstance(value, str): language = Language.objects.get(language_code=language_code) try: translation = Translation( text=value, author="", from_repository=True, file_path=relative_filepath, object_path=key, project=project, language=language, created_at=commit_date, ) # Add translation to all translations in project all_translations_in_project[relative_filepath][ key ] = translation # Attempt to create translation, will be ignored if translation already in project translation.save() # If translation with file_path, object_path & created_at exists, ignore save except IntegrityError: pass except scanner.ScannerError: pass mark_deleted_translations( previous_translations, all_translations_in_project, sync_time ) # When done, set the time the sync occurred project.last_sync_time = sync_time project.save()
def test_css(self): self.assertEqual(Translation(percent=10).css, 'progress-bar-danger') self.assertEqual(Translation(percent=60).css, 'progress-bar-warning') self.assertEqual(Translation(percent=100).css, 'progress-bar-success')
def t(s): return Translation(localized_string=s)
def test_translation_unicode(): t = lambda s: Translation(localized_string=s) eq_(unicode(t('hello')), 'hello') eq_(unicode(t(None)), '')
def test_comparison_with_lazy(): x = Translation(localized_string='xxx') lazy_u = lazy(lambda x: x, unicode) x == lazy_u('xxx') lazy_u('xxx') == x
def test_css(self): self.assertEqual(Translation(percent=10).css, ' b50') self.assertEqual(Translation(percent=60).css, ' b80') self.assertEqual(Translation(percent=100).css, '')