Exemplo n.º 1
0
def test_view_projects_browse(client, request_users):
    user = request_users["user"]
    client.login(
        username=user.username,
        password=request_users["password"])
    response = client.get(reverse("pootle-projects-browse"))
    assert response.cookies["pootle-language"].value == "projects"
    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)
    items = [
        make_project_list_item(project)
        for project in obj.children]
    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': 'projects',
        'fields': table_fields,
        'headings': get_table_headings(table_fields),
        'items': items}

    if request.user.is_superuser:
        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(limit=10)
    assertions = dict(
        page="browse",
        pootle_path="/projects/",
        resource_path="",
        resource_path_parts=[],
        object=obj,
        table=table,
        browser_extends="projects/all/base.html",
        stats=obj.data_tool.get_stats(user=request.user),
        checks=get_qualitycheck_list(obj),
        top_scorers=top_scorers,
        top_scorers_data=get_top_scorers_data(top_scorers, 10),
        translation_states=get_translation_states(obj),
        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)
    view_context_test(ctx, **assertions)
Exemplo n.º 2
0
def test_view_projects_browse(client, request_users):
    user = request_users["user"]
    client.login(
        username=user.username,
        password=request_users["password"])
    response = client.get(reverse("pootle-projects-browse"))
    assert response.cookies["pootle-language"].value == "projects"
    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)
    stats = obj.data_tool.get_stats(user=request.user)

    if request.user.is_superuser:
        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(ProjectSet)(obj)

    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",
        pootle_path="/projects/",
        resource_path="",
        resource_path_parts=[],
        object=obj,
        browser_extends="projects/all/base.html",
        top_scorers=top_scorer_data,
        translation_states=get_translation_states(obj),
        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,
        checks=checks,
        stats=stats)
    view_context_test(ctx, **assertions)
Exemplo n.º 3
0
def test_view_projects_browse(client, request_users):
    user = request_users["user"]
    client.login(
        username=user.username,
        password=request_users["password"])
    response = client.get(reverse("pootle-projects-browse"))
    assert response.cookies["pootle-language"].value == "projects"
    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)
    stats = obj.data_tool.get_stats(user=request.user)

    if request.user.is_superuser:
        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(limit=10)
    assertions = dict(
        page="browse",
        pootle_path="/projects/",
        resource_path="",
        resource_path_parts=[],
        object=obj,
        browser_extends="projects/all/base.html",
        top_scorers=top_scorers,
        top_scorers_data=get_top_scorers_data(top_scorers, 10),
        translation_states=get_translation_states(obj),
        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,
        checks=checks,
        stats=stats)
    view_context_test(ctx, **assertions)
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
    def wrapped(request, *args, **kwargs):
        language_code = kwargs.pop('language_code', None)
        project_code = kwargs.pop('project_code', None)
        if request.is_ajax():
            pootle_path = request.GET.get('path', None)
            if pootle_path is not None:
                language_code, project_code, dir_path, filename = \
                    split_pootle_path(pootle_path)
                kwargs['dir_path'] = dir_path
                kwargs['filename'] = filename

        if language_code and project_code:
            try:
                path_obj = TranslationProject.objects.enabled().get(
                    language__code=language_code,
                    project__code=project_code,
                    project__disabled=False
                )
            except TranslationProject.DoesNotExist:
                path_obj = None

            if path_obj is None and not request.is_ajax():
                # Explicit selection via the UI: redirect either to
                # ``/language_code/`` or ``/projects/project_code/``.
                user_choice = request.COOKIES.get('user-choice', None)
                if user_choice and user_choice in ('language', 'project',):
                    url = {
                        'language': reverse('pootle-language-overview',
                                            args=[language_code]),
                        'project': reverse('pootle-project-overview',
                                           args=[project_code, '', '']),
                    }
                    response = redirect(url[user_choice])
                    response.delete_cookie('user-choice')

                    return response

                raise Http404
        elif language_code:
            path_obj = get_object_or_404(Language, code=language_code)
        elif project_code:
            path_obj = get_object_or_404(Project, code=project_code,
                                         disabled=False)
        else:  # No arguments: all user-accessible projects
            user_projects = Project.accessible_by_user(request.user)
            user_projects = Project.objects.filter(code__in=user_projects)
            path_obj = ProjectSet(user_projects, '/projects/')

            # HACKISH: inject directory so that permissions can be
            # queried
            directory = Directory.objects.get(pootle_path='/projects/')
            setattr(path_obj, 'directory', directory)

        request.ctx_obj = path_obj
        request.ctx_path = path_obj.pootle_path
        request.resource_obj = path_obj
        request.pootle_path = path_obj.pootle_path

        return func(request, path_obj, *args, **kwargs)
Exemplo n.º 6
0
 def score_context(self):
     (language_code, project_code, dir_path,
      filename) = split_pootle_path(self.path)
     if language_code and project_code:
         return self.object.translationproject
     elif language_code:
         return Language.objects.get(code=language_code)
     elif project_code:
         return Project.objects.get(code=project_code)
     return ProjectSet(
         Project.objects.for_user(
             self.request.user).select_related("directory"))
Exemplo n.º 7
0
def test_view_projects_browse(client, request_users):
    user = request_users["user"]
    client.login(
        username=user.username,
        password=request_users["password"])
    response = client.get(reverse("pootle-projects-browse"))
    assert response.cookies["pootle-language"].value == "projects"
    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))
    ob = ProjectSet(user_projects)
    items = [
        make_project_list_item(project)
        for project in ob.children]
    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': 'projects',
        'fields': table_fields,
        'headings': get_table_headings(table_fields),
        'items': items}

    if request.user.is_superuser:
        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')
    else:
        (url_action_continue,
         url_action_fixcritical,
         url_action_review,
         url_action_view_all) = [None] * 4

    assertions = dict(
        page="browse",
        pootle_path="/projects/",
        resource_path="",
        resource_path_parts=[],
        object=ob,
        table=table,
        browser_extends="projects/all/base.html",
        stats=jsonify(ob.get_stats()),
        check_categories=get_qualitycheck_schema(ob),
        translation_states=get_translation_states(ob),
        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)
    view_context_test(ctx, **assertions)
Exemplo n.º 8
0
def test_view_projects_browse(client, request_users):
    user = request_users["user"]
    client.login(username=user.username, password=request_users["password"])
    response = client.get(reverse("pootle-projects-browse"))
    assert response.cookies["pootle-language"].value == "projects"
    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)
    items = [make_project_list_item(project) for project in obj.children]
    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': 'projects',
        'fields': table_fields,
        'headings': get_table_headings(table_fields),
        'items': items
    }

    if request.user.is_superuser:
        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(limit=10)
    assertions = dict(page="browse",
                      pootle_path="/projects/",
                      resource_path="",
                      resource_path_parts=[],
                      object=obj,
                      table=table,
                      browser_extends="projects/all/base.html",
                      top_scorers=top_scorers,
                      top_scorers_data=get_top_scorers_data(top_scorers, 10),
                      translation_states=get_translation_states(obj),
                      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,
                      checks=checks,
                      stats=stats)
    view_context_test(ctx, **assertions)
Exemplo n.º 9
0
def test_get_qc_stats_projects(client, request_users, settings):
    user = request_users["user"]
    if user.username != "nobody":
        client.login(username=user.username,
                     password=request_users["password"])
    user_projects = (
        Project.objects.for_user(user).select_related("directory"))
    project_set = ProjectSet(user_projects)
    response = client.get("/xhr/stats/checks/?path=/projects/",
                          HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    result = json.loads(response.content)
    checks = project_set.data_tool.get_checks() or {}
    for k, v in result.items():
        assert checks[k] == v
Exemplo n.º 10
0
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)
Exemplo n.º 11
0
def test_view_projects_browse(client, request_users):
    user = request_users["user"]
    client.login(username=user.username, password=request_users["password"])
    response = client.get(reverse("pootle-projects-browse"))
    assert response.cookies["pootle-language"].value == "projects"
    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)
    stats = obj.data_tool.get_stats(user=request.user)

    if request.user.is_superuser:
        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(ProjectSet)(obj)

    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",
                      pootle_path="/projects/",
                      resource_path="",
                      resource_path_parts=[],
                      object=obj,
                      browser_extends="projects/all/base.html",
                      top_scorers=top_scorer_data,
                      translation_states=get_translation_states(obj),
                      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,
                      checks=checks,
                      stats=stats)
    view_context_test(ctx, **assertions)
Exemplo n.º 12
0
def test_get_contributors_projects(client, request_users):

    user = request_users["user"]
    if user.username != "nobody":
        client.login(
            username=user.username,
            password=request_users["password"])

    directory = Directory.objects.projects
    response = client.get(
        "/xhr/stats/contributors/?path=%s" % directory.pootle_path,
        HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    assert response.status_code == 200
    result = json.loads(response.content)
    user_projects = Project.accessible_by_user(user)
    user_projects = (
        Project.objects.for_user(user)
                       .filter(code__in=user_projects))
    top_scorers_data = get_top_scorers_test_data(ProjectSet(user_projects))
    for k, v in result.items():
        assert json.loads(json.dumps(top_scorers_data[k])) == v
Exemplo n.º 13
0
def test_view_projects_browse(site_permissions, site_matrix_with_vfolders,
                              site_matrix_with_announcements,
                              client, nobody, default):
    response = client.get(reverse("pootle-projects-browse"))
    assert response.cookies["pootle-language"].value == "projects"
    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))
    ob = ProjectSet(user_projects)
    items = [
        make_project_list_item(project)
        for project in ob.children]
    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': 'projects',
        'fields': table_fields,
        'headings': get_table_headings(table_fields),
        'items': items}
    assertions = dict(
        page="browse",
        pootle_path="/projects/",
        resource_path="",
        resource_path_parts=[],
        object=ob,
        table=table,
        browser_extends="projects/all/base.html",
        stats=jsonify(ob.get_stats()),
        check_categories=get_qualitycheck_schema(ob),
        translation_states=get_translation_states(ob),
        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'))
    view_context_test(ctx, **assertions)
Exemplo n.º 14
0
def test_view_projects_browse(
    site_permissions, site_matrix_with_vfolders, site_matrix_with_announcements, client, nobody, default
):
    response = client.get(reverse("pootle-projects-browse"))
    assert response.cookies["pootle-language"].value == "projects"
    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)
    ob = ProjectSet(user_projects)
    items = [make_project_list_item(project) for project in ob.children]
    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": "projects", "fields": table_fields, "headings": get_table_headings(table_fields), "items": items}
    assertions = dict(
        page="browse",
        pootle_path="/projects/",
        resource_path="",
        resource_path_parts=[],
        resource_obj=ob,
        table=table,
        browser_extends="projects/all/base.html",
        stats=jsonify(ob.get_stats()),
        check_categories=get_qualitycheck_schema(ob),
        translation_states=get_translation_states(ob),
        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"),
    )
    view_context_test(ctx, **assertions)
Exemplo n.º 15
0
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 {})
Exemplo n.º 16
0
def project_set():
    from pootle_project.models import Project, ProjectSet

    return ProjectSet(Project.objects.exclude(disabled=True))
Exemplo n.º 17
0
    def wrapped(request, *args, **kwargs):
        if request.is_ajax():
            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)
            kwargs['dir_path'] = dir_path
            kwargs['filename'] = filename

            # Remove potentially present but unwanted args
            try:
                del kwargs['language_code']
                del kwargs['project_code']
            except KeyError:
                pass
        else:
            language_code = kwargs.pop('language_code', None)
            project_code = kwargs.pop('project_code', None)

        if language_code and project_code:
            try:
                path_obj = TranslationProject.objects.get_for_user(
                    user=request.user,
                    language_code=language_code,
                    project_code=project_code,
                )
            except TranslationProject.DoesNotExist:
                path_obj = None

            if path_obj is None:
                if not request.is_ajax():
                    # Explicit selection via the UI: redirect either to
                    # ``/language_code/`` or ``/projects/project_code/``
                    user_choice = request.COOKIES.get('user-choice', None)
                    if user_choice and user_choice in ('language', 'project',):
                        url = {
                            'language': reverse('pootle-language-browse',
                                                args=[language_code]),
                            'project': reverse('pootle-project-browse',
                                               args=[project_code, '', '']),
                        }
                        response = redirect(url[user_choice])
                        response.delete_cookie('user-choice')

                        return response

                raise Http404
        elif language_code:
            user_projects = Project.accessible_by_user(request.user)
            language = get_object_or_404(Language, code=language_code)
            children = language.children \
                               .filter(project__code__in=user_projects)
            language.set_children(children)
            path_obj = language
        elif project_code:
            try:
                path_obj = Project.objects.get_for_user(project_code,
                                                        request.user)
            except Project.DoesNotExist:
                raise Http404
        else:  # No arguments: all user-accessible projects
            user_projects = Project.accessible_by_user(request.user)
            user_projects = Project.objects.for_user(request.user) \
                                           .filter(code__in=user_projects)

            path_obj = ProjectSet(user_projects)

        request.ctx_obj = path_obj
        request.ctx_path = path_obj.pootle_path
        request.resource_obj = path_obj
        request.pootle_path = path_obj.pootle_path

        return func(request, path_obj, *args, **kwargs)
Exemplo n.º 18
0
Arquivo: index.py Projeto: ii0/pootle
 def project_set(self):
     user_projects = Project.accessible_by_user(self.request.user)
     user_projects = (
         Project.objects.for_user(self.request.user)
                        .filter(code__in=user_projects))
     return ProjectSet(user_projects)