当前位置: 首页>>代码示例>>Python>>正文


Python SearchForm.is_valid方法代码示例

本文整理汇总了Python中weblate.trans.forms.SearchForm.is_valid方法的典型用法代码示例。如果您正苦于以下问题:Python SearchForm.is_valid方法的具体用法?Python SearchForm.is_valid怎么用?Python SearchForm.is_valid使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在weblate.trans.forms.SearchForm的用法示例。


在下文中一共展示了SearchForm.is_valid方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: search

# 需要导入模块: from weblate.trans.forms import SearchForm [as 别名]
# 或者: from weblate.trans.forms.SearchForm import is_valid [as 别名]
def search(translation, request):
    """Perform search or returns cached search results."""
    # Possible new search
    form = SearchForm(request.GET)

    # Process form
    form_valid = form.is_valid()
    if not form_valid:
        show_form_errors(request, form)

    search_result = {
        'form': form,
        'offset': form.cleaned_data.get('offset', 1),
        'checksum': form.cleaned_data.get('checksum'),
    }
    search_url = form.urlencode()
    session_key = 'search_{0}_{1}'.format(translation.pk, search_url)

    if (session_key in request.session and
            'offset' in request.GET and
            'items' in request.session[session_key]):
        search_result.update(request.session[session_key])
        return search_result

    allunits = translation.unit_set.search(
        form.cleaned_data,
        translation=translation,
    )

    search_query = form.get_search_query() if form_valid else ''
    name = form.get_name() if form_valid else ''

    # Grab unit IDs
    unit_ids = list(allunits.values_list('id', flat=True))

    # Check empty search results
    if not unit_ids:
        messages.warning(request, _('No string matched your search!'))
        return redirect(translation)

    # Remove old search results
    cleanup_session(request.session)

    store_result = {
        'query': search_query,
        'url': search_url,
        'items': form.items(),
        'key': session_key,
        'name': force_text(name),
        'ids': unit_ids,
        'ttl': int(time.time()) + 86400,
    }
    request.session[session_key] = store_result

    search_result.update(store_result)
    return search_result
开发者ID:nijel,项目名称:weblate,代码行数:58,代码来源:edit.py

示例2: search

# 需要导入模块: from weblate.trans.forms import SearchForm [as 别名]
# 或者: from weblate.trans.forms.SearchForm import is_valid [as 别名]
def search(request):
    """
    Performs site-wide search on units.
    """
    search_form = SearchForm(request.GET)
    context = {
        'search_form': search_form,
    }

    if search_form.is_valid():
        units = Unit.objects.search(
            None,
            search_form.cleaned_data,
        ).select_related(
            'translation',
        )

        # Filter results by ACL
        acl_projects, filtered = Project.objects.get_acl_status(request.user)
        if filtered:
            units = units.filter(
                translation__subproject__project__in=acl_projects
            )

        limit = request.GET.get('limit', 50)
        page = request.GET.get('page', 1)

        paginator = Paginator(units, limit)

        try:
            units = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            units = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver last page of
            # results.
            units = paginator.page(paginator.num_pages)

        context['page_obj'] = units
        context['title'] = _('Search for %s') % (
            search_form.cleaned_data['q']
        )
        context['query_string'] = search_form.urlencode()
        context['search_query'] = search_form.cleaned_data['q']
    else:
        messages.error(request, _('Invalid search query!'))

    return render(
        request,
        'search.html',
        context
    )
开发者ID:Insoleet,项目名称:weblate,代码行数:55,代码来源:basic.py

示例3: download_translation_format

# 需要导入模块: from weblate.trans.forms import SearchForm [as 别名]
# 或者: from weblate.trans.forms.SearchForm import is_valid [as 别名]
def download_translation_format(request, project, component, lang, fmt):
    obj = get_translation(request, project, component, lang)

    form = SearchForm(request.GET)
    if form.is_valid():
        units = obj.unit_set.search(
            form.cleaned_data,
            translation=obj,
        )
    else:
        units = obj.unit_set.all()

    return download_translation_file(obj, fmt, units)
开发者ID:dsnoeck,项目名称:weblate,代码行数:15,代码来源:files.py

示例4: parse_search_url

# 需要导入模块: from weblate.trans.forms import SearchForm [as 别名]
# 或者: from weblate.trans.forms.SearchForm import is_valid [as 别名]
def parse_search_url(request):
    # Check where we are
    rqtype = request.REQUEST.get('type', 'all')
    direction = request.REQUEST.get('dir', 'forward')
    pos = request.REQUEST.get('pos', '-1')
    try:
        pos = int(pos)
    except:
        pos = -1

    # Pre-process search form
    if request.method == 'POST':
        s = SearchForm(request.POST)
    else:
        s = SearchForm(request.GET)
    if s.is_valid():
        search_query = s.cleaned_data['q']
        search_exact = s.cleaned_data['exact']
        search_source = s.cleaned_data['src']
        search_target = s.cleaned_data['tgt']
        search_context = s.cleaned_data['ctx']
        search_url = '&q=%s&src=%s&tgt=%s&ctx=%s&exact=%s' % (
            search_query,
            bool2str(search_source),
            bool2str(search_target),
            bool2str(search_context),
            bool2str(search_exact),
        )
    else:
        search_query = ''
        search_exact = False
        search_source = True
        search_target = True
        search_context = True
        search_url = ''

    if 'date' in request.REQUEST:
        search_url += '&date=%s' % request.REQUEST['date']

    return (
        rqtype,
        direction,
        pos,
        search_query,
        search_exact,
        search_source,
        search_target,
        search_context,
        search_url
        )
开发者ID:aputtu,项目名称:weblate,代码行数:52,代码来源:views.py

示例5: search

# 需要导入模块: from weblate.trans.forms import SearchForm [as 别名]
# 或者: from weblate.trans.forms.SearchForm import is_valid [as 别名]
def search(request):
    '''
    Performs sitewide search on units.
    '''
    search_form = SearchForm(request.GET)
    context = {
        'search_form': search_form,
    }

    if search_form.is_valid():
        units = Unit.objects.search(
            search_form.cleaned_data,
        ).select_related(
            'translation',
        )

        limit = request.GET.get('limit', 50)
        page = request.GET.get('page', 1)

        paginator = Paginator(units, limit)

        try:
            units = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            units = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver last page of
            # results.
            units = paginator.page(paginator.num_pages)

        context['units'] = units
        context['title'] = _('Search for %s') % (
            search_form.cleaned_data['q']
        )
        context['query_string'] = search_form.urlencode()
        context['search_query'] = search_form.cleaned_data['q']
    else:
        messages.error(request, _('Invalid search query!'))

    return render(
        request,
        'search.html',
        context
    )
开发者ID:ujdhesa,项目名称:weblate,代码行数:47,代码来源:basic.py

示例6: search

# 需要导入模块: from weblate.trans.forms import SearchForm [as 别名]
# 或者: from weblate.trans.forms.SearchForm import is_valid [as 别名]
def search(translation, request):
    '''
    Performs search or returns cached search results.
    '''

    # Already performed search
    if 'sid' in request.GET:
        # Grab from session storage
        search_id = 'search_%s' % request.GET['sid']

        # Check if we know the search
        if search_id not in request.session:
            messages.error(request, _('Invalid search string!'))
            return redirect(translation)

        return request.session[search_id]

    # Possible new search
    search_form = SearchForm(request.GET)
    review_form = ReviewForm(request.GET)

    search_query = None
    if review_form.is_valid():
        # Review
        allunits = translation.unit_set.review(
            review_form.cleaned_data['date'],
            request.user
        )

        formatted_date = formats.date_format(
            review_form.cleaned_data['date'],
            'SHORT_DATE_FORMAT'
        )
        name = _('Review of translations since %s') % formatted_date
    elif search_form.is_valid():
        # Apply search conditions
        allunits = translation.unit_set.search(
            translation,
            search_form.cleaned_data,
        )

        search_query = search_form.cleaned_data['q']
        name = search_form.get_name()
    else:
        # Error reporting
        if 'date' in request.GET:
            show_form_errors(request, review_form)
        elif 'q' in request.GET or 'type' in request.GET:
            show_form_errors(request, search_form)

        # Filtering by type
        allunits = translation.unit_set.all()
        name = _('All strings')

    # Grab unit IDs
    unit_ids = list(allunits.values_list('id', flat=True))

    # Check empty search results
    if len(unit_ids) == 0:
        messages.warning(request, _('No string matched your search!'))
        return redirect(translation)

    # Checksum unit access
    offset = 0
    if 'checksum' in request.GET:
        try:
            unit = allunits.filter(checksum=request.GET['checksum'])[0]
            offset = unit_ids.index(unit.id)
        except (Unit.DoesNotExist, IndexError):
            messages.warning(request, _('No string matched your search!'))
            return redirect(translation)

    # Remove old search results
    cleanup_session(request.session)

    if name is not None:
        name = unicode(name)

    # Store in cache and return
    search_id = str(uuid.uuid1())
    search_result = {
        'query': search_query,
        'name': name,
        'ids': unit_ids,
        'search_id': search_id,
        'ttl': int(time.time()) + 86400,
        'offset': offset,
    }

    request.session['search_%s' % search_id] = search_result

    return search_result
开发者ID:chervol,项目名称:weblate,代码行数:94,代码来源:edit.py

示例7: search

# 需要导入模块: from weblate.trans.forms import SearchForm [as 别名]
# 或者: from weblate.trans.forms.SearchForm import is_valid [as 别名]
def search(translation, request):
    """
    Performs search or returns cached search results.
    """

    # Already performed search
    if "sid" in request.GET:
        # Grab from session storage
        search_id = "search_%s" % request.GET["sid"]

        # Check if we know the search
        if search_id not in request.session:
            messages.error(request, _("Invalid search string!"))
            return redirect(translation)

        return request.session[search_id]

    # Possible new search
    rqtype = request.GET.get("type", "all")

    search_form = SearchForm(request.GET)
    review_form = ReviewForm(request.GET)

    search_query = None
    if review_form.is_valid():
        # Review
        allunits = translation.unit_set.review(review_form.cleaned_data["date"], request.user)

        formatted_date = formats.date_format(review_form.cleaned_data["date"], "SHORT_DATE_FORMAT")
        name = _("Review of translations since %s") % formatted_date
    elif search_form.is_valid():
        # Apply search conditions
        allunits = translation.unit_set.search(search_form.cleaned_data)

        search_query = search_form.cleaned_data["q"]
        name = get_search_name(search_form.cleaned_data["search"], search_query)
    else:
        # Error reporting
        if "date" in request.GET:
            show_form_errors(request, review_form)
        elif "q" in request.GET:
            show_form_errors(request, search_form)

        # Filtering by type
        allunits = translation.unit_set.filter_type(rqtype, translation, ignored="ignored" in request.GET)

        name = get_filter_name(rqtype)

    # Grab unit IDs
    unit_ids = list(allunits.values_list("id", flat=True))

    # Check empty search results
    if len(unit_ids) == 0:
        messages.warning(request, _("No string matched your search!"))
        return redirect(translation)

    # Checksum unit access
    offset = 0
    if "checksum" in request.GET:
        try:
            unit = allunits.filter(checksum=request.GET["checksum"])[0]
            offset = unit_ids.index(unit.id)
        except (Unit.DoesNotExist, IndexError):
            messages.warning(request, _("No string matched your search!"))
            return redirect(translation)

    # Remove old search results
    cleanup_session(request.session)

    # Store in cache and return
    search_id = str(uuid.uuid1())
    search_result = {
        "query": search_query,
        "name": unicode(name),
        "ids": unit_ids,
        "search_id": search_id,
        "ttl": int(time.time()) + 86400,
        "offset": offset,
    }

    request.session["search_%s" % search_id] = search_result

    return search_result
开发者ID:paour,项目名称:weblate,代码行数:85,代码来源:edit.py

示例8: search

# 需要导入模块: from weblate.trans.forms import SearchForm [as 别名]
# 或者: from weblate.trans.forms.SearchForm import is_valid [as 别名]
def search(translation, request):
    """Perform search or returns cached search results."""
    # Possible new search
    search_form = SearchForm(request.GET)
    review_form = ReviewForm(request.GET)

    # Process form
    if 'date' in request.GET:
        if review_form.is_valid():
            form = review_form
        else:
            show_form_errors(request, review_form)
            # Use blank form
            form = SearchForm([])
            form.is_valid()
    elif search_form.is_valid():
        form = search_form
    else:
        show_form_errors(request, search_form)
        # Use blank form
        form = SearchForm([])
        form.is_valid()

    search_result = {
        'form': form,
        'offset': form.cleaned_data['offset'],
        'checksum': form.cleaned_data['checksum'],
    }
    search_url = form.urlencode()
    session_key = 'search_{0}_{1}'.format(translation.pk, search_url)

    if session_key in request.session:
        search_result.update(request.session[session_key])
        return search_result

    if form.cleaned_data['type'] == 'review':
        allunits = translation.unit_set.review(
            form.cleaned_data['date'],
            request.user
        )
    else:
        allunits = translation.unit_set.search(
            translation,
            form.cleaned_data,
        )
        if form.cleaned_data['type'] == 'random':
            allunits = allunits[:25]

    search_query = form.get_search_query()
    name = form.get_name()

    # Grab unit IDs
    unit_ids = list(allunits.values_list('id', flat=True))

    # Check empty search results
    if len(unit_ids) == 0:
        messages.warning(request, _('No string matched your search!'))
        return redirect(translation)

    # Remove old search results
    cleanup_session(request.session)

    store_result = {
        'query': search_query,
        'url': search_url,
        'key': session_key,
        'name': force_text(name),
        'ids': unit_ids,
        'ttl': int(time.time()) + 86400,
    }
    request.session[session_key] = store_result

    search_result.update(store_result)
    return search_result
开发者ID:saily,项目名称:weblate,代码行数:76,代码来源:edit.py


注:本文中的weblate.trans.forms.SearchForm.is_valid方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。