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_to_response("search.html", RequestContext(request, context))
def show_translation(request, project, subproject, lang): obj = get_translation(request, project, subproject, lang) last_changes = Change.objects.filter( translation=obj).order_by('-timestamp')[:10] # Check locks obj.is_locked(request) # How much is user allowed to configure upload? if request.user.has_perm('trans.author_translation'): form = ExtraUploadForm() elif request.user.has_perm('trans.overwrite_translation'): form = UploadForm() else: form = SimpleUploadForm() # Is user allowed to do automatic translation? if request.user.has_perm('trans.automatic_translation'): autoform = AutoForm(obj) else: autoform = None # Search form for everybody search_form = SearchForm() # Review form for logged in users if request.user.is_anonymous(): review_form = None else: review_form = ReviewForm( initial={ 'date': datetime.date.today() - datetime.timedelta(days=31) }) return render_to_response( 'translation.html', RequestContext( request, { 'object': obj, 'form': form, 'autoform': autoform, 'search_form': search_form, 'review_form': review_form, 'last_changes': last_changes, 'last_changes_url': urlencode(obj.get_kwargs()), 'last_changes_rss': reverse( 'rss-translation', kwargs=obj.get_kwargs(), ), }))
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_to_response( 'search.html', RequestContext(request, context) )
def __init__(self, request): ''' Parses request and fills in search options. ''' # Default values self.query = '' self.type = 'ftx' self.source = True self.target = True self.context = False self.url = '' # Search type self.rqtype = request.REQUEST.get('type', 'all') # Browsing direction self.direction = request.REQUEST.get('dir', 'forward') # Current position pos = request.REQUEST.get('pos', '-1') try: self.pos = int(pos) except: self.pos = -1 # Process search form if request.method == 'POST': search_form = SearchForm(request.POST) else: search_form = SearchForm(request.GET) if search_form.is_valid(): self.load_form(search_form) # Include date for review search if 'date' in request.REQUEST: self.url += '&date=%s' % request.REQUEST['date']
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 HttpResponseRedirect(translation.get_absolute_url()) 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'], search_form.cleaned_data['q'], search_form.cleaned_data['src'], search_form.cleaned_data['ctx'], search_form.cleaned_data['tgt'], ) 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 HttpResponseRedirect(translation.get_absolute_url()) # 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 HttpResponseRedirect(translation.get_absolute_url()) # Remove old search results cleanup_session(request.session) # 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
def search(translation, request): ''' Performs search or retuns 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 HttpResponseRedirect(translation.get_absolute_url()) return request.session[search_id] # Possible new search rqtype = request.GET.get('type', 'all') search_form = SearchForm(request.GET) review_form = ReviewForm(request.GET) 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'], search_form.cleaned_data['q'], search_form.cleaned_data['src'], search_form.cleaned_data['ctx'], search_form.cleaned_data['tgt'], ) name = get_search_name( search_form.cleaned_data['search'], search_form.cleaned_data['q'], ) else: # Filtering by type allunits = translation.unit_set.filter_type(rqtype, translation) 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 HttpResponseRedirect(translation.get_absolute_url()) # 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 HttpResponseRedirect(translation.get_absolute_url()) # Remove old search results cleanup_session(request.session) # Store in cache and return search_id = str(uuid.uuid1()) search_result = { '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
def translate(request, project, subproject, lang): ''' Generic entry point for translating, suggesting and searching. ''' obj = get_translation(request, project, subproject, lang) # Check locks project_locked, user_locked, own_lock = obj.is_locked(request, True) locked = project_locked or user_locked # Search results search_result = search(obj, request) # Handle redirects if isinstance(search_result, HttpResponse): return search_result # Get numer of results num_results = len(search_result['ids']) # Search offset try: offset = int(request.GET.get('offset', search_result.get('offset', 0))) except ValueError: offset = 0 # Check boundaries if offset < 0 or offset >= num_results: messages.info(request, _('You have reached end of translating.')) # Delete search del request.session['search_%s' % search_result['search_id']] # Redirect to translation return HttpResponseRedirect(obj.get_absolute_url()) # Some URLs we will most likely use base_unit_url = '%s?sid=%s&offset=' % ( obj.get_translate_url(), search_result['search_id'], ) this_unit_url = base_unit_url + str(offset) next_unit_url = base_unit_url + str(offset + 1) response = None # Any form submitted? if request.method == 'POST' and not project_locked: response = handle_translate( obj, request, user_locked, this_unit_url, next_unit_url ) # Handle translation merging elif 'merge' in request.GET and not locked: response = handle_merge(obj, request, next_unit_url) # Handle accepting/deleting suggestions elif not locked and ('accept' in request.GET or 'delete' in request.GET): response = handle_suggestions(obj, request, this_unit_url) # Pass possible redirect further if response is not None: return response # Grab actual unit try: unit = obj.unit_set.get(pk=search_result['ids'][offset]) except Unit.DoesNotExist: # Can happen when using SID for other translation messages.error(request, _('Invalid search string!')) return HttpResponseRedirect(obj.get_absolute_url()) # Show secondary languages for logged in users if request.user.is_authenticated(): profile = request.user.get_profile() secondary_langs = profile.secondary_languages.exclude( id=unit.translation.language.id ) project = unit.translation.subproject.project secondary = get_distinct_translations( Unit.objects.filter( checksum=unit.checksum, translated=True, translation__subproject__project=project, translation__language__in=secondary_langs, ) ) antispam = None else: secondary = None antispam = AntispamForm() # Prepare form form = TranslationForm(initial={ 'checksum': unit.checksum, 'target': (unit.translation.language, unit.get_target_plurals()), 'fuzzy': unit.fuzzy, }) return render_to_response( 'translate.html', RequestContext( request, { 'this_unit_url': this_unit_url, 'first_unit_url': base_unit_url + '0', 'last_unit_url': base_unit_url + str(num_results - 1), 'next_unit_url': next_unit_url, 'prev_unit_url': base_unit_url + str(offset - 1), 'object': obj, 'unit': unit, 'last_changes': unit.change_set.all()[:10], 'last_changes_rss': reverse( 'rss-translation', kwargs=obj.get_kwargs(), ), 'last_changes_url': urlencode(obj.get_kwargs()), 'total': obj.unit_set.all().count(), 'search_id': search_result['search_id'], 'offset': offset, 'filter_name': search_result['name'], 'filter_count': num_results, 'filter_pos': offset + 1, 'form': form, 'antispam': antispam, 'comment_form': CommentForm(), 'search_form': SearchForm(), 'update_lock': own_lock, 'secondary': secondary, 'locked': locked, 'user_locked': user_locked, 'project_locked': project_locked, }, ) )
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 HttpResponseRedirect(translation.get_absolute_url()) 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"], search_form.cleaned_data["q"], search_form.cleaned_data["src"], search_form.cleaned_data["ctx"], search_form.cleaned_data["tgt"], ) search_query = search_form.cleaned_data["q"] name = get_search_name(search_form.cleaned_data["search"], search_query) else: # 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 HttpResponseRedirect(translation.get_absolute_url()) # 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 HttpResponseRedirect(translation.get_absolute_url()) # Remove old search results cleanup_session(request.session) # 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