def new_plugin_page(request): """ Create a new plugin page. Create PageMeta in all existing languages. """ context = { "title": _("Create a new plugin page"), "form_url": request.path, "abort_url": reverse("admin:index"), } languages = Language.objects.all() # Create for every language a own PageMeta model form. pagemeta_formset = [] pagemeta_is_valid = True # Needed for check if all forms are valid. for lang in languages: if request.method == "POST": form = PageMetaForm(request.POST, prefix=lang.code) if not form.is_valid(): pagemeta_is_valid = False else: form = PageMetaForm(prefix=lang.code) form.language = lang # for language info in fieldset legend pagemeta_formset.append(form) if request.method == "POST": pagetree_form = PageTreeForm(request.POST) pluginpage_form = PluginPageForm(request.POST) if pagemeta_is_valid and pagetree_form.is_valid( ) and pluginpage_form.is_valid(): sid = transaction.savepoint() try: # Create new PageTree entry new_pagetree = pagetree_form.save(commit=False) new_pagetree.page_type = PageTree.PLUGIN_TYPE new_pagetree.save() # Create new PluginPage entry new_pluginpage = pluginpage_form.save(commit=False) new_pluginpage.pagetree = new_pagetree new_pluginpage.save( ) # needs primary key before a many-to-many relationship can be used. # Create all PageMeta entries and attach them to PluginPage new_pluginpage_instance = [] for language, pagemeta_form in zip(languages, pagemeta_formset): new_pagemeta = pagemeta_form.save(commit=False) new_pagemeta.pagetree = new_pagetree new_pagemeta.language = language new_pagemeta.save() except: transaction.savepoint_rollback(sid) raise else: transaction.savepoint_commit(sid) messages.info( request, _("New plugin page %r created.") % new_pluginpage) return http.HttpResponseRedirect( new_pluginpage.get_absolute_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) pluginpage_form = PluginPageForm() # A list of all existing forms -> for form errorlist all_forms = pagemeta_formset[:] + [pluginpage_form, pagetree_form] context.update({ "all_forms": all_forms, # For display the form error list from all existing forms. "has_errors": request.method == "POST", # At least one form has errors. # The forms: "pluginpage_form": pluginpage_form, "pagetree_form": pagetree_form, "pagemeta_formset": pagemeta_formset, }) return context
def _edit_plugin_page(request, context, pagetree): """ edit a PluginPage entry with PageMeta in all Languages """ pagemetas = PageMeta.objects.filter(pagetree=pagetree) pluginpage = PluginPage.objects.get(pagetree=pagetree) if request.method == "POST" and "cancel" in request.POST: url = pluginpage.get_absolute_url() messages.info(request, _("Edit plugin page (%s) aborted, ok.") % url) return http.HttpResponseRedirect(url) # Create for every language a own PageMeta model form. pagemeta_formset = [] pagemeta_is_valid = True # Needed for check if all forms are valid. for pagemeta in pagemetas: if request.method == "POST": form = PageMetaForm(request.POST, prefix=pagemeta.language.code, instance=pagemeta) if not form.is_valid(): pagemeta_is_valid = False else: form = PageMetaForm(prefix=pagemeta.language.code, instance=pagemeta) form.language = pagemeta.language # for language info in fieldset legend pagemeta_formset.append(form) if request.method == "POST": pagetree_form = PageTreeForm(request.POST, instance=pagetree) pluginpage_form = PluginPageForm(request.POST, instance=pluginpage) if pagemeta_is_valid and pagetree_form.is_valid( ) and pluginpage_form.is_valid(): sid = transaction.savepoint() try: pagetree_form.save() pluginpage_form.save() # Save all PageMeta entries and attach them to PluginPage new_pluginpage_instance = [] for pagemeta_form in pagemeta_formset: pagemeta_form.save() except: transaction.savepoint_rollback(sid) raise else: transaction.savepoint_commit(sid) messages.info(request, _("Plugin page %r updated.") % pluginpage) return http.HttpResponseRedirect(pluginpage.get_absolute_url()) else: pagetree_form = PageTreeForm(instance=pagetree) pluginpage_form = PluginPageForm(instance=pluginpage) # A list of all existing forms -> for form errorlist all_forms = pagemeta_formset[:] + [pluginpage_form, pagetree_form] context = { "title": _("Edit a plugin page"), "template_name": "page_admin/edit_plugin_page.html", "all_forms": all_forms, # For display the form error list from all existing forms. "has_errors": request.method == "POST", # At least one form has errors. # The forms: "pluginpage_form": pluginpage_form, "pagetree_form": pagetree_form, "pagemeta_formset": pagemeta_formset, } return context
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
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