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 decorated_f(request): """Loads :cls:`pootle_app.models.Directory` and :cls:`pootle_store.models.Store` models and populates the request object. """ pootle_path = request.GET.get('path', None) if pootle_path is None: raise Http400(_('Arguments missing.')) lang, proj, dir_path, filename = split_pootle_path(pootle_path) store = None if filename: try: store = Store.objects.select_related( 'parent', ).get(pootle_path=pootle_path) directory = store.parent except Store.DoesNotExist: raise Http404(_('Store does not exist.')) else: directory = Directory.objects.get(pootle_path=pootle_path) _check_permissions(request, directory, permission_codes) path_obj = store or directory request.pootle_path = pootle_path return f(request, path_obj)
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. """ pootle_path = request.GET.get('path', None) if pootle_path is None: raise Http400(_('Arguments missing.')) units_qs = Unit.objects.get_for_path(pootle_path, request.user) step_queryset = get_step_query(request, units_qs) is_initial_request = request.GET.get('initial', False) chunk_size = request.GET.get("count", request.user.unit_rows) uids_param = filter(None, request.GET.get('uids', '').split(u',')) uids = filter(None, map(to_int, uids_param)) units = None unit_groups = [] uid_list = [] if is_initial_request: uid_list = list(step_queryset.values_list('id', flat=True)) if len(uids) == 1: try: uid = uids[0] index = uid_list.index(uid) begin = max(index - chunk_size, 0) end = min(index + chunk_size + 1, len(uid_list)) uids = uid_list[begin:end] except ValueError: raise Http404 # `uid` not found in `uid_list` else: count = 2 * chunk_size units = step_queryset[:count] if units is None and uids: units = step_queryset.filter(id__in=uids) units_by_path = groupby(units, lambda x: x.store.pootle_path) for pootle_path, units in units_by_path: unit_groups.append(_path_units_with_meta(pootle_path, units)) response = { 'unitGroups': unit_groups, } if uid_list: response['uIds'] = uid_list return HttpResponse(jsonify(response), mimetype="application/json")
def get_uids(request): """Gets all uids based on search criteria :return: A JSON-encoded string containing the sorted list of unit IDs (uids) """ request_params = request.GET.copy() request_params["include_disabled"] = "all" in request.GET search_form = UnitSearchForm(request_params, 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 = DBSearchBackend( 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_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): """Gets source and target texts and its metadata. :return: A JSON-encoded object containing the source and target texts grouped by the store they belong to. When the ``pager`` GET parameter is present, pager information will be returned too. """ pootle_path = request.GET.get('path', None) if pootle_path is None: raise Http400(_('Arguments missing.')) page = None request.profile = get_profile(request.user) limit = request.profile.get_unit_rows() units_qs = Unit.objects.get_for_path(pootle_path, request.profile) step_queryset = get_step_query(request, units_qs) # Maybe we are trying to load directly a specific unit, so we have # to calculate its page number uid = request.GET.get('uid', None) if uid is not None: try: # XXX: Watch for performance, might want to drop into raw SQL # at some stage uid_list = list(step_queryset.values_list('id', flat=True)) preceding = uid_list.index(int(uid)) page = preceding / limit + 1 except ValueError: pass # uid wasn't a number or not present in the results pager = paginate(request, step_queryset, items=limit, page=page) unit_groups = [] units_by_path = groupby(pager.object_list, lambda x: x.store.pootle_path) for pootle_path, units in units_by_path: unit_groups.append(_path_units_with_meta(pootle_path, units)) response = { 'unit_groups': unit_groups, } if request.GET.get('pager', False): response['pager'] = { 'count': pager.paginator.count, 'number': pager.number, 'num_pages': pager.paginator.num_pages, 'per_page': pager.paginator.per_page, } return HttpResponse(jsonify(response), mimetype="application/json")
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) uid_list, units_qs = get_search_backend()( request.user, **search_form.cleaned_data).search() bad_uid = (search_form.cleaned_data["initial"] and len(search_form.cleaned_data["uids"]) == 1 and search_form.cleaned_data["uids"][0] not in uid_list) if bad_uid: raise Http404 response = {'unitGroups': GroupedResults(units_qs).data} if uid_list: response['uIds'] = uid_list return JsonResponse(response)
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 = DBSearchBackend(request.user, **form.cleaned_data).get_units() return JsonResponse( ViewRowResults(units, form.cleaned_data['headers']).data)
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 """ request_params = request.GET.copy() request_params["include_disabled"] = "all" in request.GET form = UnitViewRowsForm(request_params, 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 = DBSearchBackend(request.user, **form.cleaned_data).get_units() return JsonResponse(ViewRowResults(units, form.cleaned_data["headers"]).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. """ pootle_path = request.GET.get('path', None) if pootle_path is None: raise Http400(_('Arguments missing.')) User = get_user_model() request.profile = User.get(request.user) limit = request.profile.get_unit_rows() units_qs = Unit.objects.get_for_path(pootle_path, request.profile) units_qs = units_qs.select_related( 'store__translation_project__project', 'store__translation_project__language', ) step_queryset = get_step_query(request, units_qs) is_initial_request = request.GET.get('initial', False) chunk_size = request.GET.get('count', limit) uids_param = filter(None, request.GET.get('uids', '').split(u',')) uids = filter(None, map(to_int, uids_param)) units = [] unit_groups = [] uid_list = [] if is_initial_request: sort_by_field = None if len(step_queryset.query.order_by) == 1: sort_by_field = step_queryset.query.order_by[0] sort_on = None for key, item in ALLOWED_SORTS.items(): if sort_by_field in item.values(): sort_on = key break if sort_by_field is None or sort_on == 'units': uid_list = list(step_queryset.values_list('id', flat=True)) else: # Not using `values_list()` here because it doesn't know about all # existing relations when `extra()` has been used before in the # queryset. This affects annotated names such as those ending in # `__max`, where Django thinks we're trying to lookup a field on a # relationship field. That's why `sort_by_field` alias for `__max` # is used here. This alias must be queried in # `values('sort_by_field', 'id')` with `id` otherwise # Django looks for `sort_by_field` field in the initial table. # https://code.djangoproject.com/ticket/19434 uid_list = [ u['id'] for u in step_queryset.values('id', 'sort_by_field') ] if len(uids) == 1: try: uid = uids[0] index = uid_list.index(uid) begin = max(index - chunk_size, 0) end = min(index + chunk_size + 1, len(uid_list)) uids = uid_list[begin:end] except ValueError: raise Http404 # `uid` not found in `uid_list` else: count = 2 * chunk_size uids = uid_list[:count] if not units and uids: units = step_queryset.filter(id__in=uids) units_by_path = groupby(units, lambda x: x.store.pootle_path) for pootle_path, units in units_by_path: unit_groups.append(_path_units_with_meta(pootle_path, units)) response = { 'unitGroups': unit_groups, } if uid_list: response['uIds'] = uid_list return HttpResponse(jsonify(response), content_type="application/json")
def form_invalid(self, form): raise Http400(form.errors)
def get_qualitycheck_stats(request, *args, **kwargs): pootle_path = request.GET.get('path', None) if pootle_path is None: raise Http400(_('Arguments missing.')) (language_code, project_code, dir_path, filename) = split_pootle_path(pootle_path) if language_code and project_code: tp = get_object_or_404( TranslationProject, language__code=language_code, project__code=project_code) tp_path = "/%s%s" % (dir_path, filename) if filename: resource = get_object_or_404( Store, translation_project=tp, tp_path=tp_path) elif tp_path != "/": resource = get_object_or_404( Directory, tp=tp, tp_path=tp_path) else: resource = tp.directory elif language_code: resource = get_object_or_404(Language, code=language_code) elif project_code: project = get_object_or_404(Project, code=project_code) if dir_path or filename: tp_path = "/%s%s" % (dir_path, filename) if not filename: dirs = Directory.objects.live().filter(tp__project=project) if dir_path.count("/"): dirs = dirs.select_related( "parent", "tp", "tp__language") resources = ( dirs.exclude(pootle_path__startswith="/templates") .filter(tp_path=tp_path)) else: resources = ( Store.objects.live() .select_related("translation_project__language") .filter(translation_project__project=project) .filter(tp_path=tp_path)) if resources: resource = ProjectResource( resources, ("/projects/%s%s" % (project.code, tp_path))) else: raise Http404 else: resource = project else: resource = ProjectSet( Project.objects.for_user(request.user).select_related("directory")) failing_checks = resource.data_tool.get_checks() return JsonResponse(failing_checks if failing_checks is not None else {})
def form_invalid(self, form): if form.non_field_errors(): raise Http404 raise Http400(form.errors)