def show_project(request, project): obj = get_project(request, project) user = request.user dict_langs = Language.objects.filter(dictionary__project=obj).annotate( Count('dictionary')).order() last_changes = Change.objects.prefetch().order().filter(project=obj)[:10] language_stats = sort_unicode(obj.stats.get_language_stats(), lambda x: force_text(x.language.name)) # Paginate components of project. all_components = obj.component_set.select_related().order() components = prefetch_stats(get_paginator(request, all_components)) return render( request, 'project.html', { 'allow_index': True, 'object': obj, 'project': obj, 'dicts': dict_langs, 'last_changes': last_changes, 'last_changes_url': urlencode({'project': obj.slug}), 'language_stats': language_stats, 'search_form': SearchForm(), 'whiteboard_form': optional_form(WhiteboardForm, user, 'project.edit', obj), 'delete_form': optional_form(DeleteForm, user, 'project.edit', obj, obj=obj), 'rename_form': optional_form(ProjectRenameForm, user, 'project.edit', obj, request=request, instance=obj), 'replace_form': optional_form(ReplaceForm, user, 'unit.edit', obj), 'bulk_state_form': optional_form(BulkStateForm, user, 'translation.auto', obj, user=user, obj=obj), 'components': components, 'licenses': ', '.join(sorted({x.license for x in all_components if x.license})), })
def show_project(request, project): obj = get_project(request, project) user = request.user dict_langs = Language.objects.filter( dictionary__project=obj ).annotate(Count('dictionary')) last_changes = Change.objects.prefetch().filter(project=obj)[:10] language_stats = sort_unicode( obj.stats.get_language_stats(), lambda x: force_text(x.language.name) ) # Paginate components of project. all_components = obj.component_set.select_related() components = prefetch_stats(get_paginator( request, all_components )) return render( request, 'project.html', { 'allow_index': True, 'object': obj, 'project': obj, 'dicts': dict_langs, 'last_changes': last_changes, 'last_changes_url': urlencode( {'project': obj.slug} ), 'language_stats': language_stats, 'language_count': Language.objects.filter( translation__component__project=obj ).distinct().count(), 'search_form': SearchForm(), 'whiteboard_form': optional_form( WhiteboardForm, user, 'project.edit', obj ), 'delete_form': optional_form( DeleteForm, user, 'project.edit', obj, obj=obj ), 'rename_form': optional_form( ProjectRenameForm, user, 'project.edit', obj, request=request, instance=obj ), 'replace_form': optional_form(ReplaceForm, user, 'unit.edit', obj), 'bulk_state_form': optional_form( BulkStateForm, user, 'translation.auto', obj, user=user, obj=obj ), 'components': components, 'licenses': ', '.join( sorted({x.license for x in all_components if x.license}) ), } )
def show_project(request, project): obj = get_project(request, project) dict_langs = Language.objects.filter(dictionary__project=obj).annotate( Count('dictionary')) if request.method == 'POST' and can_edit_project(request.user, obj): settings_form = ProjectSettingsForm(request.POST, instance=obj) if settings_form.is_valid(): settings_form.save() messages.success(request, _('Settings saved')) return redirect(obj) else: messages.error( request, _('Invalid settings, please check the form for errors!')) else: settings_form = ProjectSettingsForm(instance=obj) last_changes = Change.objects.for_project(obj)[:10] language_stats = sort_unicode(get_per_language_stats(obj), lambda tup: force_text(tup[0])) language_stats = [(tup[0], translation_percent(tup[1], tup[2]), translation_percent(tup[3], tup[4])) for tup in language_stats] return render( request, 'project.html', { 'object': obj, 'project': obj, 'dicts': dict_langs, 'last_changes': last_changes, 'last_changes_url': urlencode({'project': obj.slug}), 'add_user_form': UserManageForm(), 'settings_form': settings_form, 'language_stats': language_stats, 'unit_count': Unit.objects.filter(translation__subproject__project=obj).count(), 'words_count': obj.get_total_words(), 'language_count': Language.objects.filter( translation__subproject__project=obj).distinct().count(), 'strings_count': obj.get_total(), 'source_words_count': obj.get_source_words(), })
def show_project(request, project): obj = get_project(request, project) dict_langs = Language.objects.filter(dictionary__project=obj).annotate( Count('dictionary')) last_changes = Change.objects.prefetch().filter(project=obj)[:10] language_stats = sort_unicode(obj.stats.get_language_stats(), lambda x: force_text(x.language.name)) # Is user allowed to do automatic translation? if request.user.has_perm('translation.auto', obj): mass_state_form = MassStateForm(request.user, obj) else: mass_state_form = None if request.user.has_perm('unit.edit', obj): replace_form = ReplaceForm() else: replace_form = None # Paginate components of project. all_components = obj.component_set.select_related() components = prefetch_stats(get_paginator(request, all_components)) return render( request, 'project.html', { 'allow_index': True, 'object': obj, 'project': obj, 'dicts': dict_langs, 'last_changes': last_changes, 'last_changes_url': urlencode({'project': obj.slug}), 'language_stats': language_stats, 'language_count': Language.objects.filter( translation__component__project=obj).distinct().count(), 'search_form': SearchForm(), 'replace_form': replace_form, 'mass_state_form': mass_state_form, 'components': components, 'licenses': ', '.join( sorted(set([x.license for x in all_components if x.license]))), })
def as_choices(self): return ( item[:2] for item in sort_unicode( ( (code, f"{_(name)} ({code})", name) for name, code in self.values_list("name", "code") ), lambda tup: tup[2], ) )
def show_project(request, project): obj = get_project(request, project) dict_langs = Language.objects.filter(dictionary__project=obj).annotate( Count('dictionary')) last_changes = Change.objects.for_project(obj)[:10] language_stats = sort_unicode(obj.stats.get_language_stats(), lambda x: force_text(x.language.name)) if can_translate(request.user, project=obj): replace_form = ReplaceForm() mass_state_form = MassStateForm(request.user, obj) else: replace_form = None mass_state_form = None return render( request, 'project.html', { 'allow_index': True, 'object': obj, 'project': obj, 'dicts': dict_langs, 'last_changes': last_changes, 'last_changes_url': urlencode({'project': obj.slug}), 'language_stats': language_stats, 'unit_count': Unit.objects.filter(translation__subproject__project=obj).count(), 'words_count': obj.stats.all_words, 'language_count': Language.objects.filter( translation__subproject__project=obj).distinct().count(), 'strings_count': obj.stats.source_strings, 'source_words_count': obj.stats.source_words, 'search_form': SearchForm(), 'replace_form': replace_form, 'mass_state_form': mass_state_form, 'components': prefetch_stats(obj.subproject_set.select_related()), })
def show_project(request, project): obj = get_project(request, project) user = request.user last_changes = Change.objects.prefetch().order().filter(project=obj)[:10] language_stats = sort_unicode( obj.stats.get_language_stats(), lambda x: force_str(x.language) ) # Paginate components of project. all_components = obj.component_set.prefetch().order() components = prefetch_stats(get_paginator(request, all_components)) return render( request, 'project.html', { 'allow_index': True, 'object': obj, 'project': obj, 'last_changes': last_changes, 'reports_form': ReportsForm(), 'last_changes_url': urlencode({'project': obj.slug}), 'language_stats': language_stats, 'search_form': SearchForm(request.user), 'whiteboard_form': optional_form(WhiteboardForm, user, 'project.edit', obj), 'delete_form': optional_form( DeleteForm, user, 'project.edit', obj, obj=obj ), 'rename_form': optional_form( ProjectRenameForm, user, 'project.edit', obj, request=request, instance=obj, ), 'replace_form': optional_form(ReplaceForm, user, 'unit.edit', obj), 'bulk_state_form': optional_form( BulkEditForm, user, 'translation.auto', obj, user=user, obj=obj, project=obj, auto_id="id_bulk_%s", ), 'components': components, 'licenses': obj.component_set.exclude(license='').order_by('license'), }, )
def show_project(request, project): obj = get_project(request, project) dict_langs = Language.objects.filter( dictionary__project=obj ).annotate(Count('dictionary')) last_changes = Change.objects.for_project(obj)[:10] language_stats = sort_unicode( get_per_language_stats(obj), lambda tup: force_text(tup[0]) ) language_stats = [ ( tup[0], translation_percent(tup[1], tup[2]), translation_percent(tup[3], tup[4]) ) for tup in language_stats ] if can_translate(request.user, project=obj): replace_form = ReplaceForm() else: replace_form = None return render( request, 'project.html', { 'allow_index': True, 'object': obj, 'project': obj, 'dicts': dict_langs, 'last_changes': last_changes, 'last_changes_url': urlencode( {'project': obj.slug} ), 'language_stats': language_stats, 'unit_count': Unit.objects.filter( translation__subproject__project=obj ).count(), 'words_count': obj.get_total_words(), 'language_count': Language.objects.filter( translation__subproject__project=obj ).distinct().count(), 'strings_count': obj.get_total(), 'source_words_count': obj.get_source_words(), 'search_form': SearchForm(), 'replace_form': replace_form, } )
def show_project(request, project): obj = get_project(request, project) dict_langs = Dictionary.objects.filter(project=obj).values_list( 'language', flat=True).distinct() if request.method == 'POST' and can_edit_project(request.user, obj): settings_form = ProjectSettingsForm(request.POST, instance=obj) if settings_form.is_valid(): settings_form.save() messages.success(request, _('Settings saved')) return redirect(obj) else: messages.error( request, _('Invalid settings, please check the form for errors!')) else: settings_form = ProjectSettingsForm(instance=obj) dicts = [] for language in Language.objects.filter(id__in=dict_langs): dicts.append({ 'language': language, 'count': Dictionary.objects.filter(language=language, project=obj).count(), }) last_changes = Change.objects.prefetch().filter( Q(translation__subproject__project=obj) | Q(dictionary__project=obj))[:10] language_stats = sort_unicode(get_per_language_stats(obj), lambda tup: force_text(tup[0])) language_stats = [(tup[0], translation_percent(tup[1], tup[2])) for tup in language_stats] return render( request, 'project.html', { 'object': obj, 'project': obj, 'dicts': dicts, 'last_changes': last_changes, 'last_changes_url': urlencode({'project': obj.slug}), 'add_user_form': UserManageForm(), 'settings_form': settings_form, 'language_stats': language_stats, })
def get_unit_translations(request, unit_id): """Return unit's other translations.""" unit = get_object_or_404(Unit, pk=int(unit_id)) request.user.check_access_component(unit.translation.component) return render( request, "js/translations.html", { "units": sort_unicode( unit.source_unit.unit_set.exclude(pk=unit.pk).prefetch(), lambda unit: str(unit.translation.language), ) }, )
def show_project(request, project): obj = get_project(request, project) dict_langs = Language.objects.filter( dictionary__project=obj ).annotate(Count('dictionary')) last_changes = Change.objects.for_project(obj)[:10] language_stats = sort_unicode( obj.stats.get_language_stats(), lambda x: force_text(x.language.name) ) # Is user allowed to do automatic translation? if request.user.has_perm('translation.auto', obj): mass_state_form = MassStateForm(request.user, obj) else: mass_state_form = None if not request.user.has_perm('unit.edit', obj): replace_form = ReplaceForm() else: replace_form = None return render( request, 'project.html', { 'allow_index': True, 'object': obj, 'project': obj, 'dicts': dict_langs, 'last_changes': last_changes, 'last_changes_url': urlencode( {'project': obj.slug} ), 'language_stats': language_stats, 'language_count': Language.objects.filter( translation__component__project=obj ).distinct().count(), 'search_form': SearchForm(), 'replace_form': replace_form, 'mass_state_form': mass_state_form, 'components': prefetch_stats(obj.component_set.select_related()), } )
def get_unit_translations(request, unit_id): """Return unit's other translations.""" unit = get_object_or_404(Unit, pk=int(unit_id)) user = request.user user.check_access_component(unit.translation.component) return render( request, "js/translations.html", { "units": sort_unicode( unit.source_unit.unit_set.exclude(pk=unit.pk) .prefetch() .prefetch_full(), lambda unit: "{}-{}".format( user.profile.get_translation_order(unit.translation), unit.translation.language, ), ), "component": unit.translation.component, }, )
def optgroups(self, name, value, attrs=None): groups = super(SortedSelectMixin, self).optgroups(name, value, attrs) return sort_unicode(groups, lambda val: force_text(val[1][0]["label"]))
def show_project(request, project): obj = get_project(request, project) dict_langs = Dictionary.objects.filter( project=obj ).values_list( 'language', flat=True ).distinct() if request.method == 'POST' and can_edit_project(request.user, obj): settings_form = ProjectSettingsForm(request.POST, instance=obj) if settings_form.is_valid(): settings_form.save() messages.success(request, _('Settings saved')) return redirect(obj) else: messages.error( request, _('Invalid settings, please check the form for errors!') ) else: settings_form = ProjectSettingsForm(instance=obj) dicts = [] for language in Language.objects.filter(id__in=dict_langs): dicts.append( { 'language': language, 'count': Dictionary.objects.filter( language=language, project=obj ).count(), } ) last_changes = Change.objects.for_project(obj)[:10] language_stats = sort_unicode( get_per_language_stats(obj), lambda tup: force_text(tup[0]) ) language_stats = [ ( tup[0], translation_percent(tup[1], tup[2]), translation_percent(tup[3], tup[4]) ) for tup in language_stats ] return render( request, 'project.html', { 'object': obj, 'project': obj, 'dicts': dicts, 'last_changes': last_changes, 'last_changes_url': urlencode( {'project': obj.slug} ), 'add_user_form': UserManageForm(), 'settings_form': settings_form, 'language_stats': language_stats, } )
def render(self, response): translations = [] offset = 20 color = self.COLOR_MAP[self.color] language_width = 190 languages = self.obj.stats.get_language_stats() for stats in sort_unicode(languages, lambda x: str(x.language)): # Skip empty translations if stats.translated == 0: continue language = stats.language percent = stats.translated_percent if self.color == "auto": color = get_percent_color(percent) language_name = str(language) language_width = max( language_width, (render_size("DejaVu Sans", Pango.Weight.NORMAL, 11, 0, language_name)[0].width + 10), ) translations.append(( # Language name language_name, # Translation percent int(percent), # Text y offset offset, # Bar y offset offset - 6, # Bar width int(percent * 1.5), # Bar color color, # Row URL get_site_url( reverse( "project-language", kwargs={ "lang": language.code, "project": self.obj.slug }, )), # Top offset for horizontal 10 + int((100 - percent) * 1.5), )) offset += 15 response.write( render_to_string( self.template_name, { "height": len(translations) * 15 + 15, "width": language_width + 210, "language_offset": language_width, "bar_offset": language_width + 10, "text_offset": language_width + 170, "translations": translations, "site_url": get_site_url(), "horizontal_height": language_width + 130, "fonts_cdn_url": settings.FONTS_CDN_URL, }, ))
def show_project(request, project): obj = get_project(request, project) obj.stats.ensure_basic() user = request.user last_changes = obj.change_set.prefetch().order()[:10] last_announcements = (obj.change_set.prefetch().order().filter( action=Change.ACTION_ANNOUNCEMENT)[:10]) all_components = prefetch_stats( obj.child_components.filter_access(user).prefetch().order()) all_components = get_paginator(request, all_components) for component in all_components: component.is_shared = None if component.project == obj else component.project language_stats = obj.stats.get_language_stats() # Show ghost translations for user languages component = None for component in all_components: if component.can_add_new_language(user, fast=True): break if component: add_ghost_translations( component, user, language_stats, GhostProjectLanguageStats, is_shared=component.is_shared, ) language_stats = sort_unicode( language_stats, lambda x: f"{user.profile.get_translation_order(x)}-{x.language}", ) components = prefetch_tasks(all_components) return render( request, "project.html", { "allow_index": True, "object": obj, "project": obj, "last_changes": last_changes, "last_announcements": last_announcements, "reports_form": ReportsForm(), "last_changes_url": urlencode({"project": obj.slug}), "language_stats": [stat.obj or stat for stat in language_stats], "search_form": SearchForm(request.user), "announcement_form": optional_form(AnnouncementForm, user, "project.edit", obj), "delete_form": optional_form( ProjectDeleteForm, user, "project.edit", obj, obj=obj), "rename_form": optional_form( ProjectRenameForm, user, "project.edit", obj, request=request, instance=obj, ), "replace_form": optional_form(ReplaceForm, user, "unit.edit", obj), "bulk_state_form": optional_form( BulkEditForm, user, "translation.auto", obj, user=user, obj=obj, project=obj, auto_id="id_bulk_%s", ), "components": components, "licenses": sorted( (component for component in all_components if component.license), key=lambda component: component.license, ), }, )
def as_choices(self): return (item[:2] for item in sort_unicode( ((code, "{} ({})".format(_(name), code), name) for name, code in self.values_list("name", "code")), lambda tup: tup[2], ))
def order_translated(self): return sort_unicode(self, lambda x: x.name)
def show_project(request, project): obj = get_project(request, project) obj.stats.ensure_basic() user = request.user last_changes = Change.objects.prefetch().order().filter(project=obj)[:10] last_announcements = (Change.objects.prefetch().order().filter( project=obj, action=Change.ACTION_ANNOUNCEMENT)[:10]) language_stats = obj.stats.get_language_stats() # Show ghost translations for user languages component = None for component in obj.component_set.filter_access(user).all(): if component.can_add_new_language(user): break if component: add_ghost_translations(component, user, language_stats, GhostProjectLanguageStats) language_stats = sort_unicode( language_stats, lambda x: "{}-{}".format(user.profile.get_language_order(x.language), x .language), ) all_components = obj.component_set.filter_access(user).prefetch().order() components = prefetch_tasks(prefetch_stats(all_components)) return render( request, "project.html", { "allow_index": True, "object": obj, "project": obj, "last_changes": last_changes, "last_announcements": last_announcements, "reports_form": ReportsForm(), "last_changes_url": urlencode({"project": obj.slug}), "language_stats": [stat.obj or stat for stat in language_stats], "search_form": SearchForm(request.user), "announcement_form": optional_form(AnnouncementForm, user, "project.edit", obj), "delete_form": optional_form( ProjectDeleteForm, user, "project.edit", obj, obj=obj), "rename_form": optional_form( ProjectRenameForm, user, "project.edit", obj, request=request, instance=obj, ), "replace_form": optional_form(ReplaceForm, user, "unit.edit", obj), "bulk_state_form": optional_form( BulkEditForm, user, "translation.auto", obj, user=user, obj=obj, project=obj, auto_id="id_bulk_%s", ), "components": components, "licenses": obj.component_set.exclude(license="").order_by("license"), }, )
def show_component(request, project, component): obj = get_component(request, project, component) obj.stats.ensure_basic() user = request.user last_changes = Change.objects.prefetch().order().filter(component=obj)[:10] translations = prefetch_stats(list(obj.translation_set.prefetch())) # Show ghost translations for user languages add_ghost_translations(obj, user, translations, GhostTranslation) translations = sort_unicode( translations, lambda x: "{}-{}".format(user.profile.get_language_order(x.language), x .language), ) return render( request, "component.html", { "allow_index": True, "object": obj, "project": obj.project, "translations": translations, "reports_form": ReportsForm(), "last_changes": last_changes, "last_changes_url": urlencode({ "component": obj.slug, "project": obj.project.slug }), "replace_form": optional_form(ReplaceForm, user, "unit.edit", obj), "bulk_state_form": optional_form( BulkEditForm, user, "translation.auto", obj, user=user, obj=obj, project=obj.project, auto_id="id_bulk_%s", ), "announcement_form": optional_form(AnnouncementForm, user, "component.edit", obj), "delete_form": optional_form( ComponentDeleteForm, user, "component.edit", obj, obj=obj), "rename_form": optional_form( ComponentRenameForm, user, "component.edit", obj, request=request, instance=obj, ), "move_form": optional_form( ComponentMoveForm, user, "component.edit", obj, request=request, instance=obj, ), "search_form": SearchForm(request.user), "alerts": obj.all_alerts, }, )
def show_project(request, project): obj = get_project(request, project) user = request.user last_changes = Change.objects.prefetch().order().filter(project=obj)[:10] language_stats = sort_unicode(obj.stats.get_language_stats(), lambda x: force_str(x.language)) # Paginate components of project. all_components = obj.component_set.prefetch().order() components = prefetch_stats(get_paginator(request, all_components)) return render( request, "project.html", { "allow_index": True, "object": obj, "project": obj, "last_changes": last_changes, "reports_form": ReportsForm(), "last_changes_url": urlencode({"project": obj.slug}), "language_stats": language_stats, "search_form": SearchForm(request.user), "announcement_form": optional_form(AnnouncementForm, user, "project.edit", obj), "delete_form": optional_form(DeleteForm, user, "project.edit", obj, obj=obj), "rename_form": optional_form( ProjectRenameForm, user, "project.edit", obj, request=request, instance=obj, ), "replace_form": optional_form(ReplaceForm, user, "unit.edit", obj), "bulk_state_form": optional_form( BulkEditForm, user, "translation.auto", obj, user=user, obj=obj, project=obj, auto_id="id_bulk_%s", ), "components": components, "licenses": obj.component_set.exclude(license="").order_by("license"), }, )
def optgroups(self, name, value, attrs=None): groups = super().optgroups(name, value, attrs) return sort_unicode(groups, lambda val: force_str(val[1][0]["label"]))
def show_project(request, project): obj = get_project(request, project) dict_langs = Language.objects.filter( dictionary__project=obj ).annotate(Count('dictionary')) if request.method == 'POST' and can_edit_project(request.user, obj): settings_form = ProjectSettingsForm(request.POST, instance=obj) if settings_form.is_valid(): settings_form.save() messages.success(request, _('Settings saved')) return redirect(obj) else: messages.error( request, _('Invalid settings, please check the form for errors!') ) else: settings_form = ProjectSettingsForm(instance=obj) last_changes = Change.objects.for_project(obj)[:10] language_stats = sort_unicode( get_per_language_stats(obj), lambda tup: force_text(tup[0]) ) language_stats = [ ( tup[0], translation_percent(tup[1], tup[2]), translation_percent(tup[3], tup[4]) ) for tup in language_stats ] if can_translate(request.user, project=obj): replace_form = ReplaceForm() else: replace_form = None return render( request, 'project.html', { 'allow_index': True, 'object': obj, 'project': obj, 'dicts': dict_langs, 'last_changes': last_changes, 'last_changes_url': urlencode( {'project': obj.slug} ), 'settings_form': settings_form, 'language_stats': language_stats, 'unit_count': Unit.objects.filter( translation__subproject__project=obj ).count(), 'words_count': obj.get_total_words(), 'language_count': Language.objects.filter( translation__subproject__project=obj ).distinct().count(), 'strings_count': obj.get_total(), 'source_words_count': obj.get_source_words(), 'search_form': SearchForm(), 'replace_form': replace_form, } )