예제 #1
0
def _edit_content_page(request, context, pagetree):
    """ edit a PageContent """
    default_lang_entry = Language.objects.get_or_create_default(request)
    pagemeta = PageTree.objects.get_pagemeta(request, pagetree, show_lang_errors=True)
    pagecontent = PageContent.objects.get(pagemeta=pagemeta)

    if request.method != "POST":
        pagetree_form = PageTreeForm(instance=pagetree)
        pagemeta_form = PageMetaForm(instance=pagemeta, prefix=default_lang_entry.code)
        pagecontent_form = PageContentForm(instance=pagecontent)
    else:
        if "cancel" in request.POST:
            url = pagecontent.get_absolute_url()
            messages.info(request, _("Edit content page (%s) aborted, ok.") % url)
            return http.HttpResponseRedirect(url)

        pagetree_form = PageTreeForm(request.POST, instance=pagetree)
        pagemeta_form = PageMetaForm(request.POST, instance=pagemeta, prefix=default_lang_entry.code)
        pagecontent_form = PageContentForm(request.POST, instance=pagecontent)
        if not (pagetree_form.is_valid() and pagemeta_form.is_valid() and pagecontent_form.is_valid()):
            context["has_errors"] = True
        else:
            # All forms are valid -> Save all.
            sid = transaction.savepoint()
            try:
                pagetree_form.save()
                pagemeta_form.save()
                pagecontent_form.save()
            except:
                transaction.savepoint_rollback(sid)
                raise
            else:
                transaction.savepoint_commit(sid)
                messages.info(request, _("Content page %r updated.") % pagecontent)
                return http.HttpResponseRedirect(pagecontent.get_absolute_url())

    # A list of all existing forms -> for form errorlist
    all_forms = [pagecontent_form, pagemeta_form, pagetree_form]

    context.update({
        "title": _("Edit a content page"),
        "template_name": "page_admin/edit_content_page.html",
        "default_lang_entry": default_lang_entry,
        "pagecontent": pagecontent,

        "markup_id_str": str(pagecontent.markup),

        "all_forms": all_forms, # For display the form error list from all existing forms.

        "pagetree_form": pagetree_form,
        "pagemeta_form":pagemeta_form,
        "pagecontent_form": pagecontent_form,
    })
    return context
예제 #2
0
def new_content_page(request):
    """
    Create a new content page.
    
    TODO:
        * setup design via ajax, if not set and a parent page tree was selected
        * Auto generate slug from page name with javascript
    
    can use django.forms.models.inlineformset_factory:
        PageFormSet = inlineformset_factory(PageTree, PageContent, PageMeta)
    get:
        metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of
        the metaclasses of all its bases
    see also: http://code.djangoproject.com/ticket/7837
    """
    default_lang_entry = Language.objects.get_or_create_default(request)
    context = {
        "title": _("Create a new page"),
        "default_lang_entry": default_lang_entry,
        "form_url": request.path,
        "abort_url": reverse("admin:index"),
        "has_errors": request.method == "POST", # At least one form has errors.
    }

    if request.method == "POST":
        pagetree_form = PageTreeForm(request.POST)
        pagemeta_form = PageMetaForm(request.POST, prefix=default_lang_entry.code)
        pagecontent_form = PageContentForm(request.POST)
        if pagetree_form.is_valid() and pagemeta_form.is_valid() and pagecontent_form.is_valid():
            sid = transaction.savepoint()
            try:
                # Create new PageTree entry
                new_pagetree = pagetree_form.save(commit=False)
                new_pagetree.page_type = PageTree.PAGE_TYPE
                new_pagetree.save()

                # Create new PageMeta entry
                new_pagemeta = pagemeta_form.save(commit=False)
                new_pagemeta.pagetree = new_pagetree
                new_pagemeta.language = default_lang_entry
                new_pagemeta.save()

                # Create new PageContent entry
                new_pagecontent = pagecontent_form.save(commit=False)
                new_pagecontent.pagemeta = new_pagemeta
                new_pagecontent.save()
            except:
                transaction.savepoint_rollback(sid)
                raise
            else:
                transaction.savepoint_commit(sid)
                url = new_pagecontent.get_absolute_url()
                messages.info(request, _("New content page %r created.") % url)
                return http.HttpResponseRedirect(url)
    else:
        parent_pagetree = _get_pagetree(request)
        if parent_pagetree:
            context["abort_url"] = parent_pagetree.get_absolute_url() # Go back to the cms page
            initial_data = _build_form_initial(request, parent_pagetree)
        else:
            initial_data = {}
        pagetree_form = PageTreeForm(initial=initial_data)
        pagemeta_form = PageMetaForm(prefix=default_lang_entry.code)
        pagecontent_form = PageContentForm()

    # A list of all existing forms -> for form errorlist
    all_forms = [pagecontent_form, pagemeta_form, pagetree_form]

    context.update({
        "all_forms": all_forms, # For display the form error list from all existing forms.

        "pagetree_form": pagetree_form,
        "pagemeta_form":pagemeta_form,
        "pagecontent_form": pagecontent_form,

        "pagelinklist_url": "#TODO",
    })
    return context
예제 #3
0
def new_content_page(request):
    """
    Create a new content page.
    
    TODO:
        * setup design via ajax, if not set and a parent page tree was selected
        * Auto generate slug from page name with javascript
    
    can use django.forms.models.inlineformset_factory:
        PageFormSet = inlineformset_factory(PageTree, PageContent, PageMeta)
    get:
        metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of
        the metaclasses of all its bases
    see also: http://code.djangoproject.com/ticket/7837
    """
    default_lang_entry = Language.objects.get_or_create_default(request)
    context = {
        "title": _("Create a new page"),
        "default_lang_entry": default_lang_entry,
        "form_url": request.path,
        "abort_url": reverse("admin:index"),
        "has_errors":
        request.method == "POST",  # At least one form has errors.
    }

    if request.method == "POST":
        pagetree_form = PageTreeForm(request.POST)
        pagemeta_form = PageMetaForm(request.POST,
                                     prefix=default_lang_entry.code)
        pagecontent_form = PageContentForm(request.POST)
        if pagetree_form.is_valid() and pagemeta_form.is_valid(
        ) and pagecontent_form.is_valid():
            sid = transaction.savepoint()
            try:
                # Create new PageTree entry
                new_pagetree = pagetree_form.save(commit=False)
                new_pagetree.page_type = PageTree.PAGE_TYPE
                new_pagetree.save()

                # Create new PageMeta entry
                new_pagemeta = pagemeta_form.save(commit=False)
                new_pagemeta.pagetree = new_pagetree
                new_pagemeta.language = default_lang_entry
                new_pagemeta.save()

                # Create new PageContent entry
                new_pagecontent = pagecontent_form.save(commit=False)
                new_pagecontent.pagemeta = new_pagemeta
                new_pagecontent.save()
            except:
                transaction.savepoint_rollback(sid)
                raise
            else:
                transaction.savepoint_commit(sid)
                url = new_pagecontent.get_absolute_url()
                messages.info(request, _("New content page %r created.") % url)
                return http.HttpResponseRedirect(url)
    else:
        parent_pagetree = _get_pagetree(request)
        if parent_pagetree:
            context["abort_url"] = parent_pagetree.get_absolute_url(
            )  # Go back to the cms page
            initial_data = _build_form_initial(request, parent_pagetree)
        else:
            initial_data = {}
        pagetree_form = PageTreeForm(initial=initial_data)
        pagemeta_form = PageMetaForm(prefix=default_lang_entry.code)
        pagecontent_form = PageContentForm()

    # A list of all existing forms -> for form errorlist
    all_forms = [pagecontent_form, pagemeta_form, pagetree_form]

    context.update({
        "all_forms":
        all_forms,  # For display the form error list from all existing forms.
        "pagetree_form": pagetree_form,
        "pagemeta_form": pagemeta_form,
        "pagecontent_form": pagecontent_form,
        "pagelinklist_url": "#TODO",
    })
    return context
예제 #4
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
    cancel_url = source_pagemeta.get_absolute_url()

    if request.method == "POST" and "cancel" in request.POST:
        messages.info(request,
                      _("Translate page (%s) aborted, ok.") % cancel_url)
        return http.HttpResponseRedirect(cancel_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, cancel_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,
            "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:
            raise NotImplementedError("TODO: Must be reimplemented!")
#             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
예제 #5
0
def _edit_content_page(request, context, pagetree):
    """ edit a PageContent """
    default_lang_entry = Language.objects.get_or_create_default(request)
    pagemeta = PageTree.objects.get_pagemeta(request,
                                             pagetree,
                                             show_lang_errors=True)
    pagecontent = PageContent.objects.get(pagemeta=pagemeta)

    if request.method != "POST":
        pagetree_form = PageTreeForm(instance=pagetree)
        pagemeta_form = PageMetaForm(instance=pagemeta,
                                     prefix=default_lang_entry.code)
        pagecontent_form = PageContentForm(instance=pagecontent)
    else:
        if "cancel" in request.POST:
            url = pagecontent.get_absolute_url()
            messages.info(request,
                          _("Edit content page (%s) aborted, ok.") % url)
            return http.HttpResponseRedirect(url)

        pagetree_form = PageTreeForm(request.POST, instance=pagetree)
        pagemeta_form = PageMetaForm(request.POST,
                                     instance=pagemeta,
                                     prefix=default_lang_entry.code)
        pagecontent_form = PageContentForm(request.POST, instance=pagecontent)
        if not (pagetree_form.is_valid() and pagemeta_form.is_valid()
                and pagecontent_form.is_valid()):
            context["has_errors"] = True
        else:
            # All forms are valid -> Save all.
            sid = transaction.savepoint()
            try:
                pagetree_form.save()
                pagemeta_form.save()
                pagecontent_form.save()
            except:
                transaction.savepoint_rollback(sid)
                raise
            else:
                transaction.savepoint_commit(sid)
                messages.info(request,
                              _("Content page %r updated.") % pagecontent)
                return http.HttpResponseRedirect(
                    pagecontent.get_absolute_url())

    # A list of all existing forms -> for form errorlist
    all_forms = [pagecontent_form, pagemeta_form, pagetree_form]

    context.update({
        "title": _("Edit a content page"),
        "template_name": "page_admin/edit_content_page.html",
        "default_lang_entry": default_lang_entry,
        "pagecontent": pagecontent,
        "markup_id_str": str(pagecontent.markup),
        "all_forms":
        all_forms,  # For display the form error list from all existing forms.
        "pagetree_form": pagetree_form,
        "pagemeta_form": pagemeta_form,
        "pagecontent_form": pagecontent_form,
    })
    return context
예제 #6
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
예제 #7
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

    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)

    context = {"abort_url": source_pagemeta.get_absolute_url()}

    # select the destination languare
    result = _select_language(request, context, source_pagemeta)
    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.update(
        {
            "title": _("Translate page '%(name)s' (%(source_lang)s) into %(dest_lang)s.")
            % {
                "name": source_pagemeta.name,
                "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 not (
            source_pagemeta_form.is_valid()
            and source_pagecontent_form.is_valid()
            and dest_pagemeta_form.is_valid()
            and dest_pagecontent_form.is_valid()
        ):
            context["has_errors"] = True
        else:
            # All forms are valid -> Save all.
            context["has_errors"] = False
            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.info(request, "New content %r crerated." % new_pagecontent)
                else:
                    messages.info(request, "All updated.")
                return http.HttpResponseRedirect(new_pagemeta.get_absolute_url())
    else:
        context["has_errors"] = False
        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)

        if "prefill" in request.GET:
            filled_fields = []
            if "content" not in dest_pagecontent_form.initial:
                source_content = source_pagecontent_form.initial["content"]
                try:
                    translated_content = translate(source_content, src=source_language.code, to=dest_language.code)
                except ValueError, err:
                    messages.info(request, "Can't translate content with google: %s" % err)
                else:
                    dest_pagecontent_form.initial["content"] = translated_content
                    filled_fields.append("content")
                    dest_pagecontent_form.fields["content"].widget.attrs["class"] = "auto_translated"

            for key, source_value in source_pagemeta_form.initial.iteritems():
                if (
                    not source_value
                    or not isinstance(source_value, basestring)
                    or key == "robots"
                    or dest_pagemeta_form.initial.get(key, None)
                ):
                    # Skip empty, non string, robots field and if dest. value exist
                    continue

                try:
                    dest_value = translate(source_value, src=source_language.code, to=dest_language.code)
                except ValueError, err:
                    messages.info(request, "Can't translate %(key)s with google: %(err)s" % {"key": key, "err": err})
                else:
                    dest_pagemeta_form.initial[key] = dest_value
                    filled_fields.append(key)
                    dest_pagemeta_form.fields[key].widget.attrs["class"] = "auto_translated"

            if filled_fields:
                messages.info(request, "These fields are translated via google: %s" % ", ".join(filled_fields))
            else:
                messages.info(request, "No fields translated via google.")