def clean(self, value): if not value: return try: return checksum_to_hash(value) except ValueError: raise ValidationError(_('Invalid checksum specified!'))
def test_checksum(self): """Hash to checksum conversion""" text_hash = calculate_hash(None, 'Message') checksum = hash_to_checksum(text_hash) self.assertEqual( text_hash, checksum_to_hash(checksum) )
def clean(self, value): super(ChecksumField, self).clean(value) if not value: return None try: return checksum_to_hash(value) except ValueError: raise ValidationError(_('Invalid checksum specified!'))
def review_source(request, project, component): """Listing of source strings to review.""" obj, source = get_source(request, project, component) # Grab search type and page number rqtype = request.GET.get('type', 'all') page, limit = get_page_limit(request, 50) try: id_hash = checksum_to_hash(request.GET.get('checksum', '')) except ValueError: id_hash = None ignored = 'ignored' in request.GET expand = False query_string = {'type': rqtype} if ignored: query_string['ignored'] = 'true' # Filter units: if id_hash: sources = source.unit_set.filter(id_hash=id_hash) expand = True else: sources = source.unit_set.filter_type( rqtype, source.component.project, source.language, ignored ) paginator = Paginator(sources, limit) try: sources = paginator.page(page) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. sources = paginator.page(paginator.num_pages) return render( request, 'source-review.html', { 'object': obj, 'project': obj.project, 'source': source, 'page_obj': sources, 'query_string': urlencode(query_string), 'ignored': ignored, 'expand': expand, 'title': _('Review source strings in %s') % force_text(obj), } )
def review_source(request, project, subproject): """Listing of source strings to review.""" obj, source = get_source(request, project, subproject) # Grab search type and page number rqtype = request.GET.get('type', 'all') page, limit = get_page_limit(request, 50) try: id_hash = checksum_to_hash(request.GET.get('checksum', '')) except ValueError: id_hash = None ignored = 'ignored' in request.GET expand = False query_string = {'type': rqtype} if ignored: query_string['ignored'] = 'true' # Filter units: if id_hash: sources = source.unit_set.filter(id_hash=id_hash) expand = True else: sources = source.unit_set.filter_type( rqtype, source.subproject.project, source.language, ignored ) paginator = Paginator(sources, limit) try: sources = paginator.page(page) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. sources = paginator.page(paginator.num_pages) return render( request, 'source-review.html', { 'object': obj, 'project': obj.project, 'source': source, 'page_obj': sources, 'query_string': urlencode(query_string), 'ignored': ignored, 'expand': expand, 'title': _('Review source strings in %s') % force_text(obj), } )
def clean_checksum(self): ''' Validates whether checksum is valid and fetches unit for it. ''' if 'checksum' not in self.cleaned_data: return unit_set = self.translation.unit_set try: self.cleaned_data['unit'] = unit_set.filter( id_hash=checksum_to_hash(self.cleaned_data['checksum']))[0] except (Unit.DoesNotExist, IndexError, ValueError): LOGGER.error('message %s disappeared!', self.cleaned_data['checksum']) raise ValidationError( _('Message you wanted to translate is no longer available!'))
def review_source(request, project, component): """Listing of source strings to review.""" obj, source = get_source(request, project, component) # Grab search type and page number rqtype = request.GET.get('type', 'all') try: id_hash = checksum_to_hash(request.GET.get('checksum', '')) except ValueError: id_hash = None ignored = 'ignored' in request.GET expand = False query_string = {'type': rqtype} if ignored: query_string['ignored'] = 'true' # Filter units: if id_hash: sources = source.unit_set.filter(id_hash=id_hash) expand = True else: sources = source.unit_set.filter_type( rqtype, source.component.project, source.language, ignored ) sources = get_paginator(request, sources) return render( request, 'source-review.html', { 'object': obj, 'project': obj.project, 'source': source, 'page_obj': sources, 'query_string': urlencode(query_string), 'ignored': ignored, 'expand': expand, 'title': _('Review source strings in %s') % force_text(obj), } )
def get_detail(request, project, component, checksum): """Return source translation detail in all languages.""" component = get_component(request, project, component) try: units = Unit.objects.filter( id_hash=checksum_to_hash(checksum), translation__component=component ).order_by(*Unit.ordering) except ValueError: raise Http404('Non existing unit!') try: source = units[0].source_info except IndexError: raise Http404('Non existing unit!') check_flags = [ (CHECKS[x].ignore_string, CHECKS[x].name) for x in CHECKS ] extra_flags = [(x, EXTRA_FLAGS[x]) for x in EXTRA_FLAGS] return render( request, 'js/detail.html', { 'units': units, 'source': source, 'project': component.project, 'next': request.GET.get('next', ''), 'priority_form': PriorityForm( initial={'priority': source.priority} ), 'context_form': ContextForm( initial={'context': source.context} ), 'check_flags_form': CheckFlagsForm( initial={'flags': source.check_flags} ), 'screenshot_form': ScreenshotForm(), 'extra_flags': extra_flags, 'check_flags': check_flags, } )
def get_detail(request, project, component, checksum): """Return source translation detail in all languages.""" component = get_component(request, project, component) try: units = Unit.objects.filter( id_hash=checksum_to_hash(checksum), translation__component=component ) except ValueError: raise Http404('Non existing unit!') try: source = units[0].source_info except IndexError: raise Http404('Non existing unit!') check_flags = [ (CHECKS[x].ignore_string, CHECKS[x].name) for x in CHECKS ] extra_flags = [(x, EXTRA_FLAGS[x]) for x in EXTRA_FLAGS] return render( request, 'js/detail.html', { 'units': units, 'source': source, 'project': component.project, 'next': request.GET.get('next', ''), 'priority_form': PriorityForm( initial={'priority': source.priority} ), 'context_form': ContextForm( initial={'context': source.context} ), 'check_flags_form': CheckFlagsForm( initial={'flags': source.check_flags} ), 'screenshot_form': ScreenshotForm(), 'extra_flags': extra_flags, 'check_flags': check_flags, } )
def test_checksum(self): """Hash to checksum conversion.""" text_hash = calculate_hash("Message") checksum = hash_to_checksum(text_hash) self.assertEqual(checksum, "f5351ff85ab23173") self.assertEqual(text_hash, checksum_to_hash(checksum))
def search(translation, request): """Perform search or returns cached search results.""" # Already performed search if 'sid' in request.GET: # Grab from session storage search_id = 'search_{0}'.format(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) search_result = copy.copy(request.session[search_id]) if 'params' in search_result: search_result['form'] = SearchForm(search_result['params']) else: search_result['form'] = SearchForm() return search_result # Possible new search search_form = SearchForm(request.GET) review_form = ReviewForm(request.GET) search_query = None if 'date' in 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 else: show_form_errors(request, review_form) # Filtering by type allunits = translation.unit_set.all() name = _('All strings') 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 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( id_hash=checksum_to_hash(request.GET['checksum']))[0] offset = unit_ids.index(unit.id) except (Unit.DoesNotExist, IndexError, ValueError): 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 = { 'params': request.GET, 'query': search_query, 'name': force_text(name), 'ids': unit_ids, 'search_id': search_id, 'ttl': int(time.time()) + 86400, 'offset': offset, } request.session['search_{0}'.format(search_id)] = search_result search_result = copy.copy(search_result) search_result['form'] = search_form return search_result