def render_search(context): request = context['request'] return { 'search_form': make_search_form(request=request), 'search_action': request.resource_obj.get_translate_url(), }
def _test_translate_view(tp, request, response, kwargs, settings): ctx = response.context kwargs["project_code"] = tp.project.code kwargs["language_code"] = tp.language.code resource_path = "%(dir_path)s%(filename)s" % kwargs request_path = "%s%s" % (tp.pootle_path, resource_path) vfolder, pootle_path = extract_vfolder_from_path(request_path) current_vfolder_pk = (vfolder.pk if vfolder else "") display_priority = (not current_vfolder_pk and not kwargs['filename'] and ctx['object'].has_vfolders) assertions = dict(page="translate", translation_project=tp, language=tp.language, project=tp.project, is_admin=check_permission('administrate', request), ctx_path=tp.pootle_path, pootle_path=request_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), editor_extends="translation_projects/base.html", check_categories=get_qualitycheck_schema(), previous_url=get_previous_url(request), current_vfolder_pk=current_vfolder_pk, display_priority=display_priority, cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL) view_context_test(ctx, **assertions)
def _test_translate_view(project, request, response, kwargs, settings): ctx = response.context kwargs["project_code"] = project.code ctx_path = ("/projects/%(project_code)s/" % kwargs) resource_path = ("%(dir_path)s%(filename)s" % kwargs) pootle_path = "%s%s" % (ctx_path, resource_path) view_context_test( ctx, **dict(page="translate", is_admin=False, language=None, project=project, pootle_path=pootle_path, ctx_path=ctx_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), editor_extends="projects/base.html", check_categories=get_qualitycheck_schema(), previous_url=get_previous_url(request), display_priority=(VirtualFolderTreeItem.objects.filter( pootle_path__startswith=pootle_path).exists()), cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), current_vfolder_pk="", POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL))
def get_translation_context(request, is_terminology=False): """Return a common context for translation views. :param request: a :cls:`django.http.HttpRequest` object. :param is_terminology: boolean indicating if the translation context is relevant to a terminology project. """ return { 'cantranslate': check_permission("translate", request), 'cansuggest': check_permission("suggest", request), 'canreview': check_permission("review", request), 'is_admin': check_permission('administrate', request), 'profile': request.profile, 'pootle_path': request.pootle_path, 'ctx_path': request.ctx_path, 'resource_path': (request.resource_path if hasattr(request, 'resource_path') else ''), 'check_categories': get_qualitycheck_schema(), 'search_form': make_search_form(request=request, terminology=is_terminology), 'MT_BACKENDS': settings.MT_BACKENDS, 'LOOKUP_BACKENDS': settings.LOOKUP_BACKENDS, 'AMAGAMA_URL': settings.AMAGAMA_URL, }
def test_view_projects_translate(client, settings, request_users): user = request_users["user"] client.login( username=user.username, password=request_users["password"]) response = client.get(reverse("pootle-projects-translate")) if not user.is_superuser: assert response.status_code == 403 return ctx = response.context request = response.wsgi_request assertions = dict( page="translate", has_admin_access=user.is_superuser, language=None, project=None, pootle_path="/projects/", ctx_path="/projects/", resource_path="", resource_path_parts=[], editor_extends="projects/all/base.html", check_categories=get_qualitycheck_schema(), previous_url=get_previous_url(request), display_priority=False, cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), current_vfolder_pk="", POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL) view_context_test(ctx, **assertions)
def test_view_projects_translate(site_permissions, site_matrix_with_vfolders, client, nobody, default, settings): response = client.get(reverse("pootle-projects-translate")) ctx = response.context request = response.wsgi_request assertions = dict( page="translate", is_admin=False, language=None, project=None, profile=request.profile, pootle_path="/projects/", ctx_path="/projects/", resource_path="", resource_path_parts=[], editor_extends="projects/all/base.html", check_categories=get_qualitycheck_schema(), previous_url=get_previous_url(request), display_priority=False, cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), current_vfolder_pk="", POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL, ) view_context_test(ctx, **assertions)
def _test_translate_view(language, request, response, kwargs, settings): ctx = response.context view_context_test( ctx, **dict( project=None, language=language, page="translate", ctx_path=language.directory.pootle_path, pootle_path=language.directory.pootle_path, resource_path="", resource_path_parts=[], profile=request.profile, editor_extends="languages/base.html", check_categories=get_qualitycheck_schema(), previous_url=get_previous_url(request), display_priority=False, is_admin=check_permission('administrate', request), cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), current_vfolder_pk="", POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL))
def get_translation_context(request): """Returns a common context for translation views. :param request: a :cls:`django.http.HttpRequest` object. """ resource_path = getattr(request, 'resource_path', '') vfolder_pk = getattr(request, 'current_vfolder', '') return { 'page': 'translate', 'cantranslate': check_permission("translate", request), 'cansuggest': check_permission("suggest", request), 'canreview': check_permission("review", request), 'is_admin': check_permission('administrate', request), 'profile': request.profile, 'pootle_path': request.pootle_path, 'ctx_path': request.ctx_path, 'current_vfolder_pk': vfolder_pk, 'display_priority': display_vfolder_priority(request), 'resource_path': resource_path, 'resource_path_parts': get_path_parts(resource_path), 'check_categories': get_qualitycheck_schema(), 'search_form': make_search_form(request=request), 'previous_url': get_previous_url(request), 'POOTLE_MT_BACKENDS': settings.POOTLE_MT_BACKENDS, 'AMAGAMA_URL': settings.AMAGAMA_URL, }
def _test_translate_view(tp, request, response, kwargs, settings): ctx = response.context kwargs["project_code"] = tp.project.code kwargs["language_code"] = tp.language.code resource_path = "%(dir_path)s%(filename)s" % kwargs request_path = "%s%s" % (tp.pootle_path, resource_path) vfolder, pootle_path = extract_vfolder_from_path(request_path) current_vfolder_pk = vfolder.pk if vfolder else "" display_priority = not current_vfolder_pk and ( VirtualFolderTreeItem.objects.filter(pootle_path__startswith=pootle_path).exists() ) assertions = dict( page="translate", translation_project=tp, language=tp.language, project=tp.project, is_admin=False, profile=request.profile, ctx_path=tp.pootle_path, pootle_path=request_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), editor_extends="translation_projects/base.html", check_categories=get_qualitycheck_schema(), previous_url=get_previous_url(request), current_vfolder_pk=current_vfolder_pk, display_priority=display_priority, cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL, ) view_context_test(ctx, **assertions)
def _test_translate_view(project, request, response, kwargs, settings): if not request.user.is_superuser: assert response.status_code == 403 return ctx = response.context kwargs["project_code"] = project.code ctx_path = ("/projects/%(project_code)s/" % kwargs) resource_path = ("%(dir_path)s%(filename)s" % kwargs) pootle_path = "%s%s" % (ctx_path, resource_path) display_priority = False view_context_test( ctx, **dict(page="translate", has_admin_access=request.user.is_superuser, language=None, project=project, pootle_path=pootle_path, ctx_path=ctx_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), editor_extends="projects/base.html", check_categories=get_qualitycheck_schema(), previous_url=get_previous_url(request), display_priority=display_priority, cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), current_vfolder_pk="", POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL))
def get_translation_context(request): """Returns a common context for translation views. :param request: a :cls:`django.http.HttpRequest` object. """ resource_path = getattr(request, 'resource_path', '') vfolder_pk = getattr(request, 'current_vfolder', '') display_priority = False if not vfolder_pk: display_priority = VirtualFolder.objects.filter( units__store__pootle_path__startswith=request.pootle_path).exists( ) return { 'page': 'translate', 'cantranslate': check_permission("translate", request), 'cansuggest': check_permission("suggest", request), 'canreview': check_permission("review", request), 'is_admin': check_permission('administrate', request), 'profile': request.profile, 'pootle_path': request.pootle_path, 'ctx_path': request.ctx_path, 'current_vfolder_pk': vfolder_pk, 'display_priority': display_priority, 'resource_path': resource_path, 'resource_path_parts': get_path_parts(resource_path), 'check_categories': get_qualitycheck_schema(), 'search_form': make_search_form(request=request), 'previous_url': get_previous_url(request), 'POOTLE_MT_BACKENDS': settings.POOTLE_MT_BACKENDS, 'AMAGAMA_URL': settings.AMAGAMA_URL, }
def test_view_projects_translate(client, settings, request_users): user = request_users["user"] client.login(username=user.username, password=request_users["password"]) response = client.get(reverse("pootle-projects-translate")) if not user.is_superuser: assert response.status_code == 403 return ctx = response.context request = response.wsgi_request assertions = dict(page="translate", is_admin=False, language=None, project=None, pootle_path="/projects/", ctx_path="/projects/", resource_path="", resource_path_parts=[], editor_extends="projects/all/base.html", check_categories=get_qualitycheck_schema(), previous_url=get_previous_url(request), display_priority=False, cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), current_vfolder_pk="", POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL) view_context_test(ctx, **assertions)
def _test_vf_translate_view(tp, request, response, kwargs, settings): from .tp import view_context_test ctx = response.context obj = ctx["object"] kwargs["project_code"] = tp.project.code kwargs["language_code"] = tp.language.code resource_path = "%(dir_path)s%(filename)s" % kwargs request_path = "%s%s" % (tp.pootle_path, resource_path) checks = get_qualitychecks() schema = {sc["code"]: sc for sc in get_qualitycheck_schema()} vfolder_pk = response.context["current_vfolder_pk"] check_data = DirectoryVFDataTool(obj).get_checks(user=request.user).get( vfolder_pk, {}) _checks = {} for check, checkid in checks.items(): if check not in check_data: continue _checkid = schema[checkid]["name"] _checks[_checkid] = _checks.get( _checkid, dict(checks=[], title=schema[checkid]["title"])) _checks[_checkid]["checks"].append( dict(code=check, title=CHECK_NAMES[check], count=check_data[check])) _checks = OrderedDict( (k, _checks[k]) for k in CATEGORY_IDS.keys() if _checks.get(k)) vfolder = VirtualFolder.objects.get( name=request.resolver_match.kwargs["vfolder_name"]) current_vfolder_pk = vfolder.pk display_priority = False unit_api_root = reverse("vfolder-pootle-xhr-units", kwargs=dict(vfolder_name=vfolder.name)) resource_path = ("/".join([ "++vfolder", vfolder.name, ctx['object'].pootle_path.replace(tp.pootle_path, "") ])) assertions = dict(page="translate", translation_project=tp, language=tp.language, project=tp.project, has_admin_access=check_permission( 'administrate', request), ctx_path=tp.pootle_path, pootle_path=request_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), editor_extends="translation_projects/base.html", checks=_checks, previous_url=get_previous_url(request), current_vfolder_pk=current_vfolder_pk, display_priority=display_priority, cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), unit_api_root=unit_api_root, POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL) view_context_test(ctx, **assertions)
def _test_translate_view(project, request, response, kwargs, settings): ctx = response.context user = request.profile kwargs["project_code"] = project.code ctx_path = "/projects/%(project_code)s/" % kwargs resource_path = "%(dir_path)s%(filename)s" % kwargs pootle_path = "%s%s" % (ctx_path, resource_path) view_context_test( ctx, **dict( page="translate", is_admin=False, language=None, project=project, profile=user, pootle_path="%s%s" % (ctx_path, resource_path), ctx_path=ctx_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), editor_extends="projects/base.html", check_categories=get_qualitycheck_schema(), previous_url=get_previous_url(request), display_priority=(VirtualFolderTreeItem.objects.filter(pootle_path__startswith=pootle_path).exists()), cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), current_vfolder_pk="", POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL, ) )
def get_step_query(request, units_queryset): """Narrows down unit query to units matching conditions in GET and POST.""" if 'unitstates' in request.GET: unitstates = request.GET['unitstates'].split(',') if unitstates: state_queryset = units_queryset.none() for unitstate in unitstates: if unitstate == 'untranslated': state_queryset = state_queryset | \ units_queryset.filter(state=UNTRANSLATED) elif unitstate == 'translated': state_queryset = state_queryset | \ units_queryset.filter(state=TRANSLATED) elif unitstate == 'fuzzy': state_queryset = state_queryset | \ units_queryset.filter(state=FUZZY) units_queryset = state_queryset if 'checks' in request.GET: checks = request.GET['checks'].split(',') if checks: checks_queryset = units_queryset.filter( qualitycheck__false_positive=False, qualitycheck__name__in=checks ) units_queryset = checks_queryset if 'matchnames' in request.GET: matchnames = request.GET['matchnames'].split(',') if matchnames: match_queryset = units_queryset.none() if 'hassuggestion' in matchnames: #FIXME: is None the most efficient query match_queryset = units_queryset.exclude(suggestion=None) matchnames.remove('hassuggestion') elif 'ownsuggestion' in matchnames: match_queryset = units_queryset.filter( suggestion__user=request.profile ).distinct() matchnames.remove('ownsuggestion') units_queryset = match_queryset if 'search' in request.GET and 'sfields' in request.GET: # use the search form for validation only search_form = make_search_form(request.GET) if search_form.is_valid(): units_queryset = get_search_step_query(request.translation_project, search_form, units_queryset) return units_queryset
def test_view_projects_translate(client, settings, request_users): user = request_users["user"] client.login( username=user.username, password=request_users["password"]) response = client.get(reverse("pootle-projects-translate")) if not user.is_superuser: assert response.status_code == 403 return ctx = response.context request = response.wsgi_request user_projects = Project.accessible_by_user(request.user) user_projects = ( Project.objects.for_user(request.user) .filter(code__in=user_projects)) obj = ProjectSet(user_projects) checks = get_qualitychecks() schema = {sc["code"]: sc for sc in get_qualitycheck_schema()} check_data = obj.data_tool.get_checks() _checks = {} for check, checkid in checks.items(): if check not in check_data: continue _checkid = schema[checkid]["name"] _checks[_checkid] = _checks.get( _checkid, dict(checks=[], title=schema[checkid]["title"])) _checks[_checkid]["checks"].append( dict( code=check, title=check_names[check], count=check_data[check])) _checks = OrderedDict( (k, _checks[k]) for k in CATEGORY_IDS.keys() if _checks.get(k)) assertions = dict( page="translate", has_admin_access=user.is_superuser, language=None, project=None, pootle_path="/projects/", ctx_path="/projects/", resource_path="", resource_path_parts=[], editor_extends="projects/all/base.html", checks=_checks, previous_url=get_previous_url(request), display_priority=False, cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), current_vfolder_pk="", POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL) view_context_test(ctx, **assertions)
def _test_translate_view(tp, request, response, kwargs, settings): ctx = response.context obj = ctx["object"] kwargs["project_code"] = tp.project.code kwargs["language_code"] = tp.language.code resource_path = "%(dir_path)s%(filename)s" % kwargs request_path = "%s%s" % (tp.pootle_path, resource_path) checks = get_qualitychecks() schema = {sc["code"]: sc for sc in get_qualitycheck_schema()} check_data = obj.data_tool.get_checks() _checks = {} for check, checkid in checks.items(): if check not in check_data: continue _checkid = schema[checkid]["name"] _checks[_checkid] = _checks.get( _checkid, dict(checks=[], title=schema[checkid]["title"])) _checks[_checkid]["checks"].append( dict( code=check, title=CHECK_NAMES[check], count=check_data[check])) _checks = OrderedDict( (k, _checks[k]) for k in CATEGORY_IDS.keys() if _checks.get(k)) current_vfolder_pk = "" display_priority = False if not kwargs["filename"]: vf_view = vfolders_data_view.get(obj.__class__)(obj, request.user) display_priority = vf_view.has_data unit_api_root = "/xhr/units/" assertions = dict( page="translate", translation_project=tp, language=tp.language, project=tp.project, has_admin_access=check_permission('administrate', request), ctx_path=tp.pootle_path, pootle_path=request_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), editor_extends="translation_projects/base.html", checks=_checks, previous_url=get_previous_url(request), current_vfolder_pk=current_vfolder_pk, display_priority=display_priority, cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), unit_api_root=unit_api_root, POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL) view_context_test(ctx, **assertions)
def _test_translate_view(project, request, response, kwargs, settings): if not request.user.is_superuser: assert response.status_code == 403 return ctx = response.context kwargs["project_code"] = project.code ctx_path = ( "/projects/%(project_code)s/" % kwargs) resource_path = ( "%(dir_path)s%(filename)s" % kwargs) pootle_path = "%s%s" % (ctx_path, resource_path) display_priority = False checks = get_qualitychecks() schema = {sc["code"]: sc for sc in get_qualitycheck_schema()} check_data = ctx["object"].data_tool.get_checks() _checks = {} for check, checkid in checks.items(): if check not in check_data: continue _checkid = schema[checkid]["name"] _checks[_checkid] = _checks.get( _checkid, dict(checks=[], title=schema[checkid]["title"])) _checks[_checkid]["checks"].append( dict( code=check, title=check_names[check], count=check_data[check])) _checks = OrderedDict( (k, _checks[k]) for k in CATEGORY_IDS.keys() if _checks.get(k)) view_context_test( ctx, **dict( page="translate", has_admin_access=request.user.is_superuser, language=None, project=project, pootle_path=pootle_path, ctx_path=ctx_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), editor_extends="projects/base.html", checks=_checks, previous_url=get_previous_url(request), display_priority=display_priority, cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), current_vfolder_pk="", POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL))
def _test_translate_view(tp, request, response, kwargs, settings): ctx = response.context obj = ctx["object"] kwargs["project_code"] = tp.project.code kwargs["language_code"] = tp.language.code resource_path = "%(dir_path)s%(filename)s" % kwargs request_path = "%s%s" % (tp.pootle_path, resource_path) checks = get_qualitychecks() schema = {sc["code"]: sc for sc in get_qualitycheck_schema()} check_data = obj.data_tool.get_checks() _checks = {} for check, checkid in checks.items(): if check not in check_data: continue _checkid = schema[checkid]["name"] _checks[_checkid] = _checks.get( _checkid, dict(checks=[], title=schema[checkid]["title"])) _checks[_checkid]["checks"].append( dict(code=check, title=check_names[check], count=check_data[check])) _checks = OrderedDict( (k, _checks[k]) for k in CATEGORY_IDS.keys() if _checks.get(k)) current_vfolder_pk = "" display_priority = False if not kwargs["filename"]: vf_view = vfolders_data_view.get(obj.__class__)(obj, request.user) display_priority = vf_view.has_data unit_api_root = "/xhr/units/" assertions = dict(page="translate", translation_project=tp, language=tp.language, project=tp.project, has_admin_access=check_permission( 'administrate', request), ctx_path=tp.pootle_path, pootle_path=request_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), editor_extends="translation_projects/base.html", checks=_checks, previous_url=get_previous_url(request), current_vfolder_pk=current_vfolder_pk, display_priority=display_priority, cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), unit_api_root=unit_api_root, POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL) view_context_test(ctx, **assertions)
def _test_translate_view(tp, request, response, kwargs, settings): ctx = response.context obj = ctx["object"] kwargs["project_code"] = tp.project.code kwargs["language_code"] = tp.language.code resource_path = "%(dir_path)s%(filename)s" % kwargs request_path = "%s%s" % (tp.pootle_path, resource_path) if request.path.startswith("/++vfolder"): vfolder = VirtualFolder.objects.get( name=request.resolver_match.kwargs["vfolder_name"]) current_vfolder_pk = vfolder.pk display_priority = False unit_api_root = reverse( "vfolder-pootle-xhr-units", kwargs=dict(vfolder_name=vfolder.name)) resource_path = ( "/".join( ["++vfolder", vfolder.name, ctx['object'].pootle_path.replace(tp.pootle_path, "")])) else: vfolder = None current_vfolder_pk = "" display_priority = False if not kwargs["filename"]: vf_view = vfolders_data_view.get(obj.__class__)(obj, request.user) display_priority = vf_view.has_data unit_api_root = "/xhr/units/" assertions = dict( page="translate", translation_project=tp, language=tp.language, project=tp.project, has_admin_access=check_permission('administrate', request), ctx_path=tp.pootle_path, pootle_path=request_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), editor_extends="translation_projects/base.html", check_categories=get_qualitycheck_schema(), previous_url=get_previous_url(request), current_vfolder_pk=current_vfolder_pk, display_priority=display_priority, cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), unit_api_root=unit_api_root, POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL) view_context_test(ctx, **assertions)
def render_search(context): search_form = make_search_form(request=context['request']) if context["page"] != "translate" and not context["can_translate_stats"]: search_form.fields["search"].widget.attrs["readonly"] = "readonly" search_form.fields["search"].widget.attrs["disabled"] = True search_form.fields["search"].widget.attrs["title"] = "" search_form.fields["search"].widget.attrs[ "placeholder"] = _("Search unavailable") search_form.fields[ "search"].widget.attrs["class"] = "search-widget-disabled" return { 'search_form': search_form, 'search_action': context["object"].get_translate_url()}
def get_step_query(request, units_queryset): """Narrows down unit query to units matching conditions in GET and POST.""" if 'unitstates' in request.GET: unitstates = request.GET['unitstates'].split(',') if unitstates: state_queryset = units_queryset.none() for unitstate in unitstates: if unitstate == 'untranslated': state_queryset = state_queryset | units_queryset.filter( state=UNTRANSLATED) elif unitstate == 'translated': state_queryset = state_queryset | units_queryset.filter( state=TRANSLATED) elif unitstate == 'fuzzy': state_queryset = state_queryset | units_queryset.filter( state=FUZZY) units_queryset = state_queryset if 'checks' in request.GET: checks = request.GET['checks'].split(',') if checks: checks_queryset = units_queryset.filter( qualitycheck__false_positive=False, qualitycheck__name__in=checks) units_queryset = checks_queryset if 'matchnames' in request.GET: matchnames = request.GET['matchnames'].split(',') if matchnames: match_queryset = units_queryset.none() if 'hassuggestion' in matchnames: #FIXME: is None the most efficient query match_queryset = units_queryset.exclude(suggestion=None) matchnames.remove('hassuggestion') elif 'ownsuggestion' in matchnames: match_queryset = units_queryset.filter( suggestion__user=request.profile).distinct() matchnames.remove('ownsuggestion') units_queryset = match_queryset if 'search' in request.GET and 'sfields' in request.GET: # use the search form for validation only search_form = make_search_form(request.GET) if search_form.is_valid(): units_queryset = get_search_step_query(request.translation_project, search_form, units_queryset) return units_queryset
def translate_page(request): cantranslate = check_permission("translate", request) cansuggest = check_permission("suggest", request) canreview = check_permission("review", request) translation_project = request.translation_project language = translation_project.language project = translation_project.project profile = request.profile store = getattr(request, "store", None) directory = getattr(request, "directory", None) is_single_file = store and True or False path = is_single_file and store.path or directory.path pootle_path = (is_single_file and store.pootle_path or directory.pootle_path) is_terminology = (project.is_terminology or store and store.is_terminology) search_form = make_search_form(request=request, terminology=is_terminology) previous_overview_url = previous_view_url(request, ['overview']) context = { 'cantranslate': cantranslate, 'cansuggest': cansuggest, 'canreview': canreview, 'search_form': search_form, 'store': store, 'store_id': store and store.id, 'directory': directory, 'directory_id': directory and directory.id, 'path': path, 'pootle_path': pootle_path, 'is_single_file': is_single_file, 'language': language, 'project': project, 'translation_project': translation_project, 'profile': profile, 'source_language': translation_project.project.source_language, 'previous_overview_url': previous_overview_url, 'MT_BACKENDS': settings.MT_BACKENDS, 'LOOKUP_BACKENDS': settings.LOOKUP_BACKENDS, 'AMAGAMA_URL': settings.AMAGAMA_URL, } return render_to_response('store/translate.html', context, context_instance=RequestContext(request))
def _test_translate_view(project, request, response, kwargs, settings): if not request.user.is_superuser: assert response.status_code == 403 return ctx = response.context kwargs["project_code"] = project.code ctx_path = ("/projects/%(project_code)s/" % kwargs) resource_path = ("%(dir_path)s%(filename)s" % kwargs) pootle_path = "%s%s" % (ctx_path, resource_path) display_priority = False checks = get_qualitychecks() schema = {sc["code"]: sc for sc in get_qualitycheck_schema()} check_data = ctx["object"].data_tool.get_checks() _checks = {} for check, checkid in checks.items(): if check not in check_data: continue _checkid = schema[checkid]["name"] _checks[_checkid] = _checks.get( _checkid, dict(checks=[], title=schema[checkid]["title"])) _checks[_checkid]["checks"].append( dict(code=check, title=check_names[check], count=check_data[check])) _checks = OrderedDict( (k, _checks[k]) for k in CATEGORY_IDS.keys() if _checks.get(k)) view_context_test( ctx, **dict(page="translate", has_admin_access=request.user.is_superuser, language=None, project=project, pootle_path=pootle_path, ctx_path=ctx_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), editor_extends="projects/base.html", checks=_checks, previous_url=get_previous_url(request), display_priority=display_priority, cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), current_vfolder_pk="", POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL))
def _test_translate_view(tp, request, response, kwargs, settings): ctx = response.context obj = ctx["object"] kwargs["project_code"] = tp.project.code kwargs["language_code"] = tp.language.code resource_path = "%(dir_path)s%(filename)s" % kwargs request_path = "%s%s" % (tp.pootle_path, resource_path) if request.path.startswith("/++vfolder"): vfolder = VirtualFolder.objects.get( name=request.resolver_match.kwargs["vfolder_name"]) current_vfolder_pk = vfolder.pk display_priority = False unit_api_root = reverse("vfolder-pootle-xhr-units", kwargs=dict(vfolder_name=vfolder.name)) resource_path = ("/".join([ "++vfolder", vfolder.name, ctx['object'].pootle_path.replace(tp.pootle_path, "") ])) else: vfolder = None current_vfolder_pk = "" display_priority = False if not kwargs["filename"]: vf_view = vfolders_data_view.get(obj.__class__)(obj, request.user) display_priority = vf_view.has_data unit_api_root = "/xhr/units/" assertions = dict(page="translate", translation_project=tp, language=tp.language, project=tp.project, has_admin_access=check_permission( 'administrate', request), ctx_path=tp.pootle_path, pootle_path=request_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), editor_extends="translation_projects/base.html", check_categories=get_qualitycheck_schema(), previous_url=get_previous_url(request), current_vfolder_pk=current_vfolder_pk, display_priority=display_priority, cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), unit_api_root=unit_api_root, POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL) view_context_test(ctx, **assertions)
def test_view_projects_translate(client, settings, request_users): user = request_users["user"] client.login(username=user.username, password=request_users["password"]) response = client.get(reverse("pootle-projects-translate")) if not user.is_superuser: assert response.status_code == 403 return ctx = response.context request = response.wsgi_request user_projects = Project.accessible_by_user(request.user) user_projects = (Project.objects.for_user( request.user).filter(code__in=user_projects)) obj = ProjectSet(user_projects) checks = get_qualitychecks() schema = {sc["code"]: sc for sc in get_qualitycheck_schema()} check_data = obj.data_tool.get_checks() _checks = {} for check, checkid in checks.items(): if check not in check_data: continue _checkid = schema[checkid]["name"] _checks[_checkid] = _checks.get( _checkid, dict(checks=[], title=schema[checkid]["title"])) _checks[_checkid]["checks"].append( dict(code=check, title=check_names[check], count=check_data[check])) _checks = OrderedDict( (k, _checks[k]) for k in CATEGORY_IDS.keys() if _checks.get(k)) assertions = dict(page="translate", has_admin_access=user.is_superuser, language=None, project=None, pootle_path="/projects/", ctx_path="/projects/", resource_path="", resource_path_parts=[], editor_extends="projects/all/base.html", checks=_checks, previous_url=get_previous_url(request), display_priority=False, cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), current_vfolder_pk="", POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL) view_context_test(ctx, **assertions)
def get_context_data(self, *args, **kwargs): ctx = super(PootleTranslateView, self).get_context_data(*args, **kwargs) ctx.update({ 'page': 'translate', 'ctx_path': self.ctx_path, 'check_categories': get_qualitycheck_schema(), 'cantranslate': check_permission("translate", self.request), 'cansuggest': check_permission("suggest", self.request), 'canreview': check_permission("review", self.request), 'search_form': make_search_form(request=self.request), 'previous_url': get_previous_url(self.request), 'ZING_MT_BACKENDS': settings.ZING_MT_BACKENDS, 'editor_extends': self.template_extends, }) return ctx
def get_translation_context(request, is_terminology=False): """Return a common context for translation views. :param request: a :cls:`django.http.HttpRequest` object. :param is_terminology: boolean indicating if the translation context is relevant to a terminology project. """ resource_path = getattr(request, 'resource_path', '') user = request.user if not user.is_authenticated(): user = User.objects.get_nobody_user() return { 'page': 'translate', 'cantranslate': check_permission("translate", request), 'cansuggest': check_permission("suggest", request), 'canreview': check_permission("review", request), 'is_admin': check_permission('administrate', request), 'pootle_path': request.pootle_path, 'ctx_path': request.ctx_path, 'resource_path': resource_path, 'resource_path_parts': get_path_parts(resource_path), 'check_categories': get_qualitycheck_schema(), 'unit_rows': user.unit_rows, 'search_form': make_search_form(request=request, terminology=is_terminology), 'previous_url': get_previous_url(request), 'MT_BACKENDS': settings.MT_BACKENDS, 'LOOKUP_BACKENDS': settings.LOOKUP_BACKENDS, 'AMAGAMA_URL': settings.AMAGAMA_URL, }
def get_context_data(self, *args, **kwargs): ctx = super(PootleTranslateView, self).get_context_data(*args, **kwargs) ctx.update( {'page': 'translate', 'current_vfolder_pk': self.vfolder_pk, 'ctx_path': self.ctx_path, 'display_priority': self.display_vfolder_priority, 'check_categories': get_qualitycheck_schema(), 'cantranslate': check_permission("translate", self.request), 'cansuggest': check_permission("suggest", self.request), 'canreview': check_permission("review", self.request), 'search_form': make_search_form(request=self.request), 'previous_url': get_previous_url(self.request), 'POOTLE_MT_BACKENDS': settings.POOTLE_MT_BACKENDS, 'AMAGAMA_URL': settings.AMAGAMA_URL, 'editor_extends': self.template_extends}) return ctx
def render_search(context): search_form = make_search_form(request=context['request']) is_disabled = (context["page"] != "translate" and not context["can_translate_stats"]) if is_disabled: search_form.fields["search"].widget.attrs.update({ 'readonly': 'readonly', 'disabled': True, 'title': '', 'placeholder': _("Search unavailable"), }) return { 'search_form': search_form, 'search_action': context["object"].get_translate_url(), 'is_disabled': is_disabled, }
def render_search(context, form=None, action=None): if form is None: request = context['request'] translation_project = context['translation_project'] is_terminology = translation_project.project.is_terminology form = make_search_form(request=request, terminology=is_terminology) if action is None: action = l('translate.html') template_vars = { 'search_form': form, 'search_action': action, } return template_vars
def get_context_data(self, *args, **kwargs): ctx = super().get_context_data(*args, **kwargs) ctx.update( { "page": "translate", "ctx_path": self.ctx_path, "check_categories": get_qualitycheck_schema(), "cantranslate": check_permission("translate", self.request), "cansuggest": check_permission("suggest", self.request), "canreview": check_permission("review", self.request), "search_form": make_search_form(request=self.request), "previous_url": get_previous_url(self.request), "ZING_MT_BACKENDS": settings.ZING_MT_BACKENDS, "editor_extends": self.template_extends, } ) return ctx
def get_context_data(self, *args, **kwargs): ctx = super(PootleTranslateView, self).get_context_data(*args, **kwargs) ctx.update( {'page': self.page_name, 'chunk_size': self.chunk_size, 'current_vfolder_pk': self.vfolder_pk, 'ctx_path': self.ctx_path, 'display_priority': self.display_vfolder_priority, 'checks': self.checks, 'cantranslate': check_permission("translate", self.request), 'cansuggest': check_permission("suggest", self.request), 'canreview': check_permission("review", self.request), 'search_form': make_search_form(request=self.request), 'previous_url': get_previous_url(self.request), 'POOTLE_MT_BACKENDS': settings.POOTLE_MT_BACKENDS, 'AMAGAMA_URL': settings.AMAGAMA_URL, 'editor_extends': self.template_extends}) return ctx
def _test_translate_view(language, request, response, kwargs, settings): ctx = response.context checks = get_qualitychecks() schema = {sc["code"]: sc for sc in get_qualitycheck_schema()} check_data = language.data_tool.get_checks() _checks = {} for check, checkid in checks.items(): if check not in check_data: continue _checkid = schema[checkid]["name"] _checks[_checkid] = _checks.get( _checkid, dict(checks=[], title=schema[checkid]["title"])) _checks[_checkid]["checks"].append( dict( code=check, title=check_names[check], count=check_data[check])) _checks = OrderedDict( (k, _checks[k]) for k in CATEGORY_IDS.keys() if _checks.get(k)) view_context_test( ctx, **dict( project=None, language=language, page="translate", ctx_path=language.directory.pootle_path, pootle_path=language.directory.pootle_path, resource_path="", resource_path_parts=[], editor_extends="languages/base.html", checks=_checks, previous_url=get_previous_url(request), display_priority=False, has_admin_access=check_permission('administrate', request), cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), current_vfolder_pk="", POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL))
def get_translation_context(request, is_terminology=False): """Returns a common context for translation views. :param request: a :cls:`django.http.HttpRequest` object. :param is_terminology: boolean indicating if the translation context is relevant to a terminology project. """ resource_path = getattr(request, 'resource_path', '') vfolder_pk = getattr(request, 'current_vfolder', '') display_priority = False if not vfolder_pk: display_priority = VirtualFolder.objects.filter( units__store__pootle_path__startswith=request.pootle_path ).exists() return { 'page': 'translate', 'cantranslate': check_permission("translate", request), 'cansuggest': check_permission("suggest", request), 'canreview': check_permission("review", request), 'is_admin': check_permission('administrate', request), 'profile': request.profile, 'pootle_path': request.pootle_path, 'ctx_path': request.ctx_path, 'current_vfolder_pk': vfolder_pk, 'display_priority': display_priority, 'resource_path': resource_path, 'resource_path_parts': get_path_parts(resource_path), 'check_categories': get_qualitycheck_schema(), 'search_form': make_search_form(request=request, terminology=is_terminology), 'previous_url': get_previous_url(request), 'POOTLE_MT_BACKENDS': settings.POOTLE_MT_BACKENDS, 'AMAGAMA_URL': settings.AMAGAMA_URL, }
def get_context_data(self, *args, **kwargs): ctx = super(PootleTranslateView, self).get_context_data(*args, **kwargs) ctx.update( { "page": self.page_name, "current_vfolder_pk": self.vfolder_pk, "ctx_path": self.ctx_path, "display_priority": self.display_vfolder_priority, "checks": self.checks, "cantranslate": check_permission("translate", self.request), "cansuggest": check_permission("suggest", self.request), "canreview": check_permission("review", self.request), "search_form": make_search_form(request=self.request), "previous_url": get_previous_url(self.request), "POOTLE_MT_BACKENDS": settings.POOTLE_MT_BACKENDS, "AMAGAMA_URL": settings.AMAGAMA_URL, "editor_extends": self.template_extends, } ) return ctx
def render_search(context, form=None, action=None): request = context['request'] if form is None: is_terminology = False tp = context.get('translation_project', None) if tp is not None: is_terminology = tp.project.is_terminology form = make_search_form(request=request, terminology=is_terminology) if action is None: action = request.ctx_obj.get_translate_url() template_vars = { 'search_form': form, 'search_action': action, } return template_vars
def render_search(context): search_form = make_search_form(request=context["request"]) is_disabled = context["page"] != "translate" and not context[ "can_translate_stats"] if is_disabled: search_form.fields["search"].widget.attrs.update({ "readonly": "readonly", "disabled": True, "title": "", "placeholder": _("Search unavailable"), }) return { "search_form": search_form, "search_action": context["object"].get_translate_url(), "is_disabled": is_disabled, }
def _test_translate_view(language, request, response, kwargs, settings): ctx = response.context view_context_test( ctx, **dict(project=None, language=language, page="translate", ctx_path=language.directory.pootle_path, pootle_path=language.directory.pootle_path, resource_path="", resource_path_parts=[], editor_extends="languages/base.html", check_categories=get_qualitycheck_schema(), previous_url=get_previous_url(request), display_priority=False, is_admin=check_permission('administrate', request), cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), current_vfolder_pk="", POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL))
def _test_translate_view(project, request, response, kwargs, settings): if not request.user.is_superuser: assert response.status_code == 403 return ctx = response.context kwargs["project_code"] = project.code ctx_path = ( "/projects/%(project_code)s/" % kwargs) resource_path = ( "%(dir_path)s%(filename)s" % kwargs) pootle_path = "%s%s" % (ctx_path, resource_path) display_priority = False view_context_test( ctx, **dict( page="translate", has_admin_access=request.user.is_superuser, language=None, project=project, pootle_path=pootle_path, ctx_path=ctx_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), editor_extends="projects/base.html", check_categories=get_qualitycheck_schema(), previous_url=get_previous_url(request), display_priority=display_priority, cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), current_vfolder_pk="", POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL))
def _test_translate_view(tp, request, response, kwargs, settings): ctx = response.context kwargs["project_code"] = tp.project.code kwargs["language_code"] = tp.language.code resource_path = "%(dir_path)s%(filename)s" % kwargs request_path = "%s%s" % (tp.pootle_path, resource_path) vfolder, pootle_path = extract_vfolder_from_path(request_path) current_vfolder_pk = ( vfolder.pk if vfolder else "") display_priority = ( not current_vfolder_pk and not kwargs['filename'] and ctx['object'].has_vfolders) assertions = dict( page="translate", translation_project=tp, language=tp.language, project=tp.project, has_admin_access=check_permission('administrate', request), ctx_path=tp.pootle_path, pootle_path=request_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), editor_extends="translation_projects/base.html", check_categories=get_qualitycheck_schema(), previous_url=get_previous_url(request), current_vfolder_pk=current_vfolder_pk, display_priority=display_priority, cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL) view_context_test(ctx, **assertions)
def get_step_query(request, units_queryset): """Narrows down unit query to units matching conditions in GET.""" if 'filter' in request.GET: unit_filter = request.GET['filter'] username = request.GET.get('user', None) sort_by_param = request.GET.get('sort', None) sort_on = 'units' user = request.profile if username is not None: User = get_user_model() try: user = User.objects.get(username=username) except User.DoesNotExist: pass if unit_filter: match_queryset = units_queryset.none() if unit_filter == 'all': match_queryset = units_queryset elif unit_filter == 'translated': match_queryset = units_queryset.filter(state=TRANSLATED) elif unit_filter == 'untranslated': match_queryset = units_queryset.filter(state=UNTRANSLATED) elif unit_filter == 'fuzzy': match_queryset = units_queryset.filter(state=FUZZY) elif unit_filter == 'incomplete': match_queryset = units_queryset.filter( Q(state=UNTRANSLATED) | Q(state=FUZZY), ) elif unit_filter == 'suggestions': match_queryset = units_queryset.filter( suggestion__state=SuggestionStates.PENDING).distinct() elif unit_filter in ('my-suggestions', 'user-suggestions'): match_queryset = units_queryset.filter( suggestion__state=SuggestionStates.PENDING, suggestion__user=user, ).distinct() sort_on = 'suggestions' elif unit_filter == 'user-suggestions-accepted': match_queryset = units_queryset.filter( suggestion__state=SuggestionStates.ACCEPTED, suggestion__user=user, ).distinct() elif unit_filter == 'user-suggestions-rejected': match_queryset = units_queryset.filter( suggestion__state=SuggestionStates.REJECTED, suggestion__user=user, ).distinct() elif unit_filter in ('my-submissions', 'user-submissions'): match_queryset = units_queryset.filter( submission__submitter=user, submission__type__in=SubmissionTypes.EDIT_TYPES, ).distinct() sort_on = 'submissions' elif (unit_filter in ('my-submissions-overwritten', 'user-submissions-overwritten')): match_queryset = units_queryset.filter( submission__submitter=user, ).exclude( submitted_by=user).distinct() elif unit_filter == 'checks' and 'checks' in request.GET: checks = request.GET['checks'].split(',') if checks: match_queryset = units_queryset.filter( qualitycheck__false_positive=False, qualitycheck__name__in=checks, ).distinct() sort_by = ALLOWED_SORTS[sort_on].get(sort_by_param, None) if sort_by is not None: if sort_on in SIMPLY_SORTED: match_queryset = match_queryset.order_by(sort_by) else: # It's necessary to use `Max()` here because we can't # use `distinct()` and `order_by()` at the same time # (unless PostreSQL is used and `distinct(field_name)`) sort_by_max = '%s__max' % sort_by # Omit leading `-` sign max_field = sort_by[1:] if sort_by[0] == '-' else sort_by match_queryset = match_queryset.annotate(Max(max_field)) \ .order_by(sort_by_max) units_queryset = match_queryset if 'search' in request.GET and 'sfields' in request.GET: # Accept `sfields` to be a comma-separated string of fields (#46) GET = request.GET.copy() sfields = GET['sfields'] if isinstance(sfields, unicode) and u',' in sfields: GET.setlist('sfields', sfields.split(u',')) # use the search form for validation only search_form = make_search_form(GET) if search_form.is_valid(): units_queryset = get_search_step_query(search_form, units_queryset) return units_queryset
def get_step_query(request, units_queryset): """Narrows down unit query to units matching conditions in GET.""" if 'filter' in request.GET: unit_filter = request.GET['filter'] username = request.GET.get('user', None) profile = request.profile if username is not None: try: user = User.objects.get(username=username) profile = user.get_profile() except User.DoesNotExist: pass if unit_filter: match_queryset = units_queryset.none() if unit_filter == 'all': match_queryset = units_queryset elif unit_filter == 'translated': match_queryset = units_queryset.filter(state=TRANSLATED) elif unit_filter == 'untranslated': match_queryset = units_queryset.filter(state=UNTRANSLATED) elif unit_filter == 'fuzzy': match_queryset = units_queryset.filter(state=FUZZY) elif unit_filter == 'incomplete': match_queryset = units_queryset.filter( Q(state=UNTRANSLATED) | Q(state=FUZZY), ) elif unit_filter == 'suggestions': #FIXME: is None the most efficient query match_queryset = units_queryset.exclude(suggestion=None) elif unit_filter in ('my-suggestions', 'user-suggestions'): match_queryset = units_queryset.filter( suggestion__user=profile, ).distinct() elif unit_filter == 'user-suggestions-accepted': # FIXME: Oh, this is pretty lame, we need a completely # different way to model suggestions unit_ids = SuggestionStat.objects.filter( suggester=profile, state='accepted', ).values_list('unit', flat=True) match_queryset = units_queryset.filter( id__in=unit_ids, ).distinct() elif unit_filter == 'user-suggestions-rejected': # FIXME: Oh, this is as lame as above unit_ids = SuggestionStat.objects.filter( suggester=profile, state='rejected', ).values_list('unit', flat=True) match_queryset = units_queryset.filter( id__in=unit_ids, ).distinct() elif unit_filter in ('my-submissions', 'user-submissions'): match_queryset = units_queryset.filter( submission__submitter=profile, ).distinct() elif (unit_filter in ('my-submissions-overwritten', 'user-submissions-overwritten')): match_queryset = units_queryset.filter( submission__submitter=profile, ).exclude(submitted_by=profile).distinct() elif unit_filter == 'checks' and 'checks' in request.GET: checks = request.GET['checks'].split(',') if checks: match_queryset = units_queryset.filter( qualitycheck__false_positive=False, qualitycheck__name__in=checks ).distinct() units_queryset = match_queryset if 'goal' in request.GET: try: goal = Goal.objects.get(slug=request.GET['goal']) except Goal.DoesNotExist: pass else: pootle_path = (request.GET.get('path', '') or request.path.replace("/export-view/", "/", 1)) goal_stores = goal.get_stores_for_path(pootle_path) units_queryset = units_queryset.filter(store__in=goal_stores) if 'search' in request.GET and 'sfields' in request.GET: # Accept `sfields` to be a comma-separated string of fields (#46) GET = request.GET.copy() sfields = GET['sfields'] if isinstance(sfields, unicode) and u',' in sfields: GET.setlist('sfields', sfields.split(u',')) # use the search form for validation only search_form = make_search_form(GET) if search_form.is_valid(): units_queryset = get_search_step_query(request, search_form, units_queryset) return units_queryset
def get_step_query(request, units_queryset): """Narrows down unit query to units matching conditions in GET.""" if 'filter' in request.GET: unit_filter = request.GET['filter'] username = request.GET.get('user', None) profile = request.profile if username: try: user = User.objects.get(username=username) profile = user.get_profile() except User.DoesNotExist: pass if unit_filter: match_queryset = units_queryset.none() if unit_filter == 'all': match_queryset = units_queryset elif unit_filter == 'translated': match_queryset = units_queryset.filter(state=TRANSLATED) elif unit_filter == 'untranslated': match_queryset = units_queryset.filter(state=UNTRANSLATED) elif unit_filter == 'fuzzy': match_queryset = units_queryset.filter(state=FUZZY) elif unit_filter == 'incomplete': match_queryset = units_queryset.filter( Q(state=UNTRANSLATED) | Q(state=FUZZY), ) elif unit_filter == 'suggestions': #FIXME: is None the most efficient query match_queryset = units_queryset.exclude(suggestion=None) elif unit_filter == 'user-suggestions': match_queryset = units_queryset.filter( suggestion__user=profile, ).distinct() elif unit_filter == 'user-suggestions-accepted': # FIXME: Oh, this is pretty lame, we need a completely # different way to model suggestions unit_ids = SuggestionStat.objects.filter( suggester=profile, state='accepted', ).values_list('unit', flat=True) match_queryset = units_queryset.filter( id__in=unit_ids, ).distinct() elif unit_filter == 'user-suggestions-rejected': # FIXME: Oh, this is as lame as above unit_ids = SuggestionStat.objects.filter( suggester=profile, state='rejected', ).values_list('unit', flat=True) match_queryset = units_queryset.filter( id__in=unit_ids, ).distinct() elif unit_filter == 'user-submissions': match_queryset = units_queryset.filter( submission__submitter=profile, ).distinct() elif unit_filter == 'user-submissions-overwritten': match_queryset = units_queryset.filter( submission__submitter=profile, ).exclude(submitted_by=profile).distinct() elif unit_filter == 'checks' and 'checks' in request.GET: checks = request.GET['checks'].split(',') if checks: match_queryset = units_queryset.filter( qualitycheck__false_positive=False, qualitycheck__name__in=checks ).distinct() units_queryset = match_queryset if 'search' in request.GET and 'sfields' in request.GET: # use the search form for validation only search_form = make_search_form(request.GET) if search_form.is_valid(): units_queryset = get_search_step_query(request.translation_project, search_form, units_queryset) return units_queryset
def _test_vf_translate_view(tp, request, response, kwargs, settings): from .tp import view_context_test ctx = response.context obj = ctx["object"] kwargs["project_code"] = tp.project.code kwargs["language_code"] = tp.language.code resource_path = "%(dir_path)s%(filename)s" % kwargs request_path = "%s%s" % (tp.pootle_path, resource_path) checks = get_qualitychecks() schema = {sc["code"]: sc for sc in get_qualitycheck_schema()} vfolder_pk = response.context["current_vfolder_pk"] check_data = DirectoryVFDataTool(obj).get_checks( user=request.user).get(vfolder_pk, {}) _checks = {} for check, checkid in checks.items(): if check not in check_data: continue _checkid = schema[checkid]["name"] _checks[_checkid] = _checks.get( _checkid, dict(checks=[], title=schema[checkid]["title"])) _checks[_checkid]["checks"].append( dict( code=check, title=check_names[check], count=check_data[check])) _checks = OrderedDict( (k, _checks[k]) for k in CATEGORY_IDS.keys() if _checks.get(k)) vfolder = VirtualFolder.objects.get( name=request.resolver_match.kwargs["vfolder_name"]) current_vfolder_pk = vfolder.pk display_priority = False unit_api_root = reverse( "vfolder-pootle-xhr-units", kwargs=dict(vfolder_name=vfolder.name)) resource_path = ( "/".join( ["++vfolder", vfolder.name, ctx['object'].pootle_path.replace(tp.pootle_path, "")])) assertions = dict( page="translate", translation_project=tp, language=tp.language, project=tp.project, has_admin_access=check_permission('administrate', request), ctx_path=tp.pootle_path, pootle_path=request_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), editor_extends="translation_projects/base.html", checks=_checks, previous_url=get_previous_url(request), current_vfolder_pk=current_vfolder_pk, display_priority=display_priority, cantranslate=check_permission("translate", request), cansuggest=check_permission("suggest", request), canreview=check_permission("review", request), search_form=make_search_form(request=request), unit_api_root=unit_api_root, POOTLE_MT_BACKENDS=settings.POOTLE_MT_BACKENDS, AMAGAMA_URL=settings.AMAGAMA_URL) view_context_test(ctx, **assertions)
def get_step_query(request, units_queryset): """Narrows down unit query to units matching conditions in GET.""" if 'filter' in request.GET: unit_filter = request.GET['filter'] user = request.user if request.GET.get("user"): try: user = User.objects.get(username=request.GET["user"]) except User.DoesNotExist: pass if unit_filter: match_queryset = units_queryset.none() if unit_filter == 'all': match_queryset = units_queryset elif unit_filter == 'translated': match_queryset = units_queryset.filter(state=TRANSLATED) elif unit_filter == 'untranslated': match_queryset = units_queryset.filter(state=UNTRANSLATED) elif unit_filter == 'fuzzy': match_queryset = units_queryset.filter(state=FUZZY) elif unit_filter == 'incomplete': match_queryset = units_queryset.filter( Q(state=UNTRANSLATED) | Q(state=FUZZY), ) elif unit_filter == 'suggestions': match_queryset = units_queryset.filter( suggestion__state=SuggestionStates.PENDING).distinct() elif unit_filter in ('my-suggestions', 'user-suggestions'): match_queryset = units_queryset.filter( suggestion__state=SuggestionStates.PENDING, suggestion__user=user, ).distinct() elif unit_filter == 'user-suggestions-accepted': match_queryset = units_queryset.filter( suggestion__state=SuggestionStates.ACCEPTED, suggestion__user=user, ).distinct() elif unit_filter == 'user-suggestions-rejected': match_queryset = units_queryset.filter( suggestion__state=SuggestionStates.REJECTED, suggestion__user=user, ).distinct() elif unit_filter in ('my-submissions', 'user-submissions'): match_queryset = units_queryset.filter( submission__submitter=user, submission__type=SubmissionTypes.NORMAL, ).distinct() elif (unit_filter in ('my-submissions-overwritten', 'user-submissions-overwritten')): match_queryset = units_queryset.filter( submission__submitter=user, ).exclude( submitted_by=user).distinct() elif unit_filter == 'checks' and 'checks' in request.GET: checks = request.GET['checks'].split(',') if checks: match_queryset = units_queryset.filter( qualitycheck__false_positive=False, qualitycheck__name__in=checks, ).distinct() units_queryset = match_queryset if 'goal' in request.GET: try: goal = Goal.objects.get(slug=request.GET['goal']) except Goal.DoesNotExist: pass else: pootle_path = (request.GET.get('path', '') or request.path.replace("/export-view/", "/", 1)) goal_stores = goal.get_stores_for_path(pootle_path) units_queryset = units_queryset.filter(store__in=goal_stores) if 'search' in request.GET and 'sfields' in request.GET: # Accept `sfields` to be a comma-separated string of fields (#46) GET = request.GET.copy() sfields = GET['sfields'] if isinstance(sfields, unicode) and u',' in sfields: GET.setlist('sfields', sfields.split(u',')) # use the search form for validation only search_form = make_search_form(GET) if search_form.is_valid(): units_queryset = get_search_step_query(request, search_form, units_queryset) return units_queryset
def get_step_query(request, units_queryset): """Narrows down unit query to units matching conditions in GET.""" if 'filter' in request.GET: unit_filter = request.GET['filter'] username = request.GET.get('user', None) modified_since = request.GET.get('modified-since', None) month = request.GET.get('month', None) sort_by_param = request.GET.get('sort', None) sort_on = 'units' user = request.profile if username is not None: User = get_user_model() try: user = User.objects.get(username=username) except User.DoesNotExist: pass if unit_filter: match_queryset = units_queryset.none() if unit_filter == 'all': match_queryset = units_queryset elif unit_filter == 'translated': match_queryset = units_queryset.filter(state=TRANSLATED) elif unit_filter == 'untranslated': match_queryset = units_queryset.filter(state=UNTRANSLATED) elif unit_filter == 'fuzzy': match_queryset = units_queryset.filter(state=FUZZY) elif unit_filter == 'incomplete': match_queryset = units_queryset.filter( Q(state=UNTRANSLATED) | Q(state=FUZZY), ) elif unit_filter == 'suggestions': match_queryset = units_queryset.filter( suggestion__state=SuggestionStates.PENDING).distinct() elif unit_filter in ('my-suggestions', 'user-suggestions'): match_queryset = units_queryset.filter( suggestion__state=SuggestionStates.PENDING, suggestion__user=user, ).distinct() sort_on = 'suggestions' elif unit_filter == 'user-suggestions-accepted': match_queryset = units_queryset.filter( suggestion__state=SuggestionStates.ACCEPTED, suggestion__user=user, ).distinct() elif unit_filter == 'user-suggestions-rejected': match_queryset = units_queryset.filter( suggestion__state=SuggestionStates.REJECTED, suggestion__user=user, ).distinct() elif unit_filter in ('my-submissions', 'user-submissions'): match_queryset = units_queryset.filter( submission__submitter=user, submission__type__in=SubmissionTypes.EDIT_TYPES, ).distinct() sort_on = 'submissions' elif (unit_filter in ('my-submissions-overwritten', 'user-submissions-overwritten')): match_queryset = units_queryset.filter( submission__submitter=user, submission__type__in=SubmissionTypes.EDIT_TYPES, ).exclude(submitted_by=user).distinct() elif unit_filter == 'checks' and 'checks' in request.GET: checks = request.GET['checks'].split(',') if checks: match_queryset = units_queryset.filter( qualitycheck__false_positive=False, qualitycheck__name__in=checks, ).distinct() if modified_since is not None: datetime_obj = parse_datetime(modified_since) if datetime_obj is not None: match_queryset = match_queryset.filter( submitted_on__gt=datetime_obj, ).distinct() if month is not None: [start, end] = get_date_interval(month) match_queryset = match_queryset.filter( submitted_on__gte=start, submitted_on__lte=end, ).distinct() sort_by = ALLOWED_SORTS[sort_on].get(sort_by_param, None) if sort_by is not None: if sort_on in SIMPLY_SORTED: if sort_by == 'priority': # TODO: Replace the following extra() with Coalesce # https://docs.djangoproject.com/en/1.8/ref/models/database-functions/#coalesce # once we drop support for Django<1.8.x: # .annotate( # sort_by_field=Coalesce( # Max("vfolders__priority"), # Value(1) # ) # ).order_by("-sort_by_field") match_queryset = match_queryset.extra( select={ 'sort_by_field': """ SELECT COALESCE(MAX(virtualfolder_virtualfolder.priority), 1) FROM virtualfolder_virtualfolder INNER JOIN virtualfolder_virtualfolder_units ON virtualfolder_virtualfolder.id = virtualfolder_virtualfolder_units.virtualfolder_id WHERE virtualfolder_virtualfolder_units.unit_id = pootle_store_unit.id """ }).extra(order_by=['-sort_by_field']) else: match_queryset = match_queryset.order_by(sort_by) else: # Omit leading `-` sign if sort_by[0] == '-': max_field = sort_by[1:] sort_order = '-sort_by_field' else: max_field = sort_by sort_order = 'sort_by_field' # It's necessary to use `Max()` here because we can't # use `distinct()` and `order_by()` at the same time # (unless PostreSQL is used and `distinct(field_name)`) match_queryset = match_queryset \ .annotate(sort_by_field=Max(max_field)) \ .order_by(sort_order) units_queryset = match_queryset if 'search' in request.GET and 'sfields' in request.GET: # Accept `sfields` to be a comma-separated string of fields (#46) GET = request.GET.copy() sfields = GET['sfields'] if isinstance(sfields, unicode) and u',' in sfields: GET.setlist('sfields', sfields.split(u',')) # use the search form for validation only search_form = make_search_form(GET) if search_form.is_valid(): units_queryset = get_search_step_query(search_form, units_queryset) return units_queryset
def get_step_query(request, units_queryset): """Narrows down unit query to units matching conditions in GET.""" if "filter" in request.GET: unit_filter = request.GET["filter"] username = request.GET.get("user", None) modified_since = request.GET.get("modified-since", None) month = request.GET.get("month", None) sort_by_param = request.GET.get("sort", None) sort_on = "units" user = request.profile if username is not None: User = get_user_model() try: user = User.objects.get(username=username) except User.DoesNotExist: pass if unit_filter: match_queryset = units_queryset.none() if unit_filter == "all": match_queryset = units_queryset elif unit_filter == "translated": match_queryset = units_queryset.filter(state=TRANSLATED) elif unit_filter == "untranslated": match_queryset = units_queryset.filter(state=UNTRANSLATED) elif unit_filter == "fuzzy": match_queryset = units_queryset.filter(state=FUZZY) elif unit_filter == "incomplete": match_queryset = units_queryset.filter(Q(state=UNTRANSLATED) | Q(state=FUZZY)) elif unit_filter == "suggestions": match_queryset = units_queryset.filter(suggestion__state=SuggestionStates.PENDING).distinct() elif unit_filter in ("my-suggestions", "user-suggestions"): match_queryset = units_queryset.filter( suggestion__state=SuggestionStates.PENDING, suggestion__user=user ).distinct() sort_on = "suggestions" elif unit_filter == "user-suggestions-accepted": match_queryset = units_queryset.filter( suggestion__state=SuggestionStates.ACCEPTED, suggestion__user=user ).distinct() elif unit_filter == "user-suggestions-rejected": match_queryset = units_queryset.filter( suggestion__state=SuggestionStates.REJECTED, suggestion__user=user ).distinct() elif unit_filter in ("my-submissions", "user-submissions"): match_queryset = units_queryset.filter( submission__submitter=user, submission__type__in=SubmissionTypes.EDIT_TYPES ).distinct() sort_on = "submissions" elif unit_filter in ("my-submissions-overwritten", "user-submissions-overwritten"): match_queryset = ( units_queryset.filter(submission__submitter=user, submission__type__in=SubmissionTypes.EDIT_TYPES) .exclude(submitted_by=user) .distinct() ) elif unit_filter == "checks": if "checks" in request.GET: checks = request.GET["checks"].split(",") if checks: match_queryset = units_queryset.filter( qualitycheck__false_positive=False, qualitycheck__name__in=checks ).distinct() elif "category" in request.GET: category_name = request.GET["category"] try: category = get_category_id(category_name) except KeyError: raise Http404 match_queryset = units_queryset.filter( qualitycheck__false_positive=False, qualitycheck__category=category ).distinct() if modified_since is not None: datetime_obj = parse_datetime(modified_since) if datetime_obj is not None: match_queryset = match_queryset.filter(submitted_on__gt=datetime_obj).distinct() if month is not None: [start, end] = get_date_interval(month) match_queryset = match_queryset.filter(submitted_on__gte=start, submitted_on__lte=end).distinct() sort_by = ALLOWED_SORTS[sort_on].get(sort_by_param, None) if sort_by is not None: if sort_on in SIMPLY_SORTED: if sort_by == "priority": # TODO: Replace the following extra() with Coalesce # https://docs.djangoproject.com/en/1.8/ref/models/database-functions/#coalesce # once we drop support for Django<1.8.x: # .annotate( # sort_by_field=Coalesce( # Max("vfolders__priority"), # Value(1) # ) # ).order_by("-sort_by_field") match_queryset = match_queryset.extra( select={ "sort_by_field": """ SELECT COALESCE(MAX(virtualfolder_virtualfolder.priority), 1) FROM virtualfolder_virtualfolder INNER JOIN virtualfolder_virtualfolder_units ON virtualfolder_virtualfolder.id = virtualfolder_virtualfolder_units.virtualfolder_id WHERE virtualfolder_virtualfolder_units.unit_id = pootle_store_unit.id """ } ).extra(order_by=["-sort_by_field"]) else: match_queryset = match_queryset.order_by(sort_by) else: # Omit leading `-` sign if sort_by[0] == "-": max_field = sort_by[1:] sort_order = "-sort_by_field" else: max_field = sort_by sort_order = "sort_by_field" # It's necessary to use `Max()` here because we can't # use `distinct()` and `order_by()` at the same time # (unless PostreSQL is used and `distinct(field_name)`) match_queryset = match_queryset.annotate(sort_by_field=Max(max_field)).order_by(sort_order) units_queryset = match_queryset if "search" in request.GET and "sfields" in request.GET: # Accept `sfields` to be a comma-separated string of fields (#46) GET = request.GET.copy() sfields = GET["sfields"] if isinstance(sfields, unicode) and u"," in sfields: GET.setlist("sfields", sfields.split(u",")) # use the search form for validation only search_form = make_search_form(GET) if search_form.is_valid(): units_queryset = get_search_step_query(search_form, units_queryset) return units_queryset
def get_step_query(request, units_queryset): """Narrows down unit query to units matching conditions in GET.""" if 'filter' in request.GET: unit_filter = request.GET['filter'] user = request.user if request.GET.get("user"): try: user = User.objects.get(username=request.GET["user"]) except User.DoesNotExist: pass if unit_filter: match_queryset = units_queryset.none() if unit_filter == 'all': match_queryset = units_queryset elif unit_filter == 'translated': match_queryset = units_queryset.filter(state=TRANSLATED) elif unit_filter == 'untranslated': match_queryset = units_queryset.filter(state=UNTRANSLATED) elif unit_filter == 'fuzzy': match_queryset = units_queryset.filter(state=FUZZY) elif unit_filter == 'incomplete': match_queryset = units_queryset.filter( Q(state=UNTRANSLATED) | Q(state=FUZZY), ) elif unit_filter == 'suggestions': match_queryset = units_queryset.filter( suggestion__state=SuggestionStates.PENDING ).distinct() elif unit_filter in ('my-suggestions', 'user-suggestions'): match_queryset = units_queryset.filter( suggestion__state=SuggestionStates.PENDING, suggestion__user=user, ).distinct() elif unit_filter == 'user-suggestions-accepted': match_queryset = units_queryset.filter( suggestion__state=SuggestionStates.ACCEPTED, suggestion__user=user, ).distinct() elif unit_filter == 'user-suggestions-rejected': match_queryset = units_queryset.filter( suggestion__state=SuggestionStates.REJECTED, suggestion__user=user, ).distinct() elif unit_filter in ('my-submissions', 'user-submissions'): match_queryset = units_queryset.filter( submission__submitter=user, submission__type=SubmissionTypes.NORMAL, ).distinct() elif (unit_filter in ('my-submissions-overwritten', 'user-submissions-overwritten')): match_queryset = units_queryset.filter( submission__submitter=user, ).exclude(submitted_by=user).distinct() elif unit_filter == 'checks' and 'checks' in request.GET: checks = request.GET['checks'].split(',') if checks: match_queryset = units_queryset.filter( qualitycheck__false_positive=False, qualitycheck__name__in=checks, ).distinct() units_queryset = match_queryset if 'goal' in request.GET: try: goal = Goal.objects.get(slug=request.GET['goal']) except Goal.DoesNotExist: pass else: pootle_path = (request.GET.get('path', '') or request.path.replace("/export-view/", "/", 1)) goal_stores = goal.get_stores_for_path(pootle_path) units_queryset = units_queryset.filter(store__in=goal_stores) if 'search' in request.GET and 'sfields' in request.GET: # Accept `sfields` to be a comma-separated string of fields (#46) GET = request.GET.copy() sfields = GET['sfields'] if isinstance(sfields, unicode) and u',' in sfields: GET.setlist('sfields', sfields.split(u',')) # use the search form for validation only search_form = make_search_form(GET) if search_form.is_valid(): units_queryset = get_search_step_query(request, search_form, units_queryset) return units_queryset