Skip to content

Translate

hypha.apply.funds.views.translate

TranslateSubmissionView

Bases: View

template class-attribute instance-attribute

template = 'funds/includes/translate_application_form.html'

dispatch

dispatch(request, *args, **kwargs)
Source code in hypha/apply/funds/views/translate.py
def dispatch(self, request, *args, **kwargs):
    self.submission = get_object_or_404(
        ApplicationSubmission, id=kwargs.get("pk")
    )
    if not request.user.is_org_faculty:
        messages.warning(
            self.request,
            "User attempted to translate submission but is not org faculty",
        )
        return HttpResponseRedirect(self.submission.get_absolute_url())
    return super(TranslateSubmissionView, self).dispatch(
        request, *args, **kwargs
    )

get

get(*args, **kwargs)
Source code in hypha/apply/funds/views/translate.py
def get(self, *args, **kwargs):
    raise Http404

post

post(*args, **kwargs)
Source code in hypha/apply/funds/views/translate.py
def post(self, *args, **kwargs):
    raise Http404

partial_translate_answers

partial_translate_answers(request, pk)

Partial to translate submissions's answers

Parameters:

  • request (HttpRequest) –

    HttpRequest object

  • pk (int) –

    pk of the submission to translate

Source code in hypha/apply/funds/views/translate.py
@login_required
def partial_translate_answers(request: HttpRequest, pk: int) -> HttpResponse:
    """Partial to translate submissions's answers

    Args:
        request: HttpRequest object
        pk: pk of the submission to translate

    """
    if not settings.APPLICATION_TRANSLATIONS_ENABLED:
        raise Http404

    submission = get_object_or_404(ApplicationSubmission, pk=pk)

    if not request.user.is_org_faculty or request.method != "GET":
        return HttpResponse(status=204)

    ctx = {"object": submission}

    # The existing params that were in the URL when the request was made
    prev_params = get_translation_params(request.headers.get("Hx-Current-Url", ""))
    # The requested params provided in the GET request
    params = get_translation_params(request=request)

    updated_url = submission.get_absolute_url()

    message = None

    if params and not params[0] == params[1] and not params == prev_params:
        from_lang, to_lang = params
        try:
            submission.form_data = translate_application_form_data(
                submission, from_lang, to_lang
            )

            if current_url := request.headers.get("Hx-Current-Url"):
                updated_params = QueryDict(urlparse(current_url).query, mutable=True)
                updated_params["fl"] = from_lang
                updated_params["tl"] = to_lang
                updated_url = f"{updated_url}?{updated_params.urlencode()}"

            to_lang_name = get_lang_name(to_lang)
            from_lang_name = get_lang_name(from_lang)

            message = _("Submission translated from {fl} to {tl}.").format(
                fl=from_lang_name, tl=to_lang_name
            )

            ctx.update(
                {
                    "object": submission,
                    "from_lang_name": from_lang_name,
                    "to_lang_name": to_lang_name,
                }
            )
        except ValueError:
            # TODO: WA Error/failed message type rather than success
            message = _("Submission translation failed. Contact your Administrator.")
            return HttpResponse(
                status=400,
                headers={"HX-Trigger": json.dumps({"showMessage": {message}})},
            )

    elif params == prev_params:
        message = _("Translation cleared.")

    response = render(request, "funds/includes/rendered_answers.html", ctx)

    trigger_dict = {}
    if title := submission.form_data.get("title"):
        trigger_dict.update(
            {
                "translatedSubmission": {
                    "appTitle": title,
                    "docTitle": submission.title_text_display,
                }
            }
        )

    if message:
        trigger_dict.update({"showMessage": message})

    if trigger_dict:
        response["HX-Trigger"] = json.dumps(trigger_dict)

    response["HX-Replace-Url"] = updated_url

    return response