示例#1
0
    def page_add_edit(self, request, id=None):
        model = self.model
        opts = model._meta

        if id:
            page = get_object_or_404(Page, pk=id)
            form = PageForm(request, instance=page)
            add = False
        else:
            page = None
            form = PageForm(request)
            add = True

        page_contents = not add and page.pagecontent_set.all()

        pagecontent_data = None

        if request.method == 'POST':
            if not add:
                pagecontent_forms = PageContentForm.get_forms(request)
                pagecontent_data = [pagecontent_form.render_js('from_template') for pagecontent_form in pagecontent_forms]


            if form.is_valid():
                page = form.save()

                if not add:
                    # Save the PageContents
                    for pagecontent_form in pagecontent_forms:
                        if pagecontent_form.id:
                            try:
                                page_content = PageContent.objects.get(pk=pagecontent_form.id)
                            except PageContent.DoesNotExist:
                                page_content = PageContent() # Is this ok?
                        else:
                            page_content = PageContent()
                        set_values(page_content, PAGECONTENT_FIELDS, pagecontent_form.cleaned_data)
                        page_content.page = page
                        page_content.save()

                # Inform the user
                if add:
                    request.user.message_set.create(message=_('The page was added successfully. Now you can create your content.'))
                else:
                    request.user.message_set.create(message=_('The page was updated successfully. You may edit it again below.'))

                return HttpResponseRedirect('../%d/' % page.id)

        else: # get
            if not add:
                pagecontent_data = []
                for page_content in page_contents:
                    content_form = PageContentForm(
                        initial=get_values(page_content, PAGECONTENT_FIELDS),
                        id=page_content.id)
                    pagecontent_data.append(content_form.render_js('from_template'))

        media = self.media
        if USE_TINYMCE:
            media.add_js(self.tinymce_js)
        media.add_js((
            "cms/js/dynamicforms.js",
            "cms/js/page_add.js",
        ))

        return render_to_response('cms/page_add.html', {
                'title': u'%s %s' % (add and _('Add') or _('Edit'), _('page')),
                'page': page,
                'form': form,
                'add':add,
                'page_contents': page_contents,
                'page_addons': PAGE_ADDONS,
                'pagecontent_template': PageContentForm().render_js('from_template'),
                'pagecontent_data': pagecontent_data,
                'use_tinymce': USE_TINYMCE,
                'root_path': self.admin_site.root_path,
                'media': mark_safe(media),
                'opts': opts,
            }, context_instance=RequestContext(request))
    def page_add_edit(self, request, id=None):
        model = self.model
        opts = model._meta

        if id:
            page = get_object_or_404(Page, pk=id)
            form = PageForm(request, instance=page)
            add = False
        else:
            page = None
            form = PageForm(request)
            add = True

        page_contents = not add and page.pagecontent_set.all()

        pagecontent_data = None

        if request.method == 'POST':
            if not add:
                pagecontent_forms = PageContentForm.get_forms(request)
                pagecontent_data = [pagecontent_form.render_js('from_template') for pagecontent_form in pagecontent_forms]
            if form.is_valid() and (add or pagecontent_forms.are_valid()):
                page = form.save()

                if not add:
                    # Save the PageContents
                    for pagecontent_form in pagecontent_forms:
                        if pagecontent_form.id:
                            try:
                                page_content = PageContent.objects.get(pk=pagecontent_form.id)
                            except PageContent.DoesNotExist:
                                page_content = PageContent() # Is this ok?
                        else:
                            page_content = PageContent()
                        set_values(page_content, PAGECONTENT_FIELDS, pagecontent_form.cleaned_data)
                        page_content.page = page
                        page_content.save()

                # Inform the user
                if add:
                    request.user.message_set.create(message=_('The page was added successfully. Now you can create your content.'))
                else:
                    request.user.message_set.create(message=_('The page was updated successfully. You may edit it again below.'))

                return HttpResponseRedirect('../%d/' % page.id)

        else: # get
            if not add:
                pagecontent_data = [PageContentForm(
                        initial=get_values(page_content, PAGECONTENT_FIELDS),
                        id=page_content.id
                    ).render_js('from_template') for page_content in page_contents]

        media = self.media
        if USE_TINYMCE:
            try:
                from tinymce import settings as tinymce_settings
            except ImportError:
                try:
                    from tinymce import widgets as tinymce_settings
                except ImportError:
                    raise ImportError, _('tinymce app not found in sys.path. Is tinymce app installed?')
            except NoReverseMatch:
                raise NoReverseMatch, _('Can not find tinymce helper script or app is misonfigured. Is tinymce app installed?')
            helper_script_url = reverse("tinymce-js", kwargs={'name': "cms",})
            media.add_js((
                tinymce_settings.JS_URL,
                helper_script_url,
            ))
        media.add_js((
            "cms/js/dynamicforms.js",
            "cms/js/page_add.js",
        ))

        return render_to_response('cms/page_add.html', {
                'title': u'%s %s' % (add and _('Add') or _('Edit'), _('page')),
                'page':page,
                'form':form,
                'add':add,
                'page_contents':page_contents,
                'page_addons': PAGE_ADDONS,
                'use_tinymce': unicode(USE_TINYMCE),
                'pagecontent_template':PageContentForm().render_js('from_template'),
                'pagecontent_data':pagecontent_data,
                'use_tinymce': USE_TINYMCE,
                'root_path': self.admin_site.root_path,
                'media': mark_safe(media),
                'opts': opts,
            }, context_instance=RequestContext(request))