Пример #1
0
    def test_toolbar_switch_urls(self):
        user = self.get_superuser()
        user_settings = UserSettings(language="en", user=user)
        placeholder = Placeholder(slot="clipboard")
        placeholder.save()
        user_settings.clipboard = placeholder
        user_settings.save()

        page = create_page("page", "nav_playground.html", "en", published=True)
        create_title("fr", "french home", page)
        publish_page(page, user, "fr")

        with self.login_user_context(user):
            response = self.client.get(
                "/fr/?%s" % get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON'))
            self.assertContains(
                response,
                "/fr/?%s" % get_cms_setting('CMS_TOOLBAR_URL__EDIT_OFF'), 1,
                200)
            response = self.client.get(
                "/fr/?%s" % get_cms_setting('CMS_TOOLBAR_URL__EDIT_OFF'))
            self.assertContains(
                response,
                "/fr/?%s" % get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON'), 1,
                200)
Пример #2
0
    def __init__(self, request):
        super(CMSToolbar, self).__init__()
        self.right_items = []
        self.left_items = []
        self.populated = False
        self.post_template_populated = False
        self.menus = {}
        self.request = request
        self.login_form = CMSToolbarLoginForm(request=request)
        self.is_staff = self.request.user.is_staff
        self.edit_mode = self.is_staff and self.request.session.get('cms_edit', False)
        self.edit_mode_url_on = get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON')
        self.edit_mode_url_off = get_cms_setting('CMS_TOOLBAR_URL__EDIT_OFF')
        self.build_mode = self.is_staff and self.request.session.get('cms_build', False)
        self.use_draft = self.is_staff and self.edit_mode or self.build_mode
        self.show_toolbar = self.is_staff or self.request.session.get('cms_edit', False)
        self.obj = None
        self.redirect_url = None
        if settings.USE_I18N:
            self.language = get_language_from_request(request)
        else:
            self.language = settings.LANGUAGE_CODE

        # We need to store the current language in case the user's preferred language is different.
        self.toolbar_language = self.language

        if self.is_staff:
            try:
                user_settings = UserSettings.objects.select_related('clipboard').get(user=self.request.user)
            except UserSettings.DoesNotExist:
                user_settings = UserSettings(language=self.language, user=self.request.user)
                placeholder = Placeholder(slot="clipboard")
                placeholder.save()
                user_settings.clipboard = placeholder
                user_settings.save()
            if (settings.USE_I18N and user_settings.language in dict(settings.LANGUAGES)) or (
                    not settings.USE_I18N and user_settings.language == settings.LANGUAGE_CODE):
                self.toolbar_language = user_settings.language
            else:
                user_settings.language = self.language
                user_settings.save()
            self.clipboard = user_settings.clipboard
        with force_language(self.language):
            try:
                self.app_name = resolve(self.request.path).app_name
            except Resolver404:
                self.app_name = ""
        toolbars = toolbar_pool.get_toolbars()

        self.toolbars = SortedDict()
        app_key = ''
        for key in toolbars:
            app_name = ".".join(key.split(".")[:-2])
            if app_name == self.app_name and len(key) > len(app_key):
                app_key = key
        for key in toolbars:
            toolbar = toolbars[key](self.request, self, key == app_key, app_key)
            self.toolbars[key] = toolbar
Пример #3
0
 def get_user_settings(self):
     user_settings = None
     if self.is_staff:
         try:
             user_settings = UserSettings.objects.select_related('clipboard').get(user=self.request.user)
         except UserSettings.DoesNotExist:
             user_settings = UserSettings(language=self.language, user=self.request.user)
             placeholder = Placeholder(slot="clipboard")
             placeholder.save()
             user_settings.clipboard = placeholder
             user_settings.save()
     return user_settings
Пример #4
0
    def test_toolbar_switch_urls(self):
        user = self.get_superuser()
        user_settings = UserSettings(language="en", user=user)
        placeholder = Placeholder(slot="clipboard")
        placeholder.save()
        user_settings.clipboard = placeholder
        user_settings.save()

        page = create_page("page", "nav_playground.html", "en", published=True)
        create_title("fr", "french home", page)
        publish_page(page, user, "fr")

        page.set_as_homepage()

        edit_on = get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON')
        edit_off = get_cms_setting('CMS_TOOLBAR_URL__EDIT_OFF')

        with self.login_user_context(user):
            response = self.client.get("/fr/?{}".format(edit_on))
            expected = """
                <a href="?structure" class="cms-btn cms-btn-disabled" title="Toggle structure"
                data-cms-structure-btn='{ "url": "/fr/?structure", "name": "Structure" }'
                data-cms-content-btn='{ "url": "/fr/?edit", "name": "Content" }'>
                <span class="cms-icon cms-icon-plugins"></span></a>
            """
            self.assertContains(
                response,
                expected,
                count=1,
                html=True,
            )
            self.assertContains(
                response,
                '<a class="cms-btn cms-btn-switch-save" href="/fr/?preview&{}">'
                '<span>View published</span></a>'.format(edit_off),
                count=1,
                html=True,
            )
            response = self.client.get("/fr/?preview&{}".format(edit_off))
            self.assertContains(
                response,
                expected,
                count=1,
                html=True,
            )
            self.assertContains(
                response,
                '<a class="cms-btn cms-btn-action cms-btn-switch-edit" href="/fr/?{}">Edit</a>'
                .format(edit_on),
                count=1,
                html=True,
            )
Пример #5
0
    def __init__(self, request):
        super(CMSToolbar, self).__init__()
        self.right_items = []
        self.left_items = []
        self.populated = False
        self.post_template_populated = False
        self.menus = {}
        self.request = request
        self.login_form = CMSToolbarLoginForm(request=request)
        self.is_staff = self.request.user.is_staff
        self.edit_mode = self.is_staff and self.request.session.get(
            'cms_edit', False)
        self.edit_mode_url_on = get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON')
        self.edit_mode_url_off = get_cms_setting('CMS_TOOLBAR_URL__EDIT_OFF')
        self.build_mode = self.is_staff and self.request.session.get(
            'cms_build', False)
        self.use_draft = self.is_staff and self.edit_mode or self.build_mode
        self.show_toolbar = self.is_staff or self.request.session.get(
            'cms_edit', False)
        self.obj = None
        self.redirect_url = None
        if settings.USE_I18N:
            self.language = get_language_from_request(request)
        else:
            self.language = settings.LANGUAGE_CODE

        # We need to store the current language in case the user's preferred language is different.
        self.toolbar_language = self.language

        if self.is_staff:
            try:
                user_settings = UserSettings.objects.select_related(
                    'clipboard').get(user=self.request.user)
            except UserSettings.DoesNotExist:
                user_settings = UserSettings(language=self.language,
                                             user=self.request.user)
                placeholder = Placeholder(slot="clipboard")
                placeholder.save()
                user_settings.clipboard = placeholder
                user_settings.save()
            if (settings.USE_I18N and user_settings.language in dict(
                    settings.LANGUAGES)) or (not settings.USE_I18N
                                             and user_settings.language
                                             == settings.LANGUAGE_CODE):
                self.toolbar_language = user_settings.language
            else:
                user_settings.language = self.language
                user_settings.save()
            self.clipboard = user_settings.clipboard
        with force_language(self.language):
            try:
                decorator = resolve(self.request.path_info).func
                try:
                    # If the original view is decorated we try to extract the real function
                    # module instead of the decorator's one
                    if decorator and getattr(decorator, 'func_closure', False):
                        # python 2
                        self.app_name = decorator.func_closure[
                            0].cell_contents.__module__
                    elif decorator and getattr(decorator, '__closure__',
                                               False):
                        # python 3
                        self.app_name = decorator.__closure__[
                            0].cell_contents.__module__
                    else:
                        raise AttributeError()
                except (TypeError, AttributeError):
                    # no decorator
                    self.app_name = decorator.__module__
            except Resolver404:
                self.app_name = ""
        toolbars = toolbar_pool.get_toolbars()
        parts = self.app_name.split('.')
        while parts:
            path = '.'.join(parts)
            if path in installed_apps():
                self.app_name = path
                break
            parts.pop()

        self.toolbars = SortedDict()
        for key in toolbars:
            toolbar = toolbars[key](self.request, self,
                                    toolbars[key].check_current_app(
                                        key, self.app_name), self.app_name)
            self.toolbars[key] = toolbar