def _test_view_project_children(view, project): request = view.request kwargs = view.kwargs resource_path = ("%(dir_path)s%(filename)s" % kwargs) project_path = ("%s/%s" % (kwargs["project_code"], resource_path)) if not (kwargs["dir_path"] or kwargs["filename"]): obj = project elif not kwargs["filename"]: obj = ProjectResource(Directory.objects.live().filter( pootle_path__regex="^/.*/%s$" % project_path), pootle_path="/projects/%s" % project_path) else: obj = ProjectResource(Store.objects.live().filter( pootle_path__regex="^/.*/%s$" % project_path), pootle_path="/projects/%s" % project_path) item_func = (make_xlanguage_item if (kwargs["dir_path"] or kwargs["filename"]) else make_language_item) items = [ item_func(item) for item in obj.get_children_for_user(request.user) ] stats = obj.data_tool.get_stats(user=request.user) stats = StatsDisplay(obj, stats=stats).stats for item in items: if item["code"] in stats["children"]: item["stats"] = stats["children"][item["code"]] items.sort(cmp_by_last_activity) assert view.object_children == items
def _test_browse_view(project, request, response, kwargs): cookie_data = json.loads( unquote(response.cookies[SIDEBAR_COOKIE_NAME].value)) assert cookie_data["foo"] == "bar" assert "announcements_projects_%s" % project.code in cookie_data ctx = response.context kwargs["project_code"] = project.code resource_path = ("%(dir_path)s%(filename)s" % kwargs) project_path = ("%s/%s" % (kwargs["project_code"], resource_path)) if not (kwargs["dir_path"] or kwargs["filename"]): ob = project elif not kwargs["filename"]: ob = ProjectResource(Directory.objects.live().filter( pootle_path__regex="^/.*/%s$" % project_path), pootle_path="/projects/%s" % project_path) else: ob = ProjectResource(Store.objects.live().filter( pootle_path__regex="^/.*/%s$" % project_path), pootle_path="/projects/%s" % project_path) item_func = (make_xlanguage_item if (kwargs["dir_path"] or kwargs["filename"]) else make_language_item) items = [ item_func(item) for item in ob.get_children_for_user(request.user) ] items.sort(lambda x, y: locale.strcoll(x['title'], y['title'])) table_fields = [ 'name', 'progress', 'total', 'need-translation', 'suggestions', 'critical', 'last-updated', 'activity' ] table = { 'id': 'project', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'items': items } assertions = dict( page="browse", project=project, browser_extends="projects/base.html", pootle_path="/projects/%s" % project_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), url_action_continue=ob.get_translate_url(state='incomplete'), url_action_fixcritical=ob.get_critical_url(), url_action_review=ob.get_translate_url(state='suggestions'), url_action_view_all=ob.get_translate_url(state='all'), translation_states=get_translation_states(ob), check_categories=get_qualitycheck_schema(ob), table=table, stats=jsonify(ob.get_stats())) sidebar = get_sidebar_announcements_context(request, (project, )) for k in ["has_sidebar", "is_sidebar_open", "announcements"]: assertions[k] = sidebar[0][k] view_context_test(ctx, **assertions)
def _test_browse_view(project, request, response, kwargs): cookie_data = json.loads(unquote(response.cookies[SIDEBAR_COOKIE_NAME].value)) assert cookie_data["foo"] == "bar" assert "announcements_projects_%s" % project.code in cookie_data ctx = response.context kwargs["project_code"] = project.code resource_path = "%(dir_path)s%(filename)s" % kwargs project_path = "%s/%s" % (kwargs["project_code"], resource_path) if not (kwargs["dir_path"] or kwargs["filename"]): ob = project elif not kwargs["filename"]: ob = ProjectResource( Directory.objects.live().filter(pootle_path__regex="^/.*/%s$" % project_path), pootle_path="/projects/%s" % project_path, ) else: ob = ProjectResource( Store.objects.live().filter(pootle_path__regex="^/.*/%s$" % project_path), pootle_path="/projects/%s" % project_path, ) item_func = make_xlanguage_item if (kwargs["dir_path"] or kwargs["filename"]) else make_language_item items = [item_func(item) for item in ob.get_children_for_user(request.profile)] items.sort(lambda x, y: locale.strcoll(x["title"], y["title"])) table_fields = [ "name", "progress", "total", "need-translation", "suggestions", "critical", "last-updated", "activity", ] table = {"id": "project", "fields": table_fields, "headings": get_table_headings(table_fields), "items": items} assertions = dict( page="browse", project=project, resource_obj=ob, browser_extends="projects/base.html", pootle_path="/projects/%s" % project_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), url_action_continue=ob.get_translate_url(state="incomplete"), url_action_fixcritical=ob.get_critical_url(), url_action_review=ob.get_translate_url(state="suggestions"), url_action_view_all=ob.get_translate_url(state="all"), translation_states=get_translation_states(ob), check_categories=get_qualitycheck_schema(ob), table=table, stats=jsonify(ob.get_stats()), ) sidebar = get_sidebar_announcements_context(request, (project,)) for k in ["has_sidebar", "is_sidebar_open", "announcements"]: assertions[k] = sidebar[0][k] view_context_test(ctx, **assertions)
def test_data_cache_keys(language0, project0, subdir0, vfolder0): # language assert ('pootle_data.%s.%s.%s' % (language0.data_tool.cache_key_name, language0.code, revision.get(Language)(language0).get( key="stats")) == language0.data_tool.cache_key) # project assert ('pootle_data.%s.%s.%s' % (project0.data_tool.cache_key_name, project0.code, revision.get(Project)(project0).get( key="stats")) == project0.data_tool.cache_key) # directory assert ('pootle_data.%s.%s.%s' % (subdir0.data_tool.cache_key_name, subdir0.pootle_path, revision.get(Directory)(subdir0).get( key="stats")) == subdir0.data_tool.cache_key) # projectresource resource_path = "%s%s" % (project0.pootle_path, subdir0.path) projectresource = ProjectResource(Directory.objects.none(), resource_path) assert ('pootle_data.%s.%s.%s' % (projectresource.data_tool.cache_key_name, resource_path, revision.get(ProjectResource)(projectresource).get( key="stats")) == projectresource.data_tool.cache_key) # projectset projectset = ProjectSet(Project.objects.all()) assert ( 'pootle_data.%s.%s.%s' % (projectset.data_tool.cache_key_name, "ALL", revision.get(ProjectSet) (projectset).get(key="stats")) == projectset.data_tool.cache_key) # vfolders vfdata = vfolders_data_tool.get(Directory)(subdir0) assert ('pootle_data.%s.%s.%s' % (vfdata.cache_key_name, subdir0.pootle_path, revision.get(subdir0.__class__)(subdir0).get( key="stats")) == vfdata.cache_key)
def project_dir_resources0(project0, subdir0): """Returns a ProjectResource object for a Directory""" from pootle_app.models import Directory from pootle_project.models import ProjectResource resources = Directory.objects.live().filter( name=subdir0.name, parent__translationproject__project=project0) return ProjectResource(resources, ("/projects/%s/%s" % (project0.code, subdir0.name)))
def set_project_resource(request, path_obj, dir_path, filename): """Loads :cls:`pootle_app.models.Directory` and :cls:`pootle_store.models.Store` models and populates the request object. This is the same as `set_resource` but operates at the project level across all languages. :param path_obj: A :cls:`pootle_project.models.Project` object. :param dir_path: Path relative to the root of `path_obj`. :param filename: Optional filename. """ query_ctx_path = ''.join(['/%/', path_obj.code, '/']) query_pootle_path = query_ctx_path + dir_path obj_directory = getattr(path_obj, 'directory', path_obj) ctx_path = obj_directory.pootle_path resource_path = dir_path pootle_path = ctx_path + dir_path if filename: query_pootle_path = query_pootle_path + filename pootle_path = pootle_path + filename resource_path = resource_path + filename resources = Store.objects.extra( where=[ 'pootle_store_store.pootle_path LIKE %s', 'pootle_store_store.pootle_path NOT LIKE %s', ], params=[query_pootle_path, '/templates/%' ]).select_related('translation_project__language') else: resources = Directory.objects.extra( where=[ 'pootle_app_directory.pootle_path LIKE %s', 'pootle_app_directory.pootle_path NOT LIKE %s', ], params=[query_pootle_path, '/templates/%']).select_related('parent') if not resources.exists(): raise Http404 request.store = None request.directory = None request.pootle_path = pootle_path request.resource_obj = ProjectResource(resources, pootle_path) request.resource_path = resource_path request.ctx_obj = path_obj or request.resource_obj request.ctx_path = ctx_path
def _test_view_project_children(view, project): request = view.request kwargs = view.kwargs resource_path = ( "%(dir_path)s%(filename)s" % kwargs) project_path = ( "%s/%s" % (kwargs["project_code"], resource_path)) if not (kwargs["dir_path"] or kwargs["filename"]): obj = project elif not kwargs["filename"]: obj = ProjectResource( Directory.objects.live().filter( pootle_path__regex="^/.*/%s$" % project_path), pootle_path="/projects/%s" % project_path) else: obj = ProjectResource( Store.objects.live().filter( pootle_path__regex="^/.*/%s$" % project_path), pootle_path="/projects/%s" % project_path) item_func = ( make_xlanguage_item if (kwargs["dir_path"] or kwargs["filename"]) else make_language_item) items = [ item_func(item) for item in obj.get_children_for_user(request.user) ] items.sort(lambda x, y: locale.strcoll(x['title'], y['title'])) stats = obj.data_tool.get_stats(user=request.user) stats = StatsDisplay(obj, stats=stats).stats for item in items: if item["code"] in stats["children"]: item["stats"] = stats["children"][item["code"]] assert view.object_children == items
def project_store_resources0(project0, subdir0): """Returns a ProjectResource object for a Store""" from pootle_project.models import ProjectResource from pootle_store.models import Store store = subdir0.child_stores.live().first() resources = Store.objects.live().filter( name=store.name, parent__name=subdir0.name, translation_project__project=project0) return ProjectResource(resources, ("/projects/%s/%s/%s" % (project0.code, subdir0.name, store.name)))
def test_get_qc_stats_project_dir(client, request_users, settings, subdir0): user = request_users["user"] if user.username != "nobody": client.login(username=user.username, password=request_users["password"]) project = subdir0.tp.project dirs = Directory.objects.live().filter(tp__project=project) resources = (dirs.exclude(pootle_path__startswith="/templates").filter( tp_path=subdir0.tp_path)) resource_path = "/projects/%s%s" % (project.code, subdir0.tp_path) projectres = ProjectResource(resources, resource_path) response = client.get("/xhr/stats/checks/?path=%s" % resource_path, HTTP_X_REQUESTED_WITH='XMLHttpRequest') result = json.loads(response.content) checks = projectres.data_tool.get_checks() or {} for k, v in result.items(): assert checks[k] == v
def test_get_qc_stats_project_store(client, request_users, settings, subdir0): user = request_users["user"] if user.username != "nobody": client.login(username=user.username, password=request_users["password"]) store = subdir0.child_stores.first() project = subdir0.tp.project resources = (Store.objects.live( ).select_related("translation_project__language").filter( translation_project__project=project).filter(tp_path=store.tp_path)) resource_path = "/projects/%s%s" % (project.code, store.tp_path) projectres = ProjectResource(resources, resource_path) response = client.get("/xhr/stats/checks/?path=%s" % resource_path, HTTP_X_REQUESTED_WITH='XMLHttpRequest') result = json.loads(response.content) checks = projectres.data_tool.get_checks() or {} for k, v in result.items(): assert checks[k] == v
def set_project_resource(request, path_obj, dir_path, filename): """Loads :cls:`pootle_app.models.Directory` and :cls:`pootle_store.models.Store` models and populates the request object. This is the same as `set_resource` but operates at the project level across all languages. :param path_obj: A :cls:`pootle_project.models.Project` object. :param dir_path: Path relative to the root of `path_obj`. :param filename: Optional filename. """ query_ctx_path = ''.join(['/%/', path_obj.code, '/']) query_pootle_path = query_ctx_path + dir_path obj_directory = getattr(path_obj, 'directory', path_obj) ctx_path = obj_directory.pootle_path resource_path = dir_path pootle_path = ctx_path + dir_path # List of disabled TP paths disabled_tps = TranslationProject.objects.disabled().filter( project__code=path_obj.code, ).values_list('pootle_path', flat=True) disabled_tps = list(disabled_tps) disabled_tps.append('/templates/') disabled_tps_regex = '^%s' % u'|'.join(disabled_tps) sql_not_regex = 'NOT REGEXP' if connection.vendor == 'postgresql': sql_not_regex = '!~' if filename: query_pootle_path = query_pootle_path + filename pootle_path = pootle_path + filename resource_path = resource_path + filename resources = Store.objects.live().extra( where=[ 'pootle_store_store.pootle_path LIKE %s', 'pootle_store_store.pootle_path ' + sql_not_regex + ' %s', ], params=[query_pootle_path, disabled_tps_regex] ).select_related('translation_project__language') else: resources = Directory.objects.live().extra( where=[ 'pootle_app_directory.pootle_path LIKE %s', 'pootle_app_directory.pootle_path ' + sql_not_regex + ' %s', ], params=[query_pootle_path, disabled_tps_regex] ).select_related('parent') if not resources.exists(): raise Http404 request.store = None request.directory = None request.pootle_path = pootle_path request.resource_obj = ProjectResource(resources, pootle_path) request.resource_path = resource_path request.ctx_obj = path_obj or request.resource_obj request.ctx_path = ctx_path
def _test_browse_view(project, request, response, kwargs): assert (request.user.is_anonymous or "announcements/projects/%s" % project.code in request.session) ctx = response.context kwargs["project_code"] = project.code resource_path = ( "%(dir_path)s%(filename)s" % kwargs) project_path = ( "%s/%s" % (kwargs["project_code"], resource_path)) if not (kwargs["dir_path"] or kwargs["filename"]): obj = project elif not kwargs["filename"]: obj = ProjectResource( Directory.objects.live().filter( pootle_path__regex="^/.*/%s$" % project_path), pootle_path="/projects/%s" % project_path) else: obj = ProjectResource( Store.objects.live().filter( pootle_path__regex="^/.*/%s$" % project_path), pootle_path="/projects/%s" % project_path) stats = obj.data_tool.get_stats(user=request.user) if request.user.is_superuser or kwargs.get("language_code"): url_action_continue = obj.get_translate_url(state='incomplete') url_action_fixcritical = obj.get_critical_url() url_action_review = obj.get_translate_url(state='suggestions') url_action_view_all = obj.get_translate_url(state='all') else: (url_action_continue, url_action_fixcritical, url_action_review, url_action_view_all) = [None] * 4 checks = ChecksDisplay(obj).checks_by_category stats = StatsDisplay(obj, stats=stats).stats del stats["children"] User = get_user_model() top_scorers = User.top_scorers(project=project.code, limit=10) assertions = dict( page="browse", project=project, browser_extends="projects/base.html", pootle_path="/projects/%s" % project_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), url_action_continue=url_action_continue, url_action_fixcritical=url_action_fixcritical, url_action_review=url_action_review, url_action_view_all=url_action_view_all, translation_states=get_translation_states(obj), top_scorers=top_scorers, top_scorers_data=get_top_scorers_data(top_scorers, 10), checks=checks, stats=stats) sidebar = get_sidebar_announcements_context( request, (project, )) for k in ["has_sidebar", "is_sidebar_open", "announcements"]: assertions[k] = sidebar[k] view_context_test(ctx, **assertions)
def _test_browse_view(project, request, response, kwargs): cookie_data = json.loads( unquote(response.cookies[SIDEBAR_COOKIE_NAME].value)) assert cookie_data["foo"] == "bar" assert "announcements_projects_%s" % project.code in cookie_data ctx = response.context kwargs["project_code"] = project.code resource_path = ( "%(dir_path)s%(filename)s" % kwargs) project_path = ( "%s/%s" % (kwargs["project_code"], resource_path)) if not (kwargs["dir_path"] or kwargs["filename"]): obj = project elif not kwargs["filename"]: obj = ProjectResource( Directory.objects.live().filter( pootle_path__regex="^/.*/%s$" % project_path), pootle_path="/projects/%s" % project_path) else: obj = ProjectResource( Store.objects.live().filter( pootle_path__regex="^/.*/%s$" % project_path), pootle_path="/projects/%s" % project_path) item_func = ( make_xlanguage_item if (kwargs["dir_path"] or kwargs["filename"]) else make_language_item) items = [ item_func(item) for item in obj.get_children_for_user(request.user) ] items.sort(lambda x, y: locale.strcoll(x['title'], y['title'])) table_fields = ['name', 'progress', 'total', 'need-translation', 'suggestions', 'critical', 'last-updated', 'activity'] table = { 'id': 'project', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'items': items} if request.user.is_superuser or kwargs.get("language_code"): url_action_continue = obj.get_translate_url(state='incomplete') url_action_fixcritical = obj.get_critical_url() url_action_review = obj.get_translate_url(state='suggestions') url_action_view_all = obj.get_translate_url(state='all') else: (url_action_continue, url_action_fixcritical, url_action_review, url_action_view_all) = [None] * 4 User = get_user_model() top_scorers = User.top_scorers(project=project.code, limit=10) assertions = dict( page="browse", project=project, browser_extends="projects/base.html", pootle_path="/projects/%s" % project_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), url_action_continue=url_action_continue, url_action_fixcritical=url_action_fixcritical, url_action_review=url_action_review, url_action_view_all=url_action_view_all, translation_states=get_translation_states(obj), checks=get_qualitycheck_list(obj), table=table, top_scorers=top_scorers, top_scorers_data=get_top_scorers_data(top_scorers, 10), stats=obj.data_tool.get_stats(user=request.user), ) sidebar = get_sidebar_announcements_context( request, (project, )) for k in ["has_sidebar", "is_sidebar_open", "announcements"]: assertions[k] = sidebar[0][k] view_context_test(ctx, **assertions)
def _test_browse_view(project, request, response, kwargs): cookie_data = json.loads( unquote(response.cookies[SIDEBAR_COOKIE_NAME].value)) assert cookie_data["foo"] == "bar" assert "announcements_projects_%s" % project.code in cookie_data ctx = response.context kwargs["project_code"] = project.code resource_path = ("%(dir_path)s%(filename)s" % kwargs) project_path = ("%s/%s" % (kwargs["project_code"], resource_path)) if not (kwargs["dir_path"] or kwargs["filename"]): obj = project elif not kwargs["filename"]: obj = ProjectResource(Directory.objects.live().filter( pootle_path__regex="^/.*/%s$" % project_path), pootle_path="/projects/%s" % project_path) else: obj = ProjectResource(Store.objects.live().filter( pootle_path__regex="^/.*/%s$" % project_path), pootle_path="/projects/%s" % project_path) item_func = (make_xlanguage_item if (kwargs["dir_path"] or kwargs["filename"]) else make_language_item) items = [ item_func(item) for item in obj.get_children_for_user(request.user) ] items.sort(lambda x, y: locale.strcoll(x['title'], y['title'])) stats = obj.data_tool.get_stats(user=request.user) for item in items: if item["code"] in stats["children"]: item["stats"] = stats["children"][item["code"]] table_fields = [ 'name', 'progress', 'total', 'need-translation', 'suggestions', 'critical', 'last-updated', 'activity' ] table = { 'id': 'project', 'fields': table_fields, 'headings': get_table_headings(table_fields), 'items': items } if request.user.is_superuser or kwargs.get("language_code"): url_action_continue = obj.get_translate_url(state='incomplete') url_action_fixcritical = obj.get_critical_url() url_action_review = obj.get_translate_url(state='suggestions') url_action_view_all = obj.get_translate_url(state='all') else: (url_action_continue, url_action_fixcritical, url_action_review, url_action_view_all) = [None] * 4 checks = ChecksDisplay(obj).checks_by_category stats = StatsDisplay(obj, stats=stats).stats del stats["children"] User = get_user_model() top_scorers = User.top_scorers(project=project.code, limit=10) assertions = dict(page="browse", project=project, browser_extends="projects/base.html", pootle_path="/projects/%s" % project_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), url_action_continue=url_action_continue, url_action_fixcritical=url_action_fixcritical, url_action_review=url_action_review, url_action_view_all=url_action_view_all, translation_states=get_translation_states(obj), table=table, top_scorers=top_scorers, top_scorers_data=get_top_scorers_data(top_scorers, 10), checks=checks, stats=stats) sidebar = get_sidebar_announcements_context(request, (project, )) for k in ["has_sidebar", "is_sidebar_open", "announcements"]: assertions[k] = sidebar[0][k] view_context_test(ctx, **assertions)
def get_qualitycheck_stats(request, *args, **kwargs): pootle_path = request.GET.get('path', None) if pootle_path is None: raise Http400(_('Arguments missing.')) (language_code, project_code, dir_path, filename) = split_pootle_path(pootle_path) if language_code and project_code: tp = get_object_or_404( TranslationProject, language__code=language_code, project__code=project_code) tp_path = "/%s%s" % (dir_path, filename) if filename: resource = get_object_or_404( Store, translation_project=tp, tp_path=tp_path) elif tp_path != "/": resource = get_object_or_404( Directory, tp=tp, tp_path=tp_path) else: resource = tp.directory elif language_code: resource = get_object_or_404(Language, code=language_code) elif project_code: project = get_object_or_404(Project, code=project_code) if dir_path or filename: tp_path = "/%s%s" % (dir_path, filename) if not filename: dirs = Directory.objects.live().filter(tp__project=project) if dir_path.count("/"): dirs = dirs.select_related( "parent", "tp", "tp__language") resources = ( dirs.exclude(pootle_path__startswith="/templates") .filter(tp_path=tp_path)) else: resources = ( Store.objects.live() .select_related("translation_project__language") .filter(translation_project__project=project) .filter(tp_path=tp_path)) if resources: resource = ProjectResource( resources, ("/projects/%s%s" % (project.code, tp_path))) else: raise Http404 else: resource = project else: resource = ProjectSet( Project.objects.for_user(request.user).select_related("directory")) failing_checks = resource.data_tool.get_checks() return JsonResponse(failing_checks if failing_checks is not None else {})
def _test_browse_view(project, request, response, kwargs): assert (request.user.is_anonymous or "announcements/projects/%s" % project.code in request.session) ctx = response.context kwargs["project_code"] = project.code resource_path = ("%(dir_path)s%(filename)s" % kwargs) project_path = ("%s/%s" % (kwargs["project_code"], resource_path)) if not (kwargs["dir_path"] or kwargs["filename"]): obj = project elif not kwargs["filename"]: obj = ProjectResource(Directory.objects.live().filter( pootle_path__regex="^/.*/%s$" % project_path), pootle_path="/projects/%s" % project_path) else: obj = ProjectResource(Store.objects.live().filter( pootle_path__regex="^/.*/%s$" % project_path), pootle_path="/projects/%s" % project_path) stats = obj.data_tool.get_stats(user=request.user) if request.user.is_superuser or kwargs.get("language_code"): url_action_continue = obj.get_translate_url(state='incomplete') url_action_fixcritical = obj.get_critical_url() url_action_review = obj.get_translate_url(state='suggestions') url_action_view_all = obj.get_translate_url(state='all') else: (url_action_continue, url_action_fixcritical, url_action_review, url_action_view_all) = [None] * 4 checks = ChecksDisplay(obj).checks_by_category stats = StatsDisplay(obj, stats=stats).stats del stats["children"] chunk_size = TOP_CONTRIBUTORS_CHUNK_SIZE score_data = scores.get(Project)(project) def scores_to_json(score): score["user"] = score["user"].to_dict() return score top_scorers = score_data.display(limit=chunk_size, formatter=scores_to_json) top_scorer_data = dict( items=list(top_scorers), has_more_items=len(score_data.top_scorers) > chunk_size) assertions = dict(page="browse", project=project, browser_extends="projects/base.html", pootle_path="/projects/%s" % project_path, resource_path=resource_path, resource_path_parts=get_path_parts(resource_path), url_action_continue=url_action_continue, url_action_fixcritical=url_action_fixcritical, url_action_review=url_action_review, url_action_view_all=url_action_view_all, translation_states=get_translation_states(obj), top_scorers=top_scorer_data, checks=checks, stats=stats) sidebar = get_sidebar_announcements_context(request, (project, )) for k in ["has_sidebar", "is_sidebar_open", "announcements"]: assertions[k] = sidebar[k] view_context_test(ctx, **assertions)
def set_project_resource(request, path_obj, dir_path, filename): """Loads :cls:`pootle_app.models.Directory` and :cls:`pootle_store.models.Store` models and populates the request object. This is the same as `set_resource` but operates at the project level across all languages. :param path_obj: A :cls:`pootle_project.models.Project` object. :param dir_path: Path relative to the root of `path_obj`. :param filename: Optional filename. """ query_ctx_path = "".join(["/%/", path_obj.code, "/"]) query_pootle_path = query_ctx_path + dir_path obj_directory = getattr(path_obj, "directory", path_obj) ctx_path = obj_directory.pootle_path resource_path = dir_path pootle_path = ctx_path + dir_path # List of TP paths available for user user_tps = TranslationProject.objects.for_user(request.user) user_tps = user_tps.filter(project__code=path_obj.code, ).values_list( "pootle_path", flat=True) user_tps_regex = "^%s" % u"|".join(list(user_tps)) sql_regex = "REGEXP" if connection.vendor == "postgresql": sql_regex = "~" if filename: query_pootle_path = query_pootle_path + filename pootle_path = pootle_path + filename resource_path = resource_path + filename resources = (Store.objects.live().extra( where=[ "pootle_store_store.pootle_path LIKE %s", "pootle_store_store.pootle_path " + sql_regex + " %s", ], params=[query_pootle_path, user_tps_regex], ).select_related("translation_project__language")) else: resources = (Directory.objects.live().extra( where=[ "pootle_app_directory.pootle_path LIKE %s", "pootle_app_directory.pootle_path " + sql_regex + " %s", ], params=[query_pootle_path, user_tps_regex], ).select_related("parent")) if not resources.exists(): raise Http404 request.store = None request.directory = None request.pootle_path = pootle_path request.resource_obj = ProjectResource(resources, pootle_path) request.resource_path = resource_path request.ctx_obj = path_obj or request.resource_obj request.ctx_path = ctx_path