示例#1
0
def test_vfolder_data_view(tp0, request_users):
    user = request_users["user"]
    dir0 = tp0.directory
    vf_data = vfolders_data_view.get(dir0.__class__)(dir0, user=user)
    assert isinstance(vf_data, VFoldersDataView)
    assert vf_data.context is dir0
    assert isinstance(vf_data.vfolder_data_tool, DirectoryVFDataTool)
    assert vf_data.vfolder_data_tool.context is dir0
    assert (
        vf_data.all_stats
        == vf_data.vfolder_data_tool.get_stats(user=user))
    assert vf_data.stats["children"] == vf_data.all_stats
    # ordering?
    rows = [
        make_vfolder_dict(dir0, *vf)
        for vf
        in vf_data.all_stats.items()]
    for i, row in enumerate(vf_data.table_items):
        assert rows[i] == row
    assert "priority" in vf_data.table_fields
    expected_table_data = dict(
        children=dict(
            id='vfolders',
            fields=vf_data.table_fields,
            headings=get_table_headings(vf_data.table_fields),
            rows=vf_data.table_items))
    assert vf_data.table_data == expected_table_data
示例#2
0
文件: views.py 项目: sjhale/pootle
    def vfolders_data_view(self):
        if 'virtualfolder' not in settings.INSTALLED_APPS:
            return
        from virtualfolder.delegate import vfolders_data_view

        return vfolders_data_view.get(self.object.__class__)(
            self.object, self.request.user)
示例#3
0
文件: tp.py 项目: arky/pootle
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)
示例#4
0
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)
示例#5
0
def test_vfolder_data_checks(subdir0, request_users):
    user = request_users["user"]
    vf_data = vfolders_data_view.get(subdir0.__class__)(subdir0, user=user)
    data_tool = vf_data.vfolder_data_tool
    checks = {}
    cd = data_tool.filter_data(data_tool.checks_data_model)
    if not data_tool.show_all_to(user):
        cd = data_tool.filter_accessible(cd)
    cd = (cd.values_list("store__vfolders", "name").annotate(Sum("count")))
    for vf, name, count in cd:
        checks[vf] = checks.get(vf, {})
        checks[vf][name] = count
    assert checks == data_tool.get_checks(user=user)
示例#6
0
文件: tp.py 项目: phlax/pootle
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)
示例#7
0
def test_vfolder_data_checks(subdir0, request_users):
    user = request_users["user"]
    vf_data = vfolders_data_view.get(subdir0.__class__)(subdir0, user=user)
    data_tool = vf_data.vfolder_data_tool
    checks = {}
    cd = data_tool.filter_data(data_tool.checks_data_model)
    if not data_tool.show_all_to(user):
        cd = data_tool.filter_accessible(cd)
    cd = (cd.values_list("store__vfolders", "name")
            .annotate(Sum("count")))
    for vf, name, count in cd:
        checks[vf] = checks.get(vf, {})
        checks[vf][name] = count
    assert checks == data_tool.get_checks(user=user)
示例#8
0
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)
示例#9
0
def test_vfolder_data_view(tp0, request_users):
    user = request_users["user"]
    dir0 = tp0.directory
    vf_data = vfolders_data_view.get(dir0.__class__)(dir0, user=user)
    assert isinstance(vf_data, VFoldersDataView)
    assert vf_data.context is dir0
    assert isinstance(vf_data.vfolder_data_tool, DirectoryVFDataTool)
    assert vf_data.vfolder_data_tool.context is dir0
    assert (vf_data.all_stats == vf_data.vfolder_data_tool.get_stats(
        user=user))
    assert vf_data.stats["children"] == vf_data.all_stats
    # ordering?
    rows = [make_vfolder_dict(dir0, *vf) for vf in vf_data.all_stats.items()]
    for i, row in enumerate(vf_data.table_items):
        assert rows[i] == row
    expected_table_data = dict(
        children=dict(id='vfolders',
                      fields=vf_data.table_fields,
                      headings=get_table_headings(vf_data.table_fields),
                      items=vf_data.table_items))
    assert vf_data.table_data == expected_table_data
示例#10
0
def _test_browse_view(tp, request, response, kwargs):
    cookie_data = json.loads(
        unquote(response.cookies[SIDEBAR_COOKIE_NAME].value))
    assert cookie_data["foo"] == "bar"
    assert "announcements_projects_%s" % tp.project.code in cookie_data
    assert "announcements_%s" % tp.language.code in cookie_data
    assert ("announcements_%s_%s" % (tp.language.code, tp.project.code)
            in cookie_data)
    ctx = response.context
    kwargs["project_code"] = tp.project.code
    kwargs["language_code"] = tp.language.code
    resource_path = "%(dir_path)s%(filename)s" % kwargs
    pootle_path = "%s%s" % (tp.pootle_path, resource_path)

    if not (kwargs["dir_path"] or kwargs.get("filename")):
        obj = tp.directory
    elif not kwargs.get("filename"):
        obj = Directory.objects.get(pootle_path=pootle_path)
    else:
        obj = Store.objects.get(pootle_path=pootle_path)
    if not kwargs.get("filename"):
        vf_view = vfolders_data_view.get(obj.__class__)(obj, request.user)
        stats = vf_view.stats
        vfolders = stats["vfolders"]
        stats.update(obj.data_tool.get_stats(user=request.user))
    else:
        stats = obj.data_tool.get_stats(user=request.user)
        vfolders = None

    filters = {}
    if vfolders:
        filters['sort'] = 'priority'
    dirs_with_vfolders = set(
        split_pootle_path(path)[2].split("/")[0] for path in tp.stores.filter(
            vfolders__isnull=False).values_list("pootle_path", flat=True))
    directories = [
        make_directory_item(
            child,
            **(dict(
                sort="priority") if child.name in dirs_with_vfolders else {}))
        for child in obj.get_children() if isinstance(child, Directory)
    ]
    stores = [
        make_store_item(child) for child in obj.get_children()
        if isinstance(child, Store)
    ]

    if not kwargs.get("filename"):
        table_fields = [
            'name', 'progress', 'total', 'need-translation', 'suggestions',
            'critical', 'last-updated', 'activity'
        ]
        table = {
            'id': 'tp',
            'fields': table_fields,
            'headings': get_table_headings(table_fields),
            'items': directories + stores
        }
    else:
        table = None

    User = get_user_model()
    top_scorers = User.top_scorers(language=tp.language.code,
                                   project=tp.project.code,
                                   limit=11)
    assertions = dict(
        page="browse",
        object=obj,
        translation_project=tp,
        language=tp.language,
        project=tp.project,
        has_admin_access=check_permission('administrate', request),
        is_store=(kwargs.get("filename") and True or False),
        browser_extends="translation_projects/base.html",
        pootle_path=pootle_path,
        resource_path=resource_path,
        resource_path_parts=get_path_parts(resource_path),
        translation_states=get_translation_states(obj),
        checks=get_qualitycheck_list(obj),
        top_scorers=top_scorers,
        top_scorers_data=get_top_scorers_data(top_scorers, 10),
        url_action_continue=obj.get_translate_url(state='incomplete',
                                                  **filters),
        url_action_fixcritical=obj.get_critical_url(**filters),
        url_action_review=obj.get_translate_url(state='suggestions',
                                                **filters),
        url_action_view_all=obj.get_translate_url(state='all'),
        stats=stats,
        parent=get_parent(obj))
    if table:
        assertions["table"] = table
    sidebar = get_sidebar_announcements_context(request,
                                                (tp.project, tp.language, tp))
    for k in ["has_sidebar", "is_sidebar_open", "announcements"]:
        assertions[k] = sidebar[0][k]
    view_context_test(ctx, **assertions)
    if vfolders:
        for vfolder in ctx["vfolders"]["items"]:
            assert (vfolder["is_grayed"]
                    and not ctx["has_admin_access"]) is False
        assert ctx["vfolders"]["items"] == vf_view.table_items

    assert (('display_download' in ctx
             and ctx['display_download']) == (request.user.is_authenticated()
                                              and check_permission(
                                                  'translate', request)))
示例#11
0
文件: tp.py 项目: phlax/pootle
def _test_browse_view(tp, request, response, kwargs):
    cookie_data = json.loads(
        unquote(response.cookies[SIDEBAR_COOKIE_NAME].value))
    assert cookie_data["foo"] == "bar"
    assert "announcements_projects_%s" % tp.project.code in cookie_data
    assert "announcements_%s" % tp.language.code in cookie_data
    assert (
        "announcements_%s_%s"
        % (tp.language.code, tp.project.code)
        in cookie_data)
    ctx = response.context
    kwargs["project_code"] = tp.project.code
    kwargs["language_code"] = tp.language.code
    resource_path = "%(dir_path)s%(filename)s" % kwargs
    pootle_path = "%s%s" % (tp.pootle_path, resource_path)

    if not (kwargs["dir_path"] or kwargs.get("filename")):
        obj = tp.directory
    elif not kwargs.get("filename"):
        obj = Directory.objects.get(
            pootle_path=pootle_path)
    else:
        obj = Store.objects.get(
            pootle_path=pootle_path)
    if not kwargs.get("filename"):
        vf_view = vfolders_data_view.get(obj.__class__)(obj, request.user)
        stats = vf_view.stats
        vfolders = stats["vfolders"]
        stats.update(obj.data_tool.get_stats(user=request.user))
    else:
        stats = obj.data_tool.get_stats(user=request.user)
        vfolders = None

    filters = {}
    if vfolders:
        filters['sort'] = 'priority'
    dirs_with_vfolders = set(
        split_pootle_path(path)[2].split("/")[0]
        for path
        in tp.stores.filter(
            vfolders__isnull=False).values_list(
            "pootle_path", flat=True))
    directories = [
        make_directory_item(
            child,
            **(dict(sort="priority")
               if child.name in dirs_with_vfolders
               else {}))
        for child in obj.get_children()
        if isinstance(child, Directory)]
    stores = [
        make_store_item(child)
        for child in obj.get_children()
        if isinstance(child, Store)]

    if not kwargs.get("filename"):
        table_fields = [
            'name', 'progress', 'total', 'need-translation',
            'suggestions', 'critical', 'last-updated', 'activity']
        table = {
            'id': 'tp',
            'fields': table_fields,
            'headings': get_table_headings(table_fields),
            'items': directories + stores}
    else:
        table = None

    User = get_user_model()
    top_scorers = User.top_scorers(language=tp.language.code,
                                   project=tp.project.code, limit=11)
    assertions = dict(
        page="browse",
        object=obj,
        translation_project=tp,
        language=tp.language,
        project=tp.project,
        has_admin_access=check_permission('administrate', request),
        is_store=(kwargs.get("filename") and True or False),
        browser_extends="translation_projects/base.html",
        pootle_path=pootle_path,
        resource_path=resource_path,
        resource_path_parts=get_path_parts(resource_path),
        translation_states=get_translation_states(obj),
        checks=get_qualitycheck_list(obj),
        top_scorers=top_scorers,
        top_scorers_data=get_top_scorers_data(top_scorers, 10),
        url_action_continue=obj.get_translate_url(
            state='incomplete', **filters),
        url_action_fixcritical=obj.get_critical_url(**filters),
        url_action_review=obj.get_translate_url(
            state='suggestions', **filters),
        url_action_view_all=obj.get_translate_url(state='all'),
        stats=stats,
        parent=get_parent(obj))
    if table:
        assertions["table"] = table
    sidebar = get_sidebar_announcements_context(
        request, (tp.project, tp.language, tp))
    for k in ["has_sidebar", "is_sidebar_open", "announcements"]:
        assertions[k] = sidebar[0][k]
    view_context_test(ctx, **assertions)
    if vfolders:
        for vfolder in ctx["vfolders"]["items"]:
            assert (vfolder["is_grayed"] and not ctx["has_admin_access"]) is False
        assert ctx["vfolders"]["items"] == vf_view.table_items

    assert (('display_download' in ctx and ctx['display_download']) ==
            (request.user.is_authenticated()
             and check_permission('translate', request)))