예제 #1
0
    def __call__(self, *args, **kwargs):

        node = self.stack_node
        previewed_item = self.previewed_item
        publishable = self.preview_publishable
        preview_language = self.preview_language
        user = get_current_user()

        # Set the language for the preview
        if preview_language:
            set_language(preview_language)

        # Enforce permissions
        user.require_permission(ReadPermission, target=previewed_item)

        if publishable is not previewed_item:
            user.require_permission(ReadPermission, target=publishable)

        # Disable the preview if the item's unsaved state produces validation
        # errors; these would usually lead to unhandled server errors during
        # rendering.
        errors = schema.ErrorList(node.iter_errors())

        if errors:
            error_box = templates.new("cocktail.html.ErrorBox")
            error_box.errors = errors
            message = Element("div",
                              class_name="preview-error-box",
                              children=[
                                  translations(
                                      "woost.backoffice invalid item preview",
                                      preview_language), error_box
                              ])
            message.add_resource("/resources/styles/backoffice.css")
            return message.render_page()

        # Update the edited item with the data to preview
        node.import_form_data(node.form_data, previewed_item)

        self.context.update(original_publishable=self.context["publishable"],
                            publishable=publishable)

        controller = publishable.resolve_controller()

        if controller is None:
            raise cherrypy.NotFound()

        if isinstance(controller, type):
            controller = controller()

        return controller()
    def test_ignores_duplicate_resources(self):

        from cocktail.html import Element, DocumentMetadata

        a = Element()
        a.add_resource("/foo.js")

        b = Element()
        b.add_resource("/foo.js")

        metadata = DocumentMetadata()

        metadata.collect(a)
        resources1 = list(metadata.resources)

        metadata.collect(b)
        resources2 = list(metadata.resources)

        assert resources1 == resources2