def get_latest_activity(cls, project, locale): """Get the latest activity within this project and locale.""" project_locale = get_object_or_none(ProjectLocale, project=project, locale=locale) if project_locale is not None and project_locale.latest_translation is not None: return project_locale.latest_translation.latest_activity else: return None
def save(self, imported=False, *args, **kwargs): super(Translation, self).save(*args, **kwargs) # Only one translation can be approved at a time for any # Entity/Locale. if self.approved: (Translation.objects .filter(entity=self.entity, locale=self.locale, plural_form=self.plural_form) .exclude(pk=self.pk) .update(approved=False, approved_user=None, approved_date=None)) if not imported: # Update stats AFTER changing approval status. stats = update_stats(self.entity.resource, self.locale) # Whenever a translation changes, mark the entity as having # changed in the appropriate locale. We could be smarter about # this but for now this is fine. self.entity.mark_changed(self.locale) # Check and update the latest translation where necessary. self.check_latest_translation(self.entity.resource.project) self.check_latest_translation(self.locale) self.check_latest_translation(stats) project_locale = get_object_or_none( ProjectLocale, project=self.entity.resource.project, locale=self.locale ) if project_locale: self.check_latest_translation(project_locale)
def test_util_base_get_object_or_none(project_a): assert get_object_or_none(Project, slug="does-not-exist") is None assert get_object_or_none(Project, slug=project_a.slug) == project_a
def test_get_object_or_none(self): project = ProjectFactory.create(slug='exists') assert_is_none(get_object_or_none(Project, slug='does-not-exist')) assert_equal(get_object_or_none(Project, slug='exists'), project)
def calculate_stats(self, apps): """Update stats, including denormalized ones.""" Entity = apps.get_model('base', 'Entity') ProjectLocale = apps.get_model('base', 'ProjectLocale') Translation = apps.get_model('base', 'Translation') resource = self.resource locale = self.locale entity_ids = Translation.objects.filter(locale=locale).values('entity') translated_entities = Entity.objects.filter( pk__in=entity_ids, resource=resource, obsolete=False) # Singular translations = Translation.objects.filter( entity__in=translated_entities.filter(string_plural=''), locale=locale, ) approved = translations.filter( approved=True, errors__isnull=True, warnings__isnull=True, ).count() fuzzy = translations.filter( fuzzy=True, errors__isnull=True, warnings__isnull=True, ).count() errors = translations.filter( Q( Q(Q(approved=True) | Q(fuzzy=True)) & Q(errors__isnull=False) ), ).distinct().count() warnings = translations.filter( Q( Q(Q(approved=True) | Q(fuzzy=True)) & Q(warnings__isnull=False) ), ).distinct().count() unreviewed = translations.filter( approved=False, fuzzy=False, rejected=False, ).count() missing = resource.total_strings - approved - fuzzy - errors - warnings # Plural nplurals = len(locale.cldr_plurals.split(',')) or 1 for e in translated_entities.exclude(string_plural='').values_list('pk'): translations = Translation.objects.filter( entity_id=e, locale=locale, ) plural_approved_count = translations.filter( approved=True, errors__isnull=True, warnings__isnull=True, ).count() plural_fuzzy_count = translations.filter( fuzzy=True, errors__isnull=True, warnings__isnull=True, ).count() if plural_approved_count == nplurals: approved += 1 elif plural_fuzzy_count == nplurals: fuzzy += 1 else: plural_errors_count = translations.filter( Q( Q(Q(approved=True) | Q(fuzzy=True)) & Q(errors__isnull=False) ), ).distinct().count() plural_warnings_count = translations.filter( Q( Q(Q(approved=True) | Q(fuzzy=True)) & Q(warnings__isnull=False) ), ).distinct().count() if plural_errors_count: errors += 1 elif plural_warnings_count: warnings += 1 else: missing += 1 plural_unreviewed_count = translations.filter( approved=False, fuzzy=False, rejected=False ).count() if plural_unreviewed_count: unreviewed += 1 # Calculate diffs to reduce DB queries total_strings_diff = resource.total_strings - self.total_strings approved_strings_diff = approved - self.approved_strings fuzzy_strings_diff = fuzzy - self.fuzzy_strings strings_with_errors_diff = errors - self.strings_with_errors strings_with_warnings_diff = warnings - self.strings_with_warnings unreviewed_strings_diff = unreviewed - self.unreviewed_strings # Translated Resource adjust_stats( self, total_strings_diff, approved_strings_diff, fuzzy_strings_diff, strings_with_errors_diff, strings_with_warnings_diff, unreviewed_strings_diff, ) # Project project = resource.project adjust_stats( project, total_strings_diff, approved_strings_diff, fuzzy_strings_diff, strings_with_errors_diff, strings_with_warnings_diff, unreviewed_strings_diff, ) # Locale if not project.system_project: adjust_stats( locale, total_strings_diff, approved_strings_diff, fuzzy_strings_diff, strings_with_errors_diff, strings_with_warnings_diff, unreviewed_strings_diff, ) # ProjectLocale project_locale = utils.get_object_or_none( ProjectLocale, project=project, locale=locale ) if project_locale: adjust_stats( project_locale, total_strings_diff, approved_strings_diff, fuzzy_strings_diff, strings_with_errors_diff, strings_with_warnings_diff, unreviewed_strings_diff, )
def test_util_base_get_object_or_none(project_a): assert get_object_or_none(Project, slug='does-not-exist') is None assert get_object_or_none(Project, slug=project_a.slug) == project_a
def calculate_stats(self, Translation, Entity, ProjectLocale): """Update stats, including denormalized ones.""" resource = self.resource locale = self.locale project = resource.project entity_ids = Translation.objects.filter(locale=locale).values('entity') translated_entities = Entity.objects.filter( pk__in=entity_ids, resource=resource, obsolete=False) # Singular translations = Translation.objects.filter( entity__in=translated_entities.filter(string_plural=''), locale=locale) approved = translations.filter(approved=True).count() fuzzy = translations.filter(fuzzy=True).count() # Plural nplurals = len(map(int, locale.cldr_plurals.split(','))) or 1 missing = 0 for e in translated_entities.exclude(string_plural=''): translations = Translation.objects.filter(entity=e, locale=locale) plural_approved_count = translations.filter(approved=True).count() plural_fuzzy_count = translations.filter(fuzzy=True).count() plural_translated_count = translations.values('plural_form').distinct().count() if plural_approved_count == nplurals: approved += 1 elif plural_fuzzy_count == nplurals: fuzzy += 1 elif plural_translated_count < nplurals: missing += 1 translated = max(translated_entities.count() - approved - fuzzy - missing, 0) # Calculate diffs to reduce DB queries total_strings_diff = resource.total_strings - self.total_strings approved_strings_diff = approved - self.approved_strings fuzzy_strings_diff = fuzzy - self.fuzzy_strings translated_strings_diff = translated - self.translated_strings # Translated Resource adjust_stats( self, total_strings_diff, approved_strings_diff, fuzzy_strings_diff, translated_strings_diff ) # Project adjust_stats( project, total_strings_diff, approved_strings_diff, fuzzy_strings_diff, translated_strings_diff ) # Locale adjust_stats( locale, total_strings_diff, approved_strings_diff, fuzzy_strings_diff, translated_strings_diff ) # ProjectLocale project_locale = utils.get_object_or_none( ProjectLocale, project=project, locale=locale ) if project_locale: adjust_stats( project_locale, total_strings_diff, approved_strings_diff, fuzzy_strings_diff, translated_strings_diff )
def test_util_base_get_object_or_none(project0): assert get_object_or_none(Project, slug='does-not-exist') is None assert get_object_or_none(Project, slug='project0') == project0