本文整理汇总了Python中weblate.trans.forms.SearchForm类的典型用法代码示例。如果您正苦于以下问题:Python SearchForm类的具体用法?Python SearchForm怎么用?Python SearchForm使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SearchForm类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: search
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
)
示例2: search
def search(translation, request):
"""Perform search or returns cached search results."""
# Possible new search
form = SearchForm(request.GET)
# Process form
if not form.is_valid():
show_form_errors(request, form)
search_result = {
'form': form,
'offset': form.cleaned_data.get('offset', 0),
'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:
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()
name = form.get_name()
# 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,
'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
示例3: download_translation_format
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)
示例4: parse_search_url
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
)
示例5: search
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
)
示例6: search
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
示例7: search
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
示例8: search
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