def get_units(request): """Gets source and target texts and its metadata. :return: A JSON-encoded string containing the source and target texts grouped by the store they belong to. The optional `count` GET parameter defines the chunk size to consider. The user's preference will be used by default. When the `initial` GET parameter is present, a sorted list of the result set ids will be returned too. """ search_form = UnitSearchForm(request.GET, user=request.user) if not search_form.is_valid(): errors = search_form.errors.as_data() if "path" in errors: for error in errors["path"]: if error.code == "max_length": raise Http400(_('Path too long.')) elif error.code == "required": raise Http400(_('Arguments missing.')) raise Http404(forms.ValidationError(search_form.errors).messages) total, start, end, units_qs = search_backend.get(Unit)( request.user, **search_form.cleaned_data).search() return JsonResponse({ 'start': start, 'end': end, 'total': total, 'unitGroups': GroupedResults(units_qs).data })
def get_units(request): """Gets source and target texts and its metadata. :return: A JSON-encoded string containing the source and target texts grouped by the store they belong to. The optional `count` GET parameter defines the chunk size to consider. The user's preference will be used by default. When the `initial` GET parameter is present, a sorted list of the result set ids will be returned too. """ search_form = UnitSearchForm(request.GET, user=request.user) if not search_form.is_valid(): errors = search_form.errors.as_data() if "path" in errors: for error in errors["path"]: if error.code == "max_length": raise Http400(_('Path too long.')) elif error.code == "required": raise Http400(_('Arguments missing.')) raise Http404(forms.ValidationError(search_form.errors).messages) total, start, end, units_qs = search_backend.get(Unit)( request.user, **search_form.cleaned_data).search() return JsonResponse( {'start': start, 'end': end, 'total': total, 'unitGroups': GroupedResults(units_qs).data})
def _test_export_view(tp, request, response, kwargs, settings): ctx = response.context filter_name, filter_extra = get_filter_name(request.GET) form_data = request.GET.copy() form_data["path"] = request.path.replace("export-view/", "") search_form = UnitExportForm( form_data, user=request.user) assert search_form.is_valid() total, start, end, units_qs = search_backend.get(Unit)( request.user, **search_form.cleaned_data).search() units_qs = units_qs.select_related('store') assertions = {} if total > settings.POOTLE_EXPORT_VIEW_LIMIT: units_qs = units_qs[:settings.POOTLE_EXPORT_VIEW_LIMIT] assertions.update( {'unit_total_count': total, 'displayed_unit_count': settings.POOTLE_EXPORT_VIEW_LIMIT}) unit_groups = [ (path, list(units)) for path, units in groupby( units_qs, lambda x: x.store.pootle_path)] assertions.update( dict(project=tp.project, language=tp.language, source_language=tp.project.source_language, filter_name=filter_name, filter_extra=filter_extra, unit_groups=unit_groups)) view_context_test(ctx, **assertions)
def _test_export_view(project, request, response, kwargs, settings): ctx = response.context kwargs["project_code"] = project.code filter_name, filter_extra = get_filter_name(request.GET) form_data = request.GET.copy() form_data["path"] = request.path.replace("export-view/", "") search_form = UnitExportForm(form_data, user=request.user) assert search_form.is_valid() total, start, end, units_qs = search_backend.get(Unit)( request.user, **search_form.cleaned_data).search() units_qs = units_qs.select_related('store') assertions = {} if total > settings.POOTLE_EXPORT_VIEW_LIMIT: units_qs = units_qs[:settings.POOTLE_EXPORT_VIEW_LIMIT] assertions.update({ 'unit_total_count': total, 'displayed_unit_count': settings.POOTLE_EXPORT_VIEW_LIMIT }) unit_groups = [ (path, list(units)) for path, units in groupby(units_qs, lambda x: x.store.pootle_path) ] assertions.update( dict(project=project, language=None, source_language="en", filter_name=filter_name, filter_extra=filter_extra, unit_groups=unit_groups)) view_context_test(ctx, **assertions)
def _test_export_view(tp, request, response, kwargs): ctx = response.context filter_name, filter_extra = get_filter_name(request.GET) form_data = request.GET.copy() form_data["path"] = request.path.replace("export-view/", "") search_form = UnitExportForm( form_data, user=request.user) assert search_form.is_valid() total, start, end, units_qs = search_backend.get(Unit)( request.user, **search_form.cleaned_data).search() units_qs = units_qs.select_related('store') unit_groups = [ (path, list(units)) for path, units in groupby( units_qs, lambda x: x.store.pootle_path)] view_context_test( ctx, **dict( project=tp.project, language=tp.language, source_language=tp.project.source_language, filter_name=filter_name, filter_extra=filter_extra, unit_groups=unit_groups))
def test_view_projects_export(client): response = client.get(reverse("pootle-projects-export")) ctx = response.context request = response.wsgi_request filter_name, filter_extra = get_filter_name(request.GET) form_data = request.GET.copy() form_data["path"] = request.path.replace("export-view/", "") search_form = UnitExportForm( form_data, user=request.user) assert search_form.is_valid() total, start, end, units_qs = search_backend.get(Unit)( request.user, **search_form.cleaned_data).search() units_qs = units_qs.select_related('store') unit_groups = [ (path, list(units)) for path, units in groupby( units_qs, lambda x: x.store.pootle_path)] assertions = dict( project=None, language=None, source_language="en", filter_name=filter_name, filter_extra=filter_extra, unit_groups=unit_groups) view_context_test(ctx, **assertions)
def test_unit_search_backend_custom(): class CustomSearchBackend(DBSearchBackend): pass # add a custom getter, simulating adding before pootle_store # in INSTALLED_APPS # disconnect the default search_backend search_backend.disconnect(get_search_backend, sender=Unit) @getter(search_backend, sender=Unit) def custom_get_search_backend(**kwargs): return CustomSearchBackend # reconnect the default search_backend search_backend.connect(get_search_backend, sender=Unit) assert search_backend.get(Unit) is CustomSearchBackend
def get_uids(request): """Gets all uids based on search criteria :return: A JSON-encoded string containing the sorted list of unit IDs (uids) """ search_form = UnitSearchForm(request.GET, user=request.user) if not search_form.is_valid(): errors = search_form.errors.as_data() if "path" in errors: for error in errors["path"]: if error.code == "max_length": raise Http400(_('Path too long.')) elif error.code == "required": raise Http400(_('Arguments missing.')) raise Http404(forms.ValidationError(search_form.errors).messages) begin, end, total, uids = search_backend.get(Unit)( request.user, **search_form.cleaned_data ).get_uids() last_store_id = None uid_groups = [] group = [] for uid, store_id in uids: if not store_id == last_store_id: if len(group) > 0: uid_groups.append(group) group = [] last_store_id = store_id group.append(uid) if len(group) > 0: uid_groups.append(group) return JsonResponse({ 'begin': begin, 'end': end, 'total': total, 'uids': uid_groups, })
def get_units(request): """Based on the vector of uids and the vector of header uids, return a dictionary of lightweight results for the view rows. :return: A JSON-encoded string containing the dictionary """ form = UnitViewRowsForm(request.GET, user=request.user) if not form.is_valid(): errors = form.errors.as_data() if 'uids' in errors: for error in errors['uids']: if error.code in ['invalid', 'required']: raise Http400(error.message) raise Http404(forms.ValidationError(form.errors).messages) units = search_backend.get(Unit)(request.user, **form.cleaned_data).get_units() return JsonResponse( ViewRowResults(units, form.cleaned_data['headers']).data)
def get_context_data(self, *args, **kwargs): ctx = {} filter_name, filter_extra = get_filter_name(self.request.GET) form_data = self.request.GET.copy() form_data["path"] = self.path search_form = UnitExportForm(form_data, user=self.request.user) if not search_form.is_valid(): raise Http404(ValidationError(search_form.errors).messages) total, start, end, units_qs = search_backend.get(Unit)( self.request.user, **search_form.cleaned_data).search() units_qs = units_qs.select_related('store') if total > settings.POOTLE_EXPORT_VIEW_LIMIT: units_qs = units_qs[:settings.POOTLE_EXPORT_VIEW_LIMIT] ctx.update({ 'unit_total_count': total, 'displayed_unit_count': settings.POOTLE_EXPORT_VIEW_LIMIT }) unit_groups = [ (path, list(units)) for path, units in groupby(units_qs, lambda x: x.store.pootle_path) ] ctx.update({ 'unit_groups': unit_groups, 'filter_name': filter_name, 'filter_extra': filter_extra, 'source_language': self.source_language, 'language': self.language, 'project': self.project }) return ctx
def get_context_data(self, *args, **kwargs): ctx = {} filter_name, filter_extra = get_filter_name(self.request.GET) form_data = self.request.GET.copy() form_data["path"] = self.path search_form = UnitExportForm( form_data, user=self.request.user) if not search_form.is_valid(): raise Http404( ValidationError(search_form.errors).messages) total, start, end, units_qs = search_backend.get(Unit)( self.request.user, **search_form.cleaned_data).search() units_qs = units_qs.select_related('store') if total > settings.POOTLE_EXPORT_VIEW_LIMIT: units_qs = units_qs[:settings.POOTLE_EXPORT_VIEW_LIMIT] ctx.update( {'unit_total_count': total, 'displayed_unit_count': settings.POOTLE_EXPORT_VIEW_LIMIT}) unit_groups = [ (path, list(units)) for path, units in groupby(units_qs, lambda x: x.store.pootle_path)] ctx.update( {'unit_groups': unit_groups, 'filter_name': filter_name, 'filter_extra': filter_extra, 'source_language': self.source_language, 'language': self.language, 'project': self.project}) return ctx
def test_unit_search_backend(): assert search_backend.get() is None assert search_backend.get(Unit) is DBSearchBackend