Ejemplo n.º 1
0
def importCustomstyles(import_context):
    """Import custom styles defined as a json file named "customstyles.json"
    within any generic setup directory.

    The file contains the exportable JSON and may be extended with a "path" key
    containing the relative path to the target object where the custom styles
    should be set on. When there is no "path", the styles are set on the site
    root.
    """

    files = import_context.listDirectory('.') or []
    filenames = filter(FILENAME_PATTERN.match, files)

    for filename in filenames:
        filedata = import_context.readDataFile(filename)
        if filedata is None:
            continue

        styles = json.loads(filedata)
        site = import_context.getSite()
        if 'path' in styles:
            context = site.restrictedTraverse(styles['path'].encode('utf-8'))
            del styles['path']
        else:
            context = site

        ICustomStyles(context).set_styles(styles)
 def test_touch_iphone_config(self, browser):
     ICustomStyles(self.portal).set('img.touch_iphone', '%PORTAL_URL%/t.png')
     transaction.commit()
     browser.open()
     tag = browser.css('link[rel="apple-touch-icon"]').first
     self.assertEquals('http://nohost/plone/t.png',
                       tag.attrib.get('href'))
 def test_startup_image_config(self, browser):
     ICustomStyles(self.portal).set('img.startup', '%PORTAL_URL%/s.png')
     transaction.commit()
     browser.open()
     tag = browser.css('link[rel="apple-touch-startup-image"]').first
     self.assertEquals('http://nohost/plone/s.png',
                       tag.attrib.get('href'))
 def test_favicon_config(self, browser):
     ICustomStyles(self.portal).set('img.favicon', '%PORTAL_URL%/my-fav.ico')
     transaction.commit()
     browser.open()
     tag = browser.css('link[type="image/x-icon"]').first
     self.assertEquals('http://nohost/plone/my-fav.ico',
                       tag.attrib.get('href'))
    def test_result_is_cached(self):
        view = self.get_view()
        self.assertNotIn('purple', view.generate_css(),
                         'Unexpectedly found "purple" in the CSS')

        # Setting a custom style automatically invalidates the cache.
        # For testing that things are cached, we stub the cache invalidation,
        # so that the cache persists.
        mocker = Mocker()
        invalidate_cache_mock = mocker.replace(invalidate_cache)
        expect(invalidate_cache_mock()).count(1, None)
        mocker.replay()

        ICustomStyles(self.layer['portal']).set('css.body-background',
                                                'purple')
        self.assertNotIn('purple', view.generate_css(),
                         'The result was not cached.')

        # Removing the stub and invalidating the cache should update the result.
        mocker.restore()
        mocker.verify()
        invalidate_cache()
        self.assertIn(
            'purple', view.generate_css(),
            'Expected "purple" in CSS - does the style'
            ' css.body-background no longer work?')
Ejemplo n.º 6
0
    def test_customstyles_are_stored_in_btree(self):
        adapter = ICustomStyles(self.portal)
        adapter.set('css.body-background', 'blue')

        annotations = IAnnotations(self.portal)
        self.assertEquals(OOBTree,
                          type(annotations.get(CUSTOMSTYLES_ANNOTATION_KEY)))
Ejemplo n.º 7
0
def exportCustomstyles(export_context):
    """Export custom styles defined on the site root.
    """

    site = export_context.getSite()
    styles = ICustomStyles(site).get_styles()
    data = json.dumps(styles, sort_keys=True, indent=4)
    export_context.writeDataFile('customstyles.json', data, 'application/json')
    def setUp(self):
        self.portal = self.layer['portal']
        setRoles(self.portal, TEST_USER_ID, ['Manager'])
        login(self.portal, TEST_USER_NAME)

        if ONEGOV_THEME_INSTALLED:
            from plonetheme.onegov.interfaces import ICustomStyles
            self.custom = ICustomStyles(self.portal)
            self.input_ = {'css.body-background': 'red'}
            self.custom.set_styles(self.input_)
Ejemplo n.º 9
0
    def test_caches_are_invalidated_when_setting_NEW_values(self):
        adapter = ICustomStyles(self.portal)
        self.assertEquals({}, adapter.get_styles())

        invalidate_cache_mock = self.mocker.replace(
            'plonetheme.onegov.browser.customstyles.invalidate_cache')
        self.expect(invalidate_cache_mock())
        self.replay()

        adapter.set('css.body-background', 'blue')
Ejemplo n.º 10
0
    def test_caches_are_invalidated_when_resetting_EXISTING_values(self):
        adapter = ICustomStyles(self.portal)
        adapter.set('css.body-background', 'red')

        invalidate_cache_mock = self.mocker.replace(
            'plonetheme.onegov.browser.customstyles.invalidate_cache')
        self.expect(invalidate_cache_mock())
        self.replay()

        adapter.set('css.body-background', 'blue')
Ejemplo n.º 11
0
 def get_options(self):
     nav_root = self.context.restrictedTraverse(
         getNavigationRoot(self.context))
     options = ICustomStyles(nav_root).get_styles()
     styles = []
     for key, value in options.items():
         if value and key.startswith('css.'):
             styles.append('$%s: %s;' % (key.replace('css.', ''), value))
         if value and key == 'custom_scss':
             styles.append(value)
     return '\n'.join(styles)
Ejemplo n.º 12
0
    def export_styles(self, download=False):
        """Returns a json file containing the styles.
        """
        if download:
            normalizer = getUtility(IIDNormalizer)
            normalized_title = normalizer.normalize(self.context.Title())
            self.context.REQUEST.RESPONSE.setHeader(
                'Content-Type', 'text/json; charset=utf-8')
            self.context.REQUEST.RESPONSE.setHeader(
                'Content-disposition',
                'attachment; filename=customstyles_%s.json' % normalized_title)

        return json.dumps(ICustomStyles(self.context).get_styles())
Ejemplo n.º 13
0
    def update(self):
        super(MetaViewlet, self).update()
        navroot = self.context.restrictedTraverse(
            getNavigationRoot(self.context))
        self.customstyles = ICustomStyles(navroot).get_styles()

        self.favicon = self.customstyle_value('img.favicon') or \
            '/'.join((navroot.absolute_url(), 'favicon.ico'))
        self.startup = self.customstyle_value('img.startup')
        self.touch_iphone = self.customstyle_value('img.touch_iphone')
        self.touch_iphone_76 = self.customstyle_value('img.touch_iphone_76')
        self.touch_iphone_120 = self.customstyle_value('img.touch_iphone_120')
        self.touch_iphone_152 = self.customstyle_value('img.touch_iphone_152')
Ejemplo n.º 14
0
    def test_mutating_styles_directly_does_not_store_them(self):
        adapter = ICustomStyles(self.portal)

        input = {'css.body-background': 'red'}
        adapter.set_styles(input)

        output = adapter.get_styles()
        self.assertEquals(input, output)

        output['css.body-background'] = 'green'
        self.assertEquals(
            input, adapter.get_styles(),
            'The result of get_styles could be modified so that'
            ' it changed in the storage, which should not happen.')
Ejemplo n.º 15
0
    def save_values(self, items):
        def include(item):
            name, _value = item
            if name.startswith('css.'):
                return True
            if name.startswith('img.'):
                return True
            if name == 'custom_scss':
                return True
            return False

        styles = dict(filter(include, items.items()))
        adapter = ICustomStyles(self.context)
        styles[TIMESTAMP_ANNOTATION_KEY] = str(time.time()).replace('.', '')
        adapter.set_styles(styles)
 def get_style(self, style, of=None):
     context = of or self.layer['portal']
     return ICustomStyles(context).get(style)
def link_color_in(context):
    return ICustomStyles(context).get('css.link-color')
Ejemplo n.º 18
0
 def options(self):
     return ICustomStyles(self.context).get_styles()
Ejemplo n.º 19
0
 def setData(self, themedata, metadata):
     ICustomStyles(self.obj).set_styles(themedata)
Ejemplo n.º 20
0
 def getData(self):
     return ICustomStyles(self.obj).get_styles()
Ejemplo n.º 21
0
 def test_new_timestamp_after_save_styles(self):
     timestamp = 'a random timestamp'
     ICustomStyles(self.portal).set(TIMESTAMP_ANNOTATION_KEY, timestamp)
     CustomStylesForm(self.portal, self.request).save_values({})
     self.assertNotEquals(timestamp,
                          self.get_viewlet(self.portal).timestamp())
Ejemplo n.º 22
0
    def test_setting_and_getting_styles(self):
        adapter = ICustomStyles(self.portal)

        styles = {'css.body-background': 'red'}
        adapter.set_styles(styles)
        self.assertEquals(styles, adapter.get_styles())
Ejemplo n.º 23
0
    def test_setting_and_getting_single_style(self):
        adapter = ICustomStyles(self.portal)

        adapter.set('css.body-background', 'blue')
        self.assertEquals('blue', adapter.get('css.body-background'))
Ejemplo n.º 24
0
 def timestamp(self):
     nav_root = self.context.restrictedTraverse(
         getNavigationRoot(self.context))
     return ICustomStyles(nav_root).get(TIMESTAMP_ANNOTATION_KEY, '')
Ejemplo n.º 25
0
 def test_timestamp_from_annotations(self):
     timestamp = 'a random timestamp'
     ICustomStyles(self.portal).set(TIMESTAMP_ANNOTATION_KEY, timestamp)
     self.assertEquals(timestamp, self.get_viewlet(self.portal).timestamp())