def entities(request): """Get entities for the specified project, locale and paths.""" form = forms.GetEntitiesForm(request.POST) if not form.is_valid(): return HttpResponseBadRequest(form.errors.as_json()) project = get_object_or_404(Project, slug=form.cleaned_data['project']) locale = get_object_or_404(Locale, code=form.cleaned_data['locale']) # Only return entities with provided IDs (batch editing) if form.cleaned_data['entity_ids']: return _get_entities_list(locale, project, form) # `Entity.for_project_locale` only requires a subset of the fields the form contains. We thus # make a new dict with only the keys we want to pass to that function. restrict_to_keys = ( 'paths', 'status', 'search', 'exclude_entities', 'extra', 'time', 'author', ) form_data = {k: form.cleaned_data[k] for k in restrict_to_keys if k in form.cleaned_data} entities = Entity.for_project_locale(project, locale, **form_data) # Only return a list of entity PKs (batch editing: select all) if form.cleaned_data['pk_only']: return JsonResponse({ 'entity_pks': list(entities.values_list('pk', flat=True)), }) # In-place view: load all entities if form.cleaned_data['inplace_editor']: return _get_all_entities(locale, project, form, entities) # Out-of-context view: paginate entities return _get_paginated_entities(locale, project, form, entities)
def entities(request): """Get entities for the specified project, locale and paths.""" form = forms.GetEntitiesForm(request.POST) if not form.is_valid(): return JsonResponse( { "status": False, "message": "{error}".format(error=form.errors.as_json(escape_html=True)), }, status=400, ) locale = get_object_or_404(Locale, code=form.cleaned_data["locale"]) preferred_source_locale = "" if request.user.is_authenticated: preferred_source_locale = request.user.profile.preferred_source_locale project_slug = form.cleaned_data["project"] if project_slug == "all-projects": project = Project(slug=project_slug) else: project = get_object_or_404(Project, slug=project_slug) # Only return entities with provided IDs (batch editing) if form.cleaned_data["entity_ids"]: return _get_entities_list(locale, preferred_source_locale, project, form) # `Entity.for_project_locale` only requires a subset of the fields the form contains. We thus # make a new dict with only the keys we want to pass to that function. restrict_to_keys = ( "paths", "status", "search", "exclude_entities", "extra", "time", "author", "tag", ) form_data = { k: form.cleaned_data[k] for k in restrict_to_keys if k in form.cleaned_data } try: entities = Entity.for_project_locale(project, locale, **form_data) except ValueError as error: return JsonResponse( { "status": False, "message": "{error}".format(error=error) }, status=500) # Only return a list of entity PKs (batch editing: select all) if form.cleaned_data["pk_only"]: return JsonResponse( {"entity_pks": list(entities.values_list("pk", flat=True))}) # In-place view: load all entities if form.cleaned_data["inplace_editor"]: return _get_all_entities(locale, preferred_source_locale, project, form, entities) # Out-of-context view: paginate entities return _get_paginated_entities(locale, preferred_source_locale, project, form, entities)
def entities(request): """Get entities for the specified project, locale and paths.""" form = forms.GetEntitiesForm(request.POST) if not form.is_valid(): return JsonResponse( { 'status': False, 'message': '{error}'.format(error=form.errors.as_json(escape_html=True)), }, status=400) locale = get_object_or_404(Locale, code=form.cleaned_data['locale']) preferred_source_locale = request.user.profile.preferred_source_locale project_slug = form.cleaned_data['project'] if project_slug == 'all-projects': project = Project(slug=project_slug) else: project = get_object_or_404(Project, slug=project_slug) # Only return entities with provided IDs (batch editing) if form.cleaned_data['entity_ids']: return _get_entities_list(locale, preferred_source_locale, project, form) # `Entity.for_project_locale` only requires a subset of the fields the form contains. We thus # make a new dict with only the keys we want to pass to that function. restrict_to_keys = ( 'paths', 'status', 'search', 'exclude_entities', 'extra', 'time', 'author', 'tag', ) form_data = { k: form.cleaned_data[k] for k in restrict_to_keys if k in form.cleaned_data } try: entities = Entity.for_project_locale(project, locale, **form_data) except ValueError as error: return JsonResponse( { 'status': False, 'message': '{error}'.format(error=error), }, status=500) # Only return a list of entity PKs (batch editing: select all) if form.cleaned_data['pk_only']: return JsonResponse({ 'entity_pks': list(entities.values_list('pk', flat=True)), }) # In-place view: load all entities if form.cleaned_data['inplace_editor']: return _get_all_entities(locale, preferred_source_locale, project, form, entities) # Out-of-context view: paginate entities return _get_paginated_entities(locale, preferred_source_locale, project, form, entities)