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(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