Ejemplo n.º 1
0
    def test_pack_list(self):
        destinys_child = [
            Artist("Beyoncé"),
            Artist("Kelly Rowland"),
            Artist("Michelle Williams"),
        ]
        ctx = JSContext()
        result = ctx.pack(destinys_child)

        self.assertEqual(
            result,
            [
                {
                    "_type": "music.Artist",
                    "_args": ["Beyoncé"]
                },
                {
                    "_type": "music.Artist",
                    "_args": ["Kelly Rowland"]
                },
                {
                    "_type": "music.Artist",
                    "_args": ["Michelle Williams"]
                },
            ],
        )
Ejemplo n.º 2
0
    def test_recursive_arg_packing(self):
        dangerously_in_love = Album(
            "Dangerously in Love",
            [
                Artist("Beyoncé"),
            ],
        )
        ctx = JSContext()
        result = ctx.pack(dangerously_in_love)

        self.assertEqual(
            result,
            {
                "_type":
                "music.Album",
                "_args": [
                    "Dangerously in Love",
                    [
                        {
                            "_type": "music.Artist",
                            "_args": ["Beyoncé"]
                        },
                    ],
                ],
            },
        )

        self.assertIn("music_player.js", str(ctx.media))
Ejemplo n.º 3
0
class BlockWidget(forms.Widget):
    """Wraps a block object as a widget so that it can be incorporated into a Django form"""
    def __init__(self, block_def, attrs=None):
        super().__init__(attrs=attrs)
        self.block_def = block_def
        self.js_context = JSContext()
        self.block_json = json.dumps(self.js_context.pack(self.block_def))

    def render_with_errors(self,
                           name,
                           value,
                           attrs=None,
                           errors=None,
                           renderer=None):
        value_json = json.dumps(self.block_def.get_form_state(value))

        if errors:
            errors_json = json.dumps(self.js_context.pack(errors.as_data()))
        else:
            errors_json = '[]'

        return format_html("""
                <div id="{id}" data-block="{block_json}" data-value="{value_json}" data-errors="{errors_json}"></div>
                <script>
                    initBlockWidget('{id}');
                </script>
            """,
                           id=name,
                           block_json=self.block_json,
                           value_json=value_json,
                           errors_json=errors_json)

    def render(self, name, value, attrs=None, renderer=None):
        return self.render_with_errors(name,
                                       value,
                                       attrs=attrs,
                                       errors=None,
                                       renderer=renderer)

    @property
    def media(self):
        return self.js_context.media + forms.Media(
            js=[
                # needed for initBlockWidget, although these will almost certainly be
                # pulled in by the block adapters too
                versioned_static('wagtailadmin/js/telepath/telepath.js'),
                versioned_static('wagtailadmin/js/telepath/blocks.js'),
            ],
            css={
                'all': [
                    versioned_static(
                        'wagtailadmin/css/panels/streamfield.css'),
                ]
            })

    def value_from_datadict(self, data, files, name):
        return self.block_def.value_from_datadict(data, files, name)

    def value_omitted_from_data(self, data, files, name):
        return self.block_def.value_omitted_from_data(data, files, name)
Ejemplo n.º 4
0
    def test_avoid_primitive_value_references_for_short_strings(self):
        beyonce_name = "Beyoncé"
        beyonce = Artist(beyonce_name)
        discography = [
            Album("Dangerously in Love", [beyonce]),
            Album(beyonce_name, [beyonce]),
        ]
        ctx = JSContext()
        result = ctx.pack(discography)

        self.assertEqual(result, [
            {
                '_type':
                'music.Album',
                '_args': [
                    "Dangerously in Love",
                    [
                        {
                            '_type': 'music.Artist',
                            '_args': ["Beyoncé"],
                            '_id': 1,
                        },
                    ]
                ]
            },
            {
                '_type': 'music.Album',
                '_args': ["Beyoncé", [
                    {
                        '_ref': 1
                    },
                ]]
            },
        ])
Ejemplo n.º 5
0
    def test_pack_object(self):
        beyonce = Artist("Beyoncé")
        ctx = JSContext()
        result = ctx.pack(beyonce)

        self.assertEqual(result, {
            "_type": "music.Artist",
            "_args": ["Beyoncé"]
        })
Ejemplo n.º 6
0
    def test_pack_object(self):
        beyonce = Artist("Beyoncé")
        ctx = JSContext()
        result = ctx.pack(beyonce)

        self.assertEqual(result, {
            '_type': 'music.Artist',
            '_args': ["Beyoncé"]
        })
Ejemplo n.º 7
0
    def test_list_references(self):
        destinys_child = [
            Artist("Beyoncé"),
            Artist("Kelly Rowland"),
            Artist("Michelle Williams"),
        ]
        discography = [
            Album("Destiny's Child", destinys_child),
            Album("Survivor", destinys_child),
        ]
        ctx = JSContext()
        result = ctx.pack(discography)

        self.assertEqual(
            result,
            [
                {
                    "_type":
                    "music.Album",
                    "_args": [
                        "Destiny's Child",
                        {
                            "_list": [
                                {
                                    "_type": "music.Artist",
                                    "_args": ["Beyoncé"]
                                },
                                {
                                    "_type": "music.Artist",
                                    "_args": ["Kelly Rowland"]
                                },
                                {
                                    "_type": "music.Artist",
                                    "_args": ["Michelle Williams"],
                                },
                            ],
                            "_id":
                            0,
                        },
                    ],
                },
                {
                    "_type": "music.Album",
                    "_args": [
                        "Survivor",
                        {
                            "_ref": 0
                        },
                    ],
                },
            ],
        )
Ejemplo n.º 8
0
    def test_list_references(self):
        destinys_child = [
            Artist("Beyoncé"),
            Artist("Kelly Rowland"),
            Artist("Michelle Williams")
        ]
        discography = [
            Album("Destiny's Child", destinys_child),
            Album("Survivor", destinys_child),
        ]
        ctx = JSContext()
        result = ctx.pack(discography)

        self.assertEqual(result, [
            {
                '_type':
                'music.Album',
                '_args': [
                    "Destiny's Child", {
                        '_list': [
                            {
                                '_type': 'music.Artist',
                                '_args': ["Beyoncé"]
                            },
                            {
                                '_type': 'music.Artist',
                                '_args': ["Kelly Rowland"]
                            },
                            {
                                '_type': 'music.Artist',
                                '_args': ["Michelle Williams"]
                            },
                        ],
                        '_id':
                        0,
                    }
                ]
            },
            {
                '_type': 'music.Album',
                '_args': [
                    "Survivor",
                    {
                        '_ref': 0
                    },
                ]
            },
        ])
Ejemplo n.º 9
0
    def test_adapt(self):
        packed = JSContext().pack(
            SettingsMenuItem('settings', "Settings", [
                LinkMenuItem(
                    'groups', "Groups", '/groups/', icon_name='people'),
            ]))

        self.assertEqual(
            packed, {
                '_type':
                'wagtail.sidebar.SettingsMenuItem',
                '_args': [{
                    'name': 'settings',
                    'label': 'Settings',
                    'icon_name': '',
                    'classnames': ''
                },
                          [{
                              '_type':
                              'wagtail.sidebar.LinkMenuItem',
                              '_args': [{
                                  'name': 'groups',
                                  'label': 'Groups',
                                  'icon_name': 'people',
                                  'classnames': '',
                                  'url': '/groups/'
                              }]
                          }]]
            })
Ejemplo n.º 10
0
    def test_adapt_without_footer_text(self):
        packed = JSContext().pack(
            SubMenuItem('sub-menu', "Sub menu", [
                LinkMenuItem('link', "Link", '/link/', icon_name='link-icon'),
            ])
        )

        self.assertEqual(packed, {
            '_type': 'wagtail.sidebar.SubMenuItem',
            '_args': [
                {
                    'name': 'sub-menu',
                    'label': 'Sub menu',
                    'icon_name': '',
                    'classnames': '',
                    'footer_text': ''
                },
                [
                    {
                        '_type': 'wagtail.sidebar.LinkMenuItem',
                        '_args': [
                            {
                                'name': 'link',
                                'label': 'Link',
                                'icon_name': 'link-icon',
                                'classnames': '',
                                'url': '/link/'
                            }
                        ]
                    }
                ]
            ]
        })
Ejemplo n.º 11
0
def sidebar_props(context):
    request = context["request"]
    search_areas = admin_search_areas.search_items_for_request(request)
    if search_areas:
        search_area = search_areas[0]
    else:
        search_area = None

    account_menu = [
        sidebar.LinkMenuItem("account",
                             _("Account"),
                             reverse("wagtailadmin_account"),
                             icon_name="user"),
        sidebar.LinkMenuItem("logout",
                             _("Log out"),
                             reverse("wagtailadmin_logout"),
                             icon_name="logout"),
    ]

    modules = [
        sidebar.WagtailBrandingModule(),
        sidebar.SearchModule(search_area) if search_area else None,
        sidebar.MainMenuModule(admin_menu.render_component(request),
                               account_menu, request.user),
    ]
    modules = [module for module in modules if module is not None]

    return json_script(
        {
            "modules": JSContext().pack(modules),
        },
        element_id="wagtail-sidebar-props",
    )
Ejemplo n.º 12
0
def menu_props(context):
    request = context['request']
    search_areas = admin_search_areas.search_items_for_request(request)
    if search_areas:
        search_area = search_areas[0]
    else:
        search_area = None

    account_menu = [
        sidebar.LinkMenuItem('account',
                             _("Account"),
                             reverse('wagtailadmin_account'),
                             icon_name='user'),
        sidebar.LinkMenuItem('logout',
                             _("Log out"),
                             reverse('wagtailadmin_logout'),
                             icon_name='logout'),
    ]

    modules = [
        sidebar.WagtailBrandingModule(),
        sidebar.SearchModule(search_area) if search_area else None,
        sidebar.MainMenuModule(admin_menu.render_component(request),
                               account_menu, request.user),
    ]
    modules = [module for module in modules if module is not None]

    return json.dumps({
        'modules': JSContext().pack(modules),
    },
                      cls=DjangoJSONEncoder)
Ejemplo n.º 13
0
    def test_object_references(self):
        beyonce = Artist("Beyoncé")
        jay_z = Artist("Jay-Z")
        discography = [
            Album("Dangerously in Love", [beyonce]),
            Album("Everything Is Love", [beyonce, jay_z]),
        ]
        ctx = JSContext()
        result = ctx.pack(discography)

        self.assertEqual(
            result,
            [
                {
                    "_type":
                    "music.Album",
                    "_args": [
                        "Dangerously in Love",
                        [
                            {
                                "_type": "music.Artist",
                                "_args": ["Beyoncé"],
                                "_id": 0
                            },
                        ],
                    ],
                },
                {
                    "_type":
                    "music.Album",
                    "_args": [
                        "Everything Is Love",
                        [
                            {
                                "_ref": 0
                            },
                            {
                                "_type": "music.Artist",
                                "_args": ["Jay-Z"]
                            },
                        ],
                    ],
                },
            ],
        )

        self.assertIn("music_player.js", str(ctx.media))
Ejemplo n.º 14
0
    def test_primitive_value_references(self):
        beyonce_name = "Beyoncé Giselle Knowles-Carter"
        beyonce = Artist(beyonce_name)
        discography = [
            Album("Dangerously in Love", [beyonce]),
            Album(beyonce_name, [beyonce]),
        ]
        ctx = JSContext()
        result = ctx.pack(discography)

        self.assertEqual(
            result,
            [
                {
                    "_type":
                    "music.Album",
                    "_args": [
                        "Dangerously in Love",
                        [
                            {
                                "_type":
                                "music.Artist",
                                "_args": [{
                                    "_val": "Beyoncé Giselle Knowles-Carter",
                                    "_id": 0
                                }],
                                "_id":
                                1,
                            },
                        ],
                    ],
                },
                {
                    "_type": "music.Album",
                    "_args": [
                        {
                            "_ref": 0
                        },
                        [
                            {
                                "_ref": 1
                            },
                        ],
                    ],
                },
            ],
        )
Ejemplo n.º 15
0
    def test_adapt(self):
        packed = JSContext().pack(
            SearchModule(SearchArea("Search", '/search/')))

        self.assertEqual(packed, {
            '_type': 'wagtail.sidebar.SearchModule',
            '_args': ['/search/']
        })
Ejemplo n.º 16
0
    def test_adapt(self):
        packed = JSContext().pack(
            SearchModule(SearchArea("Search", "/search/")))

        self.assertEqual(packed, {
            "_type": "wagtail.sidebar.SearchModule",
            "_args": ["/search/"]
        })
Ejemplo n.º 17
0
    def test_adapt(self):
        packed = JSContext().pack(
            CustomBrandingModule('<h1>My custom branding</h1>'))

        self.assertEqual(
            packed, {
                '_type': 'wagtail.sidebar.CustomBrandingModule',
                '_args': ['<h1>My custom branding</h1>', False]
            })
Ejemplo n.º 18
0
    def test_dict_reserved_words(self):
        profile = {
            '_artist': Artist("Beyoncé"),
            '_type': 'R&B',
        }
        ctx = JSContext()
        result = ctx.pack(profile)

        self.assertEqual(
            result, {
                '_dict': {
                    '_artist': {
                        '_type': 'music.Artist',
                        '_args': ["Beyoncé"]
                    },
                    '_type': 'R&B',
                }
            })
Ejemplo n.º 19
0
 def test_pack_dict(self):
     glastonbury = {
         'pyramid_stage': Artist("Beyoncé"),
         'acoustic_stage': Artist("Ed Sheeran"),
     }
     ctx = JSContext()
     result = ctx.pack(glastonbury)
     self.assertEqual(
         result, {
             'pyramid_stage': {
                 '_type': 'music.Artist',
                 '_args': ["Beyoncé"]
             },
             'acoustic_stage': {
                 '_type': 'music.Artist',
                 '_args': ["Ed Sheeran"]
             },
         })
Ejemplo n.º 20
0
    def test_object_references(self):
        beyonce = Artist("Beyoncé")
        jay_z = Artist("Jay-Z")
        discography = [
            Album("Dangerously in Love", [beyonce]),
            Album("Everything Is Love", [beyonce, jay_z]),
        ]
        ctx = JSContext()
        result = ctx.pack(discography)

        self.assertEqual(result, [
            {
                '_type':
                'music.Album',
                '_args': [
                    "Dangerously in Love",
                    [
                        {
                            '_type': 'music.Artist',
                            '_args': ["Beyoncé"],
                            '_id': 0
                        },
                    ]
                ]
            },
            {
                '_type':
                'music.Album',
                '_args': [
                    "Everything Is Love",
                    [
                        {
                            '_ref': 0
                        },
                        {
                            '_type': 'music.Artist',
                            '_args': ["Jay-Z"]
                        },
                    ]
                ]
            },
        ])

        self.assertIn('music_player.js', str(ctx.media))
Ejemplo n.º 21
0
    def test_collapsible(self):
        packed = JSContext().pack(CustomBrandingModule('<h1>My custom branding</h1>', collapsible=True))

        self.assertEqual(packed, {
            '_type': 'wagtail.sidebar.CustomBrandingModule',
            '_args': [
                '<h1>My custom branding</h1>',
                True
            ]
        })
Ejemplo n.º 22
0
    def test_dict_reserved_words(self):
        profile = {
            "_artist": Artist("Beyoncé"),
            "_type": "R&B",
        }
        ctx = JSContext()
        result = ctx.pack(profile)

        self.assertEqual(
            result,
            {
                "_dict": {
                    "_artist": {
                        "_type": "music.Artist",
                        "_args": ["Beyoncé"]
                    },
                    "_type": "R&B",
                }
            },
        )
Ejemplo n.º 23
0
 def test_pack_dict(self):
     glastonbury = {
         "pyramid_stage": Artist("Beyoncé"),
         "acoustic_stage": Artist("Ed Sheeran"),
     }
     ctx = JSContext()
     result = ctx.pack(glastonbury)
     self.assertEqual(
         result,
         {
             "pyramid_stage": {
                 "_type": "music.Artist",
                 "_args": ["Beyoncé"]
             },
             "acoustic_stage": {
                 "_type": "music.Artist",
                 "_args": ["Ed Sheeran"]
             },
         },
     )
Ejemplo n.º 24
0
    def test_primitive_value_references(self):
        beyonce_name = "Beyoncé Giselle Knowles-Carter"
        beyonce = Artist(beyonce_name)
        discography = [
            Album("Dangerously in Love", [beyonce]),
            Album(beyonce_name, [beyonce]),
        ]
        ctx = JSContext()
        result = ctx.pack(discography)

        self.assertEqual(result, [
            {
                '_type':
                'music.Album',
                '_args': [
                    "Dangerously in Love",
                    [
                        {
                            '_type':
                            'music.Artist',
                            '_args': [{
                                '_val': "Beyoncé Giselle Knowles-Carter",
                                '_id': 0
                            }],
                            '_id':
                            1,
                        },
                    ]
                ]
            },
            {
                '_type': 'music.Album',
                '_args': [{
                    '_ref': 0
                }, [
                    {
                        '_ref': 1
                    },
                ]]
            },
        ])
Ejemplo n.º 25
0
    def test_adapt(self):
        packed = JSContext().pack(WagtailBrandingModule())

        self.assertEqual(packed['_type'],
                         'wagtail.sidebar.WagtailBrandingModule')
        self.assertEqual(len(packed['_args']), 2)
        self.assertEqual(packed['_args'][0], reverse('wagtailadmin_home'))
        self.assertEqual(
            packed['_args'][1].keys(), {
                'desktopLogoBody', 'desktopLogoEyeClosed',
                'desktopLogoEyeOpen', 'desktopLogoTail', 'mobileLogo'
            })
Ejemplo n.º 26
0
    def test_avoid_primitive_value_references_for_short_strings(self):
        beyonce_name = "Beyoncé"
        beyonce = Artist(beyonce_name)
        discography = [
            Album("Dangerously in Love", [beyonce]),
            Album(beyonce_name, [beyonce]),
        ]
        ctx = JSContext()
        result = ctx.pack(discography)

        self.assertEqual(
            result,
            [
                {
                    "_type":
                    "music.Album",
                    "_args": [
                        "Dangerously in Love",
                        [
                            {
                                "_type": "music.Artist",
                                "_args": ["Beyoncé"],
                                "_id": 1,
                            },
                        ],
                    ],
                },
                {
                    "_type": "music.Album",
                    "_args": [
                        "Beyoncé",
                        [
                            {
                                "_ref": 1
                            },
                        ],
                    ],
                },
            ],
        )
Ejemplo n.º 27
0
    def test_recursive_arg_packing(self):
        dangerously_in_love = Album("Dangerously in Love", [
            Artist("Beyoncé"),
        ])
        ctx = JSContext()
        result = ctx.pack(dangerously_in_love)

        self.assertEqual(
            result, {
                '_type':
                'music.Album',
                '_args': [
                    "Dangerously in Love",
                    [
                        {
                            '_type': 'music.Artist',
                            '_args': ["Beyoncé"]
                        },
                    ]
                ]
            })

        self.assertIn('music_player.js', str(ctx.media))
Ejemplo n.º 28
0
    def test_pack_list(self):
        destinys_child = [
            Artist("Beyoncé"),
            Artist("Kelly Rowland"),
            Artist("Michelle Williams")
        ]
        ctx = JSContext()
        result = ctx.pack(destinys_child)

        self.assertEqual(result, [
            {
                '_type': 'music.Artist',
                '_args': ["Beyoncé"]
            },
            {
                '_type': 'music.Artist',
                '_args': ["Kelly Rowland"]
            },
            {
                '_type': 'music.Artist',
                '_args': ["Michelle Williams"]
            },
        ])
Ejemplo n.º 29
0
    def test_adapt_with_classnames_and_icon(self):
        packed = JSContext().pack(LinkMenuItem('link', "Link", '/link/', icon_name='link-icon', classnames='some classes'))

        self.assertEqual(packed, {
            '_type': 'wagtail.sidebar.LinkMenuItem',
            '_args': [
                {
                    'classnames': 'some classes',
                    'icon_name': 'link-icon',
                    'label': 'Link',
                    'name': 'link',
                    'url': '/link/'
                }
            ]
        })
Ejemplo n.º 30
0
    def test_adapt(self):
        packed = JSContext().pack(LinkMenuItem('link', "Link", '/link/'))

        self.assertEqual(packed, {
            '_type': 'wagtail.sidebar.LinkMenuItem',
            '_args': [
                {
                    'classnames': '',
                    'icon_name': '',
                    'label': 'Link',
                    'name': 'link',
                    'url': '/link/'
                }
            ]
        })