def test_entity_project_locale_order(entity_test_models): """ Return entities in correct order. """ resource0 = entity_test_models[0].entity.resource locale_a = entity_test_models[0].locale project_a = resource0.project EntityFactory.create( order=2, resource=resource0, string='Second String', ) EntityFactory.create( order=1, resource=resource0, string='First String', ) entities = Entity.map_entities( locale_a, Entity.for_project_locale( project_a, locale_a, ), ) assert entities[1]['original'] == 'First String' assert entities[2]['original'] == 'Second String'
def test_entity_project_locale_subpages(admin, entity_test_models): """ If paths specified as subpages, return project entities from paths assigned to these subpages only along with their translations for locale. """ tr0 = entity_test_models[0] subpageX = entity_test_models[3] locale_a = tr0.locale preferred_source_locale = "" entity_a = tr0.entity resource0 = tr0.entity.resource project_a = tr0.entity.resource.project subpages = [subpageX.name] entities = Entity.map_entities( locale_a, preferred_source_locale, Entity.for_project_locale( admin, project_a, locale_a, subpages, ), ) assert len(entities) == 1 assert entities[0]["path"] == resource0.path assert entities[0]["original"] == entity_a.string assert entities[0]["translation"][0]["string"] == tr0.string
def test_entity_project_locale_plurals( admin, entity_test_models, locale_b, project_b, ): """ For pluralized strings, return all available plural forms. """ tr0, tr0pl, trX, subpageX = entity_test_models locale_a = tr0.locale preferred_source_locale = "" entity_a = tr0.entity project_a = tr0.entity.resource.project entities = Entity.map_entities( locale_a, preferred_source_locale, Entity.for_project_locale( admin, project_a, locale_a, ), ) assert entities[0]["original"] == entity_a.string assert entities[0]["original_plural"] == entity_a.string_plural assert entities[0]["translation"][0]["string"] == tr0.string assert entities[0]["translation"][1]["string"] == tr0pl.string
def save_entity(resource, string, string_plural="", comment="", key="", source=""): """Admin interface: save new or update existing entity in DB.""" # Update existing entity try: if key is "": e = Entity.objects.get( resource=resource, string=string, string_plural=string_plural) else: e = Entity.objects.get(resource=resource, key=key) e.string = string e.string_plural = string_plural e.source = source # Set obsolete attribute for all updated entities to False e.obsolete = False # Add new entity except Entity.DoesNotExist: e = Entity(resource=resource, string=string, string_plural=string_plural, key=key, source=source) if len(comment) > 0: e.comment = comment e.save()
def test_entity_project_locale_order(admin, entity_test_models): """ Return entities in correct order. """ resource0 = entity_test_models[0].entity.resource locale_a = entity_test_models[0].locale preferred_source_locale = "" project_a = resource0.project EntityFactory.create( order=2, resource=resource0, string="Second String", ) EntityFactory.create( order=1, resource=resource0, string="First String", ) entities = Entity.map_entities( locale_a, preferred_source_locale, Entity.for_project_locale( admin, project_a, locale_a, ), ) assert entities[1]["original"] == "First String" assert entities[2]["original"] == "Second String"
def test_entity_project_locale_no_paths( entity_test_models, locale_b, project_b, ): """ If paths not specified, return all project entities along with their translations for locale. """ tr0, tr0pl, trX, subpageX = entity_test_models locale_a = tr0.locale entity_a = tr0.entity resource0 = tr0.entity.resource project_a = tr0.entity.resource.project entities = Entity.map_entities( locale_a, Entity.for_project_locale(project_a, locale_a), ) assert len(entities) == 2 assert entities[0]['path'] == resource0.path assert entities[0]['original'] == entity_a.string assert entities[0]['translation'][0]['string'] == tr0.string assert entities[1]['path'] == trX.entity.resource.path assert entities[1]['original'] == trX.entity.string assert entities[1]['translation'][0]['string'] == trX.string # Ensure all attributes are assigned correctly expected = { 'comment': '', 'format': 'po', 'obsolete': False, 'marked': unicode(entity_a.string), 'key': '', 'path': unicode(resource0.path), 'project': project_a.serialize(), 'translation': [ { 'pk': tr0.pk, 'fuzzy': False, 'string': unicode(tr0.string), 'approved': False, 'rejected': False, }, { 'pk': tr0pl.pk, 'fuzzy': False, 'string': unicode(tr0pl.string), 'approved': False, 'rejected': False, }, ], 'order': 0, 'source': [], 'original_plural': unicode(entity_a.string_plural), 'marked_plural': unicode(entity_a.string_plural), 'pk': entity_a.pk, 'original': unicode(entity_a.string), 'visible': False, } assert entities[0] == expected
def test_entity_project_locale_subpages(entity_test_models): """ If paths specified as subpages, return project entities from paths assigned to these subpages only along with their translations for locale. """ tr0 = entity_test_models[0] subpageX = entity_test_models[3] locale_a = tr0.locale entity_a = tr0.entity resource0 = tr0.entity.resource project_a = tr0.entity.resource.project subpages = [subpageX.name] entities = Entity.map_entities( locale_a, Entity.for_project_locale( project_a, locale_a, subpages, ), ) assert len(entities) == 1 assert entities[0]['path'] == resource0.path assert entities[0]['original'] == entity_a.string assert entities[0]['translation'][0]['string'] == tr0.string
def test_mgr_entity_filter_combined(resourceX, localeX, user0): """ All filters should be joined by AND instead of OR. Tests filters against bug introduced by bug 1243115. """ entities = [ Entity.objects.create(resource=resourceX, string="testentity%s" % i) for i in range(0, 2) ] Translation.objects.create(locale=localeX, entity=entities[0], approved=True, fuzzy=False, user=user0) Translation.objects.create(locale=localeX, entity=entities[1], approved=True, fuzzy=False, user=user0) Translation.objects.create(locale=localeX, entity=entities[1], approved=False, fuzzy=False, user=user0) assert (list( Entity.for_project_locale(resourceX.project, localeX, status='suggested', author=user0.email)) == []) assert (list( Entity.for_project_locale(resourceX.project, localeX, status='suggested', time='201001010100-205001010100')) == [])
def _get_all_entities(locale, preferred_source_locale, project, form, entities): """Return entities without pagination. This is used by the in-context mode of the Translate page. """ has_next = False entities_to_map = Entity.for_project_locale( project, locale, paths=form.cleaned_data["paths"], exclude_entities=form.cleaned_data["exclude_entities"], ) visible_entities = entities.values_list("pk", flat=True) return JsonResponse( { "entities": Entity.map_entities( locale, preferred_source_locale, entities_to_map, visible_entities, ), "has_next": has_next, "stats": TranslatedResource.objects.stats( project, form.cleaned_data["paths"], locale), }, safe=False, )
def test_mgr_entity_filter_combined(resource_a, locale_a, user_a): """ All filters should be joined by AND instead of OR. Tests filters against bug introduced by bug 1243115. """ entities = [ EntityFactory.create(resource=resource_a, string="testentity%s" % i,) for i in range(0, 2) ] TranslationFactory.create( locale=locale_a, entity=entities[0], approved=True, user=user_a, ) TranslationFactory.create( locale=locale_a, entity=entities[1], fuzzy=True, user=user_a, ) assert ( list( Entity.for_project_locale( resource_a.project, locale_a, status="unreviewed", author=user_a.email, ) ) == [] ) assert ( list( Entity.for_project_locale( resource_a.project, locale_a, status="unreviewed", time="201001010100-205001010100", ) ) == [] )
def test_entity_project_locale_cleaned_key(entity_test_models): """ If key contanis source string and Translate Toolkit separator, remove them. """ resource0 = entity_test_models[0].entity.resource locale0 = entity_test_models[0].locale project0 = resource0.project entities = Entity.map_entities( locale0, Entity.for_project_locale(project0, locale0)) assert entities[0]['key'] == '' assert entities[1]['key'] == 'Key'
def test_for_project_locale_filter(self): """ Evaluate entities filtering by locale, project, obsolete. """ other_locale = LocaleFactory.create() other_project = ProjectFactory.create(locales=[self.locale, other_locale]) obsolete_entity = EntityFactory.create(obsolete=True, resource=self.main_resource, string="Obsolete String") entities = Entity.for_project_locale(self.project, other_locale) assert_equal(len(entities), 0) entities = Entity.for_project_locale(other_project, self.locale) assert_equal(len(entities), 0) entities = Entity.for_project_locale(self.project, self.locale) assert_equal(len(entities), 2)
def test_entity_project_locale_plurals(entity_test_models, localeX, project1): """ For pluralized strings, return all available plural forms. """ tr0, tr0pl, trX, subpageX = entity_test_models locale0 = tr0.locale entity0 = tr0.entity project0 = tr0.entity.resource.project entities = Entity.map_entities( locale0, Entity.for_project_locale(project0, locale0)) assert entities[0]['original'] == entity0.string assert entities[0]['original_plural'] == entity0.string_plural assert entities[0]['translation'][0]['string'] == tr0.string assert entities[0]['translation'][1]['string'] == tr0pl.string
def test_entity_project_locale_filter(entity_test_models, localeX, project1): """ Evaluate entities filtering by locale, project, obsolete. """ tr0, tr0pl, trX, subpageX = entity_test_models locale0 = tr0.locale resource0 = tr0.entity.resource project0 = tr0.entity.resource.project Entity.objects.create(obsolete=True, resource=resource0, string='Obsolete String') assert len(Entity.for_project_locale(project0, localeX)) == 0 assert len(Entity.for_project_locale(project1, locale0)) == 0 assert len(Entity.for_project_locale(project0, locale0)) == 2
def test_entity_project_locale_tags(entity0, locale0, tag0): """ Test filtering of tags in for_project_locale """ resource0 = entity0.resource project0 = resource0.project entities = Entity.for_project_locale( project0, locale0, tag=tag0.slug) assert entity0 in entities # remove the resource <> tag association resource0.tag_set.remove(tag0) entities = Entity.for_project_locale( project0, locale0, tag=tag0.slug) assert entity0 not in entities
def test_entity_project_locale_paths(entity_test_models): """ If paths specified, return project entities from these paths only along with their translations for locale. """ tr0, tr0pl, trX, subpageX = entity_test_models locale0 = tr0.locale project0 = tr0.entity.resource.project paths = ['resourceX.po'] entities = Entity.map_entities( locale0, Entity.for_project_locale(project0, locale0, paths)) assert len(entities) == 1 assert entities[0]['path'] == trX.entity.resource.path assert entities[0]['original'] == trX.entity.string assert entities[0]['translation'][0]['string'] == trX.string
def _save_entity(project, original, comment="", key="", source=""): """Admin interface: save new or update existing entity in DB.""" try: # Update existing entity if key is "": e = Entity.objects.get(project=project, string=original) else: e = Entity.objects.get(project=project, key=key, source=source) e.string = original except Entity.DoesNotExist: # Add new entity e = Entity(project=project, string=original, key=key, source=source) if len(comment) > 0: e.comment = comment e.save()
def entities(request): """Get entities for the specified project, locale and paths.""" form = forms.GetEntitiesForm(request.POST) if not form.is_valid(): return HttpResponseBadRequest(form.errors.as_json()) project = get_object_or_404(Project, slug=form.cleaned_data['project']) locale = get_object_or_404(Locale, code=form.cleaned_data['locale']) # Only return entities with provided IDs (batch editing) if form.cleaned_data['entity_ids']: return _get_entities_list(locale, project, form) # `Entity.for_project_locale` only requires a subset of the fields the form contains. We thus # make a new dict with only the keys we want to pass to that function. restrict_to_keys = ( 'paths', 'status', 'search', 'exclude_entities', 'extra', 'time', 'author', ) form_data = {k: form.cleaned_data[k] for k in restrict_to_keys if k in form.cleaned_data} entities = Entity.for_project_locale(project, locale, **form_data) # Only return a list of entity PKs (batch editing: select all) if form.cleaned_data['pk_only']: return JsonResponse({ 'entity_pks': list(entities.values_list('pk', flat=True)), }) # In-place view: load all entities if form.cleaned_data['inplace_editor']: return _get_all_entities(locale, project, form, entities) # Out-of-context view: paginate entities return _get_paginated_entities(locale, project, form, entities)
def test_mgr_bulk_update(get_word_count_mock, resource_a, locale_a): """ Update entities method works and updates word_count field """ get_word_count_mock.return_value = 2 objs = [ EntityFactory.create( resource=resource_a, string="testentity %s" % i, ) for i in range(0, 2) ] assert get_word_count_mock.call_count == 2 testEntitiesQuerySet = Entity.for_project_locale(resource_a.project, locale_a) updated_count = testEntitiesQuerySet.bulk_update( objs, update_fields=[ "resource", "string", "string_plural", "key", "comment", "group_comment", "resource_comment", "order", "source", ], ) assert get_word_count_mock.call_count == 4 assert updated_count == len(objs)
def _save_new_strings(project, source): """Save a batch of strings into an existing project. This function takes a batch of new strings as a blob of text, separate individual strings by new lines, and then stores each one as a new source string for the project. :arg Project project: the Project object to which new strings will be associated :arg string source: a text of new-line-separated source strings :returns: True if new strings have been saved, False otherwise """ new_strings = source.strip().split("\n") # Remove empty strings from the list. new_strings = [x.strip() for x in new_strings if x.strip()] if new_strings: # Create a new fake resource for that project. resource = _get_resource_for_database_project(project) resource.total_strings = len(new_strings) resource.save() # Insert all new strings into Entity objects, associated to the fake resource. new_entities = [] for index, new_string in enumerate(new_strings): string = new_string.strip() new_entities.append( Entity(string=string, resource=resource, order=index)) Entity.objects.bulk_create(new_entities) return True return False
def execute_create_db(self): for vcs_entity in self.changes['create_db']: entity = Entity(**self.get_entity_updates(vcs_entity)) entity.save() # We can't use bulk_create since we need a PK for locale_code, vcs_translation in vcs_entity.translations.items(): for plural_form, string in vcs_translation.strings.items(): self.translations_to_create.append(Translation( entity=entity, locale=self.locales[locale_code], string=string, plural_form=plural_form, approved=not vcs_translation.fuzzy, approved_date=self.now if not vcs_translation.fuzzy else None, fuzzy=vcs_translation.fuzzy ))
def entities(request, template=None): """Get entities for the specified project, locale and paths.""" log.debug("Get entities for the specified project, locale and paths.") if not request.is_ajax(): log.error("Non-AJAX request") raise Http404 try: project = request.GET['project'] locale = request.GET['locale'] paths = json.loads(request.GET['paths']) except MultiValueDictKeyError as e: log.error(str(e)) return HttpResponse("error") log.debug("Project: " + project) log.debug("Locale: " + locale) log.debug("Paths: " + str(paths)) try: project = Project.objects.get(pk=project) except Entity.DoesNotExist as e: log.error(str(e)) return HttpResponse("error") try: locale = Locale.objects.get(code__iexact=locale) except Locale.DoesNotExist as e: log.error(str(e)) return HttpResponse("error") entities = Entity.for_project_locale(project, locale, paths) return HttpResponse(json.dumps(entities), content_type='application/json')
def test_entity_project_locale_filter(entity_test_models, locale_b, project_b): """ Evaluate entities filtering by locale, project, obsolete. """ tr0, tr0pl, trX, subpageX = entity_test_models locale_a = tr0.locale resource0 = tr0.entity.resource project_a = tr0.entity.resource.project EntityFactory.create( obsolete=True, resource=resource0, string='Obsolete String', ) assert len(Entity.for_project_locale(project_a, locale_b)) == 0 assert len(Entity.for_project_locale(project_b, locale_a)) == 0 assert len(Entity.for_project_locale(project_a, locale_a)) == 2
def entities(request): """Get entities for the specified project, locale and paths.""" try: project = request.GET['project'] locale = request.GET['locale'] paths = json.loads(request.GET['paths']) except MultiValueDictKeyError as e: log.error(str(e)) return HttpResponse("error") try: project = Project.objects.get(slug=project) except Entity.DoesNotExist as e: log.error(str(e)) return HttpResponse("error") try: locale = Locale.objects.get(code__iexact=locale) except Locale.DoesNotExist as e: log.error(str(e)) return HttpResponse("error") search = None if request.GET.get('keyword', None): search = request.GET entities = Entity.for_project_locale(project, locale, paths, search) return JsonResponse(entities, safe=False)
def entities(request): """Get entities for the specified project, locale and paths.""" try: project = request.GET['project'] locale = request.GET['locale'] paths = json.loads(request.GET['paths']) except MultiValueDictKeyError as e: log.error(str(e)) return HttpResponse("error") try: project = Project.objects.get(slug=project) except Entity.DoesNotExist as e: log.error(str(e)) return HttpResponse("error") try: locale = Locale.objects.get(code__iexact=locale) except Locale.DoesNotExist as e: log.error(str(e)) return HttpResponse("error") search = None if request.GET.get('keyword', None): search = request.GET entities = Entity.for_project_locale(project, locale, paths, search) return HttpResponse(json.dumps(entities), content_type='application/json')
def get_sibling_entities(request): """Get entities preceding and succeeding the current entity""" try: entity = int(request.GET["entity"]) locale = request.GET["locale"] except (MultiValueDictKeyError, ValueError) as e: return JsonResponse( { "status": False, "message": f"Bad Request: {e}" }, status=400, ) entity = get_object_or_404(Entity, pk=entity) locale = get_object_or_404(Locale, code=locale) preferred_source_locale = "" if request.user.is_authenticated: preferred_source_locale = request.user.profile.preferred_source_locale entities = Entity.objects.filter(resource=entity.resource, obsolete=False).order_by("order") succeeding_entities = entities.filter(order__gt=entity.order)[:2] preceding_entities = entities.filter( order__lt=entity.order).order_by("-order")[:2] return JsonResponse( { "succeeding": Entity.map_entities( locale, preferred_source_locale, succeeding_entities, [], True, ), "preceding": Entity.map_entities( locale, preferred_source_locale, preceding_entities, [], True, ), }, safe=False, )
def test_entity_project_locale_tags(entity_a, locale_a, tag_a): """ Test filtering of tags in for_project_locale """ resource = entity_a.resource project = resource.project entities = Entity.for_project_locale( project, locale_a, tag=tag_a.slug, ) assert entity_a in entities # remove the resource <> tag association resource.tag_set.remove(tag_a) entities = Entity.for_project_locale( project, locale_a, tag=tag_a.slug, ) assert entity_a not in entities
def test_entity_project_locale_cleaned_key(entity_test_models): """ If key contanis source string and Translate Toolkit separator, remove them. """ resource0 = entity_test_models[0].entity.resource locale_a = entity_test_models[0].locale project_a = resource0.project entities = Entity.map_entities( locale_a, Entity.for_project_locale( project_a, locale_a, ), ) assert entities[0]['key'] == '' assert entities[1]['key'] == 'Key'
def test_for_project_locale_filter(self): """ Evaluate entities filtering by locale, project, obsolete. """ other_locale = LocaleFactory.create() other_project = ProjectFactory.create( locales=[self.locale, other_locale]) # Obsolete_entity EntityFactory.create(obsolete=True, resource=self.main_resource, string='Obsolete String') entities = Entity.for_project_locale(self.project, other_locale) assert_equal(len(entities), 0) entities = Entity.for_project_locale(other_project, self.locale) assert_equal(len(entities), 0) entities = Entity.for_project_locale(self.project, self.locale) assert_equal(len(entities), 2)
def test_for_project_locale_cleaned_key(self): """ If key contais source string and Translate Toolkit separator, remove them. """ entities = Entity.for_project_locale(self.project, self.locale) assert_equal(entities[0]["key"], "") assert_equal(entities[1]["key"], "Key")
def test_for_project_locale_cleaned_key(self): """ If key contais source string and Translate Toolkit separator, remove them. """ entities = Entity.for_project_locale(self.project, self.locale) assert_equal(entities[0]['key'], '') assert_equal(entities[1]['key'], 'Key')
def test_for_project_locale_order(self): """ Return entities in correct order. """ entity_second = EntityFactory.create(order=1, resource=self.main_resource, string="Second String") entity_first = EntityFactory.create(order=0, resource=self.main_resource, string="First String") entities = Entity.for_project_locale(self.project, self.locale) assert_equal(entities[2]["original"], "First String") assert_equal(entities[3]["original"], "Second String")
def test_for_project_locale_plurals(self): """ For pluralized strings, return all available plural forms. """ entities = Entity.for_project_locale(self.project, self.locale) assert_equal(entities[0]["original"], "Source String") assert_equal(entities[0]["original_plural"], "Plural Source String") assert_equal(entities[0]["translation"][0]["string"], "Translated String") assert_equal(entities[0]["translation"][1]["string"], "Translated Plural String")
def test_for_project_locale_paths(self): """ If paths specified, return project entities from these paths only along with their translations for locale. """ paths = ["other.lang"] entities = Entity.for_project_locale(self.project, self.locale, paths) assert_equal(len(entities), 1) self.assert_serialized_entity(entities[0], "other.lang", "Other Source String", "Other Translated String")
def test_for_project_locale_plurals(self): """ For pluralized strings, return all available plural forms. """ entities = Entity.for_project_locale(self.project, self.locale) assert_equal(entities[0]['original'], 'Source String') assert_equal(entities[0]['original_plural'], 'Plural Source String') assert_equal(entities[0]['translation'][0]['string'], 'Translated String') assert_equal(entities[0]['translation'][1]['string'], 'Translated Plural String')
def test_entity_project_locale_cleaned_key(entity_test_models): """ If key contanis source string and Translate Toolkit separator, remove them. """ resource0 = entity_test_models[0].entity.resource locale_a = entity_test_models[0].locale preferred_source_locale = "" project_a = resource0.project entities = Entity.map_entities( locale_a, preferred_source_locale, Entity.for_project_locale( project_a, locale_a, ), ) assert entities[0]["key"] == "" assert entities[1]["key"] == "Key"
def test_for_project_locale_subpages(self): """ If paths specified as subpages, return project entities from paths assigned to these subpages only along with their translations for locale. """ subpages = [self.subpage.name] entities = Entity.for_project_locale(self.project, self.locale, subpages) assert_equal(len(entities), 1) self.assert_serialized_entity(entities[0], "main.lang", "Source String", "Translated String")
def test_for_project_locale_no_paths(self): """ If paths not specified, return all project entities along with their translations for locale. """ entities = Entity.for_project_locale(self.project, self.locale) assert_equal(len(entities), 2) self.assert_serialized_entity(entities[0], 'main.lang', 'Source String', 'Translated String') self.assert_serialized_entity(entities[1], 'other.lang', 'Other Source String', 'Other Translated String') # Ensure all attributes are assigned correctly assert_equal( entities[0], { 'comment': '', 'format': 'po', 'obsolete': False, 'marked': 'Source String', 'key': '', 'path': 'main.lang', 'translation': [{ 'pk': self.main_translation.pk, 'fuzzy': False, 'string': 'Translated String', 'approved': False }, { 'pk': self.main_translation_plural.pk, 'fuzzy': False, 'string': 'Translated Plural String', 'approved': False }], 'order': 0, 'source': [], 'original_plural': 'Plural Source String', 'marked_plural': 'Plural Source String', 'pk': self.main_entity.pk, 'original': 'Source String', 'has_suggestions': False, })
def _get_all_entities(locale, project, form, entities): """Return entities without pagination. This is used by the in-context mode of the Translate page. """ has_next = False entities_to_map = Entity.for_project_locale( project, locale, paths=form.cleaned_data['paths'], exclude_entities=form.cleaned_data['exclude_entities'] ) visible_entities = entities.values_list('pk', flat=True) return JsonResponse({ 'entities': Entity.map_entities(locale, entities_to_map, visible_entities), 'has_next': has_next, 'stats': TranslatedResource.objects.stats( project, form.cleaned_data['paths'], locale ), }, safe=False)
def test_for_project_locale_paths(self): """ If paths specified, return project entities from these paths only along with their translations for locale. """ paths = ['other.lang'] entities = Entity.for_project_locale(self.project, self.locale, paths) assert_equal(len(entities), 1) self.assert_serialized_entity(entities[0], 'other.lang', 'Other Source String', 'Other Translated String')
def test_entity_project_locale_paths(entity_test_models): """ If paths specified, return project entities from these paths only along with their translations for locale. """ tr0, tr0pl, trX, subpageX = entity_test_models locale_a = tr0.locale project_a = tr0.entity.resource.project paths = ['resourceX.po'] entities = Entity.map_entities( locale_a, Entity.for_project_locale( project_a, locale_a, paths, ), ) assert len(entities) == 1 assert entities[0]['path'] == trX.entity.resource.path assert entities[0]['original'] == trX.entity.string assert entities[0]['translation'][0]['string'] == trX.string
def test_mgr_entity_filter_combined(resource_a, locale_a, user_a): """ All filters should be joined by AND instead of OR. Tests filters against bug introduced by bug 1243115. """ entities = [ EntityFactory.create( resource=resource_a, string="testentity%s" % i, ) for i in range(0, 2) ] TranslationFactory.create( locale=locale_a, entity=entities[0], approved=True, user=user_a, ) TranslationFactory.create( locale=locale_a, entity=entities[1], fuzzy=True, user=user_a, ) assert ( list(Entity.for_project_locale( resource_a.project, locale_a, status='unreviewed', author=user_a.email, )) == [] ) assert ( list(Entity.for_project_locale( resource_a.project, locale_a, status='unreviewed', time='201001010100-205001010100', )) == [] )
def test_entity_project_locale_plurals( entity_test_models, locale_b, project_b, ): """ For pluralized strings, return all available plural forms. """ tr0, tr0pl, trX, subpageX = entity_test_models locale_a = tr0.locale entity_a = tr0.entity project_a = tr0.entity.resource.project entities = Entity.map_entities( locale_a, Entity.for_project_locale( project_a, locale_a, ), ) assert entities[0]['original'] == entity_a.string assert entities[0]['original_plural'] == entity_a.string_plural assert entities[0]['translation'][0]['string'] == tr0.string assert entities[0]['translation'][1]['string'] == tr0pl.string
def _get_entities_list(locale, project, form): """Return a specific list of entities, as defined by the `entity_ids` field of the form. This is used for batch editing. """ entities = ( Entity.objects.filter(pk__in=form.cleaned_data['entity_ids']) .distinct() .order_by('order') ) return JsonResponse({ 'entities': Entity.map_entities(locale, entities), 'stats': TranslatedResource.objects.stats( project, form.cleaned_data['paths'], locale ), }, safe=False)
def entities(request): """Get entities for the specified project, locale and paths.""" try: project = request.GET['project'] locale = request.GET['locale'] paths = json.loads(request.GET['paths']) except (MultiValueDictKeyError, ValueError) as e: return HttpResponseBadRequest('Bad Request: {error}'.format(error=e)) project = get_object_or_404(Project, slug=project) locale = get_object_or_404(Locale, code__iexact=locale) search = None if request.GET.get('keyword', None): search = request.GET entities = Entity.for_project_locale(project, locale, paths, search) return JsonResponse(entities, safe=False)
def test_manage_project_strings_new(client_superuser, locale_a): project = ProjectFactory.create( data_source='database', repositories=[], locales=[locale_a], ) url = reverse('pontoon.admin.project.strings', args=(project.slug,)) # Test sending a well-formatted batch of strings. new_strings = """Hey, I just met you And this is crazy But here's my number So call me maybe? """ response = client_superuser.post(url, {'new_strings': new_strings}) assert response.status_code == 200 # Verify a resource has been created. resources = list(Resource.objects.filter(project=project)) assert len(resources) == 1 assert resources[0].path == 'database' # Verify all strings have been created as entities. entities = Entity.for_project_locale(project, locale_a) assert len(entities) == 4 expected_strings = [ 'Hey, I just met you', 'And this is crazy', 'But here\'s my number', 'So call me maybe?', ] assert expected_strings == [x.string for x in entities] # Verify strings have the correct order. for index, entity in enumerate(entities): assert entity.order == index # Verify new strings appear on the page. assert 'Hey, I just met you' in response.content
def test_for_project_locale_order(self): """ Return entities in correct order. """ # First entity EntityFactory.create( order=1, resource=self.main_resource, string='Second String' ) # Second entity EntityFactory.create( order=0, resource=self.main_resource, string='First String' ) entities = Entity.for_project_locale(self.project, self.locale) assert_equal(entities[2]['original'], 'First String') assert_equal(entities[3]['original'], 'Second String')
def test_for_project_locale_no_paths(self): """ If paths not specified, return all project entities along with their translations for locale. """ entities = Entity.for_project_locale(self.project, self.locale) assert_equal(len(entities), 2) self.assert_serialized_entity( entities[0], 'main.lang', 'Source String', 'Translated String') self.assert_serialized_entity( entities[1], 'other.lang', 'Other Source String', 'Other Translated String') # Ensure all attributes are assigned correctly assert_equal(entities[0], { 'comment': '', 'format': 'po', 'obsolete': False, 'marked': 'Source String', 'key': '', 'path': 'main.lang', 'translation': [{ 'pk': self.main_translation.pk, 'fuzzy': False, 'string': 'Translated String', 'approved': False }, { 'pk': self.main_translation_plural.pk, 'fuzzy': False, 'string': 'Translated Plural String', 'approved': False }], 'order': 0, 'source': [], 'original_plural': 'Plural Source String', 'marked_plural': 'Plural Source String', 'pk': self.main_entity.pk, 'original': 'Source String', 'has_suggestions': False, })
def test_for_project_locale_no_paths(self): """ If paths not specified, return all project entities along with their translations for locale. """ entities = Entity.for_project_locale(self.project, self.locale) assert_equal(len(entities), 2) self.assert_serialized_entity(entities[0], "main.lang", "Source String", "Translated String") self.assert_serialized_entity(entities[1], "other.lang", "Other Source String", "Other Translated String") # Ensure all attributes are assigned correctly assert_equal( entities[0], { "comment": "", "format": "po", "obsolete": False, "marked": "Source String", "key": "", "path": "main.lang", "translation": [ {"pk": self.main_translation.pk, "fuzzy": False, "string": "Translated String", "approved": False}, { "pk": self.main_translation_plural.pk, "fuzzy": False, "string": "Translated Plural String", "approved": False, }, ], "order": 0, "source": [], "original_plural": "Plural Source String", "marked_plural": "Plural Source String", "pk": self.main_entity.pk, "original": "Source String", "has_suggestions": False, }, )
def _get_paginated_entities(locale, project, form, entities): """Return a paginated list of entities. This is used by the regular mode of the Translate page. """ paginator = Paginator(entities, form.cleaned_data['limit']) try: entities_page = paginator.page(1) except EmptyPage: return JsonResponse({ 'has_next': False, 'stats': {}, }) has_next = entities_page.has_next() entities_to_map = entities_page.object_list # If requested entity not on the first page if form.cleaned_data['entity']: entity_pk = form.cleaned_data['entity'] entities_to_map_pks = [e.pk for e in entities_to_map] # TODO: entities_to_map.values_list() doesn't return entities from selected page if entity_pk not in entities_to_map_pks: if entity_pk in entities.values_list('pk', flat=True): entities_to_map_pks.append(entity_pk) entities_to_map = entities.filter(pk__in=entities_to_map_pks) return JsonResponse({ 'entities': Entity.map_entities(locale, entities_to_map, []), 'has_next': has_next, 'stats': TranslatedResource.objects.stats( project, form.cleaned_data['paths'], locale ), }, safe=False)
def entities(request): """Get entities for the specified project, locale and paths.""" try: project = request.POST['project'] locale = request.POST['locale'] paths = request.POST.getlist('paths[]') limit = int(request.POST.get('limit', 50)) except (MultiValueDictKeyError, ValueError) as err: return HttpResponseBadRequest('Bad Request: {error}'.format(error=err)) project = get_object_or_404(Project, slug=project) locale = get_object_or_404(Locale, code__iexact=locale) filter_type = request.POST.get('filter', '') search = request.POST.get('search', '') exclude_entities = request.POST.getlist('excludeEntities[]', []) # Only return entities with provided IDs entity_ids = request.POST.getlist('entityIds[]', []) if entity_ids: entities = ( Entity.objects.filter(pk__in=entity_ids) .prefetch_resources_translations(locale) .distinct() .order_by('order') ) return JsonResponse({ 'entities': Entity.map_entities(locale, entities), 'stats': TranslatedResource.objects.stats(project, paths, locale), }, safe=False) entities = Entity.for_project_locale( project, locale, paths, filter_type, search, exclude_entities ) # Only return a list of entity PKs if request.POST.get('pkOnly', None): return JsonResponse({ 'entity_pks': list(entities.values_list('pk', flat=True)), }) visible_entities = [] # In-place view: load all entities if request.POST.get('inplaceEditor', None): has_next = False entities_to_map = Entity.for_project_locale( project, locale, paths, None, None, exclude_entities ) visible_entities = entities.values_list('pk', flat=True) # Out-of-context view: paginate entities else: paginator = Paginator(entities, limit) try: entities_page = paginator.page(1) except EmptyPage: return JsonResponse({ 'has_next': False, 'stats': {}, }) has_next = entities_page.has_next() entities_to_map = entities_page.object_list # If requested entity not on the first page entity = request.POST.get('entity', None) if entity: entity_pk = int(entity) # TODO: entities_to_map.values_list() doesn't return entities from selected page if entity_pk not in [e.pk for e in entities_to_map]: if entity_pk in entities.values_list('pk', flat=True): entities_to_map = list(entities_to_map) + list(entities.filter(pk=entity_pk)) return JsonResponse({ 'entities': Entity.map_entities(locale, entities_to_map, visible_entities), 'has_next': has_next, 'stats': TranslatedResource.objects.stats(project, paths, locale), }, safe=False)