def test_override_template(self): """ """ self.create_doc() doc = self.portal.doc overrider = IOverrider(doc) doc.setTitle("Foobar") template = u"Title {{ context.Title() }}" storage = IOverrideStorage(doc) storage.enabled_overrides = ["getUnfilteredTemplate"] storage.getUnfilteredTemplate = template self.assertEqual(overrider.getUnfilteredTemplate(), template)
def test_render_mobile_override(self): """ Render a document with mobile overrides enabled. """ self.setDiscriminateMode("mobile") self.create_doc() doc = self.portal.doc doc.setTitle("Not reached") # This title should not be visible in mobile mode overrider = IOverrider(doc) storage = IOverrideStorage(doc) storage.enabled_overrides = ["Title"] storage.Title = u"Foobar" self.portal.portal_workflow.doActionFor(doc, "submit") self.portal.portal_workflow.doActionFor(doc, "publish") browser = self.browser browser.open(self.portal.doc.absolute_url()) html = browser.contents self.assertTrue(MOBILE_HTML_MARKER in html, "Got page:" + html) assert "Foobar" in html
def test_override_not_enabled(self): """ Do not enable override, but have the field filled in """ self.setDiscriminateMode("web") self.create_doc() doc = self.portal.doc overrider = IOverrider(doc) doc.setTitle("Foobar") template = u"Title {{context.Title()}}" storage = IOverrideStorage(doc) storage.getUnfilteredTemplate = template self.assertNotEqual(overrider.getUnfilteredTemplate(), template)
def test_has_overrides(self): """ Document object overrides are enabled by default """ self.create_doc() doc = self.portal.doc overrider = IOverrider(doc) assert overrider != None assert overrider.schema != None self.assertEqual(overrider._getOverrideFieldNames(), ["Title", "Description", "getText"]) # Set one override enabled and check it is read properly storage = IOverrideStorage(doc) storage.enabled_overrides = ["Title"] self.assertEqual(overrider._isOverrideEnabled("Title", storage), True) self.assertEqual(overrider._isOverrideEnabled("Description", storage), False) # Set overriden title storage.Title = u"Foobar" # Call title accessor by emulator IOverrider steps # and see each microstep completes self.assertTrue(overrider._isOverride("Title")) storage = IOverrideStorage(self.portal.doc) self.assertTrue(overrider._isOverrideEnabled("Title", storage)) self.assertEqual(overrider._getOverrideOrOrignal("Title")(), u"Foobar") assert overrider.Title() == u"Foobar"
def test_helper_view_web(self): """ See that we get proper proxy object through helper view. """ self.setDiscriminateMode(MobileRequestType.WEB) self.create_doc() doc = self.portal.doc doc.setTitle("Not reached") overrider = IOverrider(doc) storage = IOverrideStorage(doc) storage.enabled_overrides = ["Title"] storage.Title = u"Foobar" helper = doc.restrictedTraverse("multichannel_overrider") context = helper() # Return mobile proxy object with overriden values # In web mode, you dont get override self.assertEqual(context.Title(), "Not reached")
def __init__(self, context, request): grok.View.__init__(self, context, request) self.overrider = IOverrider(self.context)
class EasyTemplateMobile(grok.View): """ Enable viewing of templated pages with mobile overrides """ grok.require("zope2.View") # TODO: How to make grok accept permissions as pseudo-constants grok.context(ITemplatedDocument) def __init__(self, context, request): grok.View.__init__(self, context, request) self.overrider = IOverrider(self.context) def title(self): """ """ return self.overrider.Title() def description(self): """ """ return self.overrider.Description() def cooked_text(self): """ @return: Resulting HTML of evaluated template """ return self._cookTemplate() def outputTemplateErrors(self, messages): """ Write template errors to the user and the log output. """ outputTemplateErrors(messages, request=self.request, logger=logger, context=self.context) def getTemplateSource(self): """ Try look up what text box we use for mobile rendering 1. Mobile "raw template" input 2. Mobile WYSIWYG 3. Generic raw template 4. Generic WYWIWYG """ storage = IOverrideStorage(self.context) # TODO: Bad API, will be changed if self.overrider._isOverrideEnabled("getUnfilteredTemplate", storage): # Raw HTML code for mobile text = self.overrider.getUnfilteredTemplate() elif self.overrider._isOverrideEnabled("getText", storage): # WYSIWYG HTML code for mobile text = self.overrider.getText() else: # Choose between normal kupu editor input # and unfiltered input unfiltered = self.context.getRawUnfilteredTemplate() if unfiltered != None and unfiltered.strip() != "": # We are using raw HTML input text = unfiltered.decode("utf-8") else: text = self.context.getText().decode("utf-8") return text def compile(self, text): """ Compile the template. """ engine = getEngine() if text == None: text = "" # TODO: Compile template only if the context has been changed t, messages = engine.loadString(text, False) return t, messages def _cookTemplate(self): """ Cook the view mode output. """ context = getTemplateContext(self.context) text = self.getTemplateSource() t, messages = self.compile(text) self.outputTemplateErrors(messages) if not t: return ERROR_MESSAGE output, messages = t.evaluate(context) self.outputTemplateErrors(messages) if not output: return ERROR_MESSAGE return unicode(output).encode("utf-8")