Esempio n. 1
0
def translate_blog_entry(request, id=None):
    if id is None:
        raise

    source_entry = BlogEntryContent.objects.get(pk=id)
    absolute_url = source_entry.get_absolute_url()

    # select the destination language
    result = select_language(request, absolute_url, source_entry.language, source_entry.headline)
    if isinstance(result, Language):
        # Language was selected or they exit only one other language
        dest_language = result
    elif isinstance(result, dict):
        # template context returned -> render language select form
        return result
    elif isinstance(result, http.HttpResponse):
        # e.g. error
        return result
    else:
        raise RuntimeError() # Should never happen


    context = {
        "title": _("Translate a blog entry"),
        "template_name": "blog/translate_blog_entry.html",
        "abort_url": absolute_url,
    }

    try:
        dest_entry = BlogEntryContent.objects.get(entry=source_entry.entry, language=dest_language)
    except BlogEntryContent.DoesNotExist:
        dest_entry = None
        dest_initial = {
            "entry": source_entry.entry,
            "language":dest_language,
            "markup": source_entry.markup,
            "is_public": source_entry.is_public,
        }

    if request.method == "POST":
        source_form = BlogContentForm(request.POST, prefix="source", instance=source_entry)

        if dest_entry is None:
            dest_form = BlogContentForm(request.POST, prefix="dest", initial=dest_initial)
        else:
            dest_form = BlogContentForm(request.POST, prefix="dest", instance=dest_entry)

        if "autotranslate" in request.POST:
            if source_form.is_valid():
                dest_form, filled_fields, errors = prefill(
                    source_form, dest_form,
                    source_entry.language, dest_language,
                    only_fields=("headline", "content"),
                    #debug=True,
                )
                if filled_fields:
                    messages.success(request, "These fields are translated with google: %s" % ", ".join(filled_fields))
                else:
                    messages.info(request, "No fields translated with google, because all fields have been a translation.")
                if errors:
                    for error in errors:
                        messages.error(request, error)
        else:
            if source_form.is_valid() and dest_form.is_valid():
                # All forms are valid -> Save all.
                source_form.save()
                dest_entry2 = dest_form.save(commit=False)
                dest_entry2.entry = source_entry.entry
                dest_entry2.save()
                if dest_entry is None:
                    messages.success(request, "All saved. New entry %r created." % dest_entry2)
                else:
                    messages.success(request, "All saved.")
                return http.HttpResponseRedirect(dest_entry2.get_absolute_url())
    else:
        source_form = BlogContentForm(prefix="source", instance=source_entry)
        if dest_entry is None:
            dest_form = BlogContentForm(prefix="dest", initial=dest_initial)
        else:
            dest_form = BlogContentForm(prefix="dest", instance=dest_entry)

    source_fields = []
    dest_fields = []
    line_fields = []
    for source_field, dest_field in zip(source_form, dest_form):
        assert source_field.name == dest_field.name
        if source_field.name in ("content", "language", "markup"):
            # Fields witch displayed side by side.
            source_fields.append(source_field)
            dest_fields.append(dest_field)
        else:
            # Fields witch renders alternating one below the other.
            source_field.language = source_entry.language
            line_fields.append(source_field)
            dest_field.language = dest_language
            line_fields.append(dest_field)

    if source_form.errors or dest_form.errors:
        has_errors = True
    else:
        has_errors = False

    context.update({
        "source_entry": source_entry,
        "dest_language": dest_language,
        "source_form": source_form,
        "dest_form": dest_form,
        "all_forms": [source_form, dest_form],
        "has_errors": has_errors,
        "source_fields": source_fields,
        "dest_fields": dest_fields,
        "line_fields": line_fields,
    })
    return context
Esempio n. 2
0
def translate_page(request, pagemeta_id=None):
    if not pagemeta_id:
        raise

    source_pagemeta = PageMeta.objects.get(id=pagemeta_id)
    pagetree = source_pagemeta.pagetree
    source_language = source_pagemeta.language
    absolute_url = source_pagemeta.get_absolute_url()

    is_pluginpage = pagetree.page_type == PageTree.PLUGIN_TYPE
    if is_pluginpage:
        messages.error(request, "TODO: Translate a plugin page!")
        return http.HttpResponseRedirect(source_pagemeta.get_absolute_url())
    else:
        source_pagecontent = PageContent.objects.get(pagemeta=source_pagemeta)


    # select the destination language
    result = select_language(request, absolute_url, source_pagemeta.language, source_pagemeta.name)
    if isinstance(result, Language):
        # Language was selected or they exit only one other language
        dest_language = result
    elif isinstance(result, dict):
        # template context returned -> render language select form
        return result
    elif isinstance(result, http.HttpResponse):
        # e.g. error
        return result
    else:
        raise RuntimeError() # Should never happen


    context = {
        "title": _("Translate page '%(name)s' (%(source_lang)s) into %(dest_lang)s.") % {
            "name": source_pagemeta.name,
            "abort_url": absolute_url,
            "source_lang": source_pagemeta.language.description,
            "dest_lang": dest_language.description,
        },
        "template_name": "page_admin/translate_page.html",
    }

    try:
        dest_pagemeta = PageMeta.objects.get(pagetree=pagetree, language=dest_language)
    except PageMeta.DoesNotExist:
        dest_pagemeta = None
    else:
        dest_pagecontent = PageContent.objects.get(pagemeta=dest_pagemeta)

    if request.method == "POST":
        source_pagemeta_form = PageMetaForm(
            request.POST, prefix="source", instance=source_pagemeta
        )
        source_pagecontent_form = PageContentForm(
            request.POST, prefix="source", instance=source_pagecontent
        )
        if dest_pagemeta is None:
            dest_pagemeta_form = PageMetaForm(request.POST, prefix="dest")
            dest_pagecontent_form = PageContentForm(request.POST, prefix="dest")
        else:
            dest_pagemeta_form = PageMetaForm(
                request.POST, prefix=dest_language.code, instance=dest_pagemeta
            )
            dest_pagecontent_form = PageContentForm(
                request.POST, prefix=dest_language.code, instance=dest_pagecontent
            )

        if "autotranslate" in request.POST:
            if source_pagemeta_form.is_valid() and source_pagecontent_form.is_valid():
                all_filled_fields = []
                all_errors = []

                # Translate PageContent
                dest_pagecontent_form, filled_fields, errors = prefill(
                    source_pagecontent_form, dest_pagecontent_form,
                    source_pagemeta.language, dest_language,
                    only_fields=("content",),
                    #debug=True,
                )
                all_filled_fields += filled_fields
                all_errors += errors

                # Translate fields from PageMeta
                dest_pagemeta_form, filled_fields, errors = prefill(
                    source_pagemeta_form, dest_pagemeta_form,
                    source_pagemeta.language, dest_language,
                    only_fields=("name", "title", "description"),
                    #debug=True,
                )
                all_filled_fields += filled_fields
                all_errors += errors


                if all_filled_fields:
                    messages.success(request, "These fields are translated with google: %s" % ", ".join(all_filled_fields))
                else:
                    messages.info(request, "No fields translated with google, because all fields have been a translation.")
                if all_errors:
                    for error in all_errors:
                        messages.error(request, error)
        else:
            # don't translate -> save if valid
            if (source_pagemeta_form.is_valid() and source_pagecontent_form.is_valid() and
                                dest_pagemeta_form.is_valid() and dest_pagecontent_form.is_valid()):
                # All forms are valid -> Save all.
                sid = transaction.savepoint()
                try:
                    source_pagecontent_form.save()
                    source_pagemeta_form.save()

                    # Create new PageMeta entry
                    new_pagemeta = dest_pagemeta_form.save(commit=False)
                    new_pagemeta.pagetree = pagetree
                    new_pagemeta.language = dest_language
                    new_pagemeta.save()

                    # Create new PageContent entry
                    new_pagecontent = dest_pagecontent_form.save(commit=False)
                    new_pagecontent.pagemeta = new_pagemeta
                    new_pagecontent.save()
                except:
                    transaction.savepoint_rollback(sid)
                    raise
                else:
                    transaction.savepoint_commit(sid)
                    if dest_pagemeta is None:
                        messages.success(request, "New content %r created." % new_pagecontent)
                    else:
                        messages.success(request, "All updated.")
                    return http.HttpResponseRedirect(new_pagemeta.get_absolute_url())
    else:
        source_pagemeta_form = PageMetaForm(
            prefix="source", instance=source_pagemeta
        )
        source_pagecontent_form = PageContentForm(
            prefix="source", instance=source_pagecontent
        )
        if dest_pagemeta is None:
            dest_pagemeta_form = PageMetaForm(
                prefix="dest", initial={
                    "robots": source_pagemeta.robots,
                    "permitViewGroup": source_pagemeta.permitViewGroup, # FIXME: Doesn't work
                }
            )
            dest_pagecontent_form = PageContentForm(
                prefix="dest", initial={
                    "markup": source_pagecontent.markup,
                }
            )
        else:
            dest_pagemeta_form = PageMetaForm(
                prefix=dest_language.code, instance=dest_pagemeta
            )
            dest_pagecontent_form = PageContentForm(
                prefix=dest_language.code, instance=dest_pagecontent
            )


    source_pagecontent_form.language = source_language
    dest_pagecontent_form.language = dest_language

    pagemeta_fields = []
    for source_field, dest_field in zip(source_pagemeta_form, dest_pagemeta_form):
        source_field.language = source_language
        pagemeta_fields.append(source_field)
        dest_field.language = dest_language
        pagemeta_fields.append(dest_field)

    all_forms = [
        source_pagemeta_form, source_pagecontent_form,
        dest_pagemeta_form, dest_pagecontent_form
    ]
    has_errors = False
    for form in all_forms:
        if form.errors:
            has_errors = True
            break

    context.update({
        "abort_url": source_pagemeta.get_absolute_url(),
        "all_forms": all_forms,
        "has_errors": has_errors,
        "source_pagemeta_form": source_pagemeta_form,
        "source_pagecontent_form": source_pagecontent_form,
        "dest_pagemeta_form": dest_pagemeta_form,
        "dest_pagecontent_form": dest_pagecontent_form,

        "pagemeta_fields": pagemeta_fields,
    })
    return context